pom.xml文件:html
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jessexu</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.4.1.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.1.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
build中的插件是用於打成可執行的fat-jar包。java
新建一個hello包:web
@Controller @EnableAutoConfiguration @ComponentScan(basePackages={"service"}) public class SampleController { @Autowired private UserService userService; @RequestMapping("/user") @ResponseBody String home() { return userService.getName(); } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } }
再新建一個service包,新建UserService:spring
package service; import org.springframework.stereotype.Service; @Service public class UserService { public String getName(){ return "hello world"; } }
運行: localhost:8080/user
數據庫
註解 @EnableAutoConfiguration
:這是一個自動配置的註解,用於自動配置一些默認配置,好比web.xml中的配置。數據庫的配置等等。詳細說明能夠看這裏:
http://docs.spring.io/spring-...apache