1. 程式人生 > >Spring Boot入門例項

Spring Boot入門例項

簡介

Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成為領導者。

Spring Boot提供了一個強大的一鍵式Spring的整合開發環境,能夠單獨進行一個Spring應用的開發,其中:

(1)集中式配置(application.properties)+註解,大大簡化了開發流程
(2)內嵌的Tomcat和Jetty容器,可直接打成jar包啟動,無需提供Java war包以及繁瑣的Web配置
(3)提供了Spring各個外掛的基於Maven的pom模板配置,開箱即用,便利無比。
(4)可以在任何你想自動化配置的地方,實現可能
(5)提供更多的企業級開發特性,如何系統監控,健康診斷,許可權控制
(6) 無冗餘程式碼生成和XML強制配置
(7)提供支援強大的Restfult風格的編碼,非常簡潔

Pom依賴
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.umgsai</groupId>
<artifactId>springboot-demo</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-demo Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <build> <finalName>springboot-demo</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <mainClass>${start-class}</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.springboot.demo.SampleController</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>assemble-all</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
Controller
@Controller
//@EnableAutoConfiguration
@SpringBootApplication
public class SampleController {

    @RequestMapping("/home")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

@RequestMapping("/user")
@Controller
public class UserController {


    @ResponseBody
    @RequestMapping("/login")
    public String login(){
        return "{status:200}";
    }
}
打包與執行

執行SampleController類中的main方法就可以啟動了

也可以將專案打包為jar包的方式執行,打包命令如下

mvn -DskipTests clean package

執行命令

java -jar springboot-demo.jar

參考連結:http://www.infoq.com/cn/articles/microframeworks1-spring-boot
http://www.cnblogs.com/woshimrf/p/5730363.html