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