spring boot零配置,模块化开发,一直以来吸引这众多人的眼球
spring boot依赖pom文件:
1.pom.xml配置
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.0.M5</version> </parent> <dependencies> <!-- 添加web应用程序依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- spring boot测试--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <finalName>spring-boot-demo</finalName> <resources> <resource> <directory>/src/main/resources</directory> </resource> </resources> <!-- 打包一个可执行的应用程序--> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- 添加spring 仓库--> <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>http://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories>
2.编写一个Demo类
/** * Created by on 15-10-17. */ @EnableAutoConfiguration @RestController public class Example { @RequestMapping(value = "/") public String home(){ return "Hello World!"; } public static void main(String[] args){ SpringApplication.run(Example.class , args); } }
3.启动main函数后,在控制台中可以发现启动了一个Tomcat容器,一个基于Spring MVC的应用也同时启动起来,这时访问http://localhost:8080
就可以看到Hello World!
出现在浏览器中了。
备注:
@RestController:官方解释:用于spring MVC处理request请求。@RestController结合了@Controller和@ResponseBody注解,这也导致方法返回的结果是数据而不是视图。