spring boot是一款轻量级的spring框架。该框架避免了以往框架中众多的xml配置文件,采用注解方式进行配置,使得项目简洁易于理解,减小了上手难度。java
1. 建立独立的Spring应用程序web
2. 嵌入的Tomcat,无需部署WAR文件spring
3. 简化Maven配置apache
4. 自动配置Springapp
5. 提供生产就绪型功能,如指标,健康检查和外部配置框架
6. 绝对没有代码生成和对XML没有要求配置maven
projectspring-boot
-----src测试
-----------mainui
-------------------java.com.xxx...
-------------------resource
----------------------------application.properties
-----------test.java
-----------test.resource
-----pom.xml
例如:
pom.xml文件的配置例子以下:
<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> <artifactId>spring-boot-test</artifactId> <groupId>com.rainman</groupId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-test</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
添加启动程序SimpleRun.java,代码以下:
package com.rainman; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @EnableAutoConfiguration public class SimpleRun { public static void main(String[] args){ SpringApplication.run(SimpleRun.class,args); } }
添加接口
添加启动程序HelloController.java,代码以下:
package com.rainman; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @EnableAutoConfiguration public class HelloController { @RequestMapping("/hello") @ResponseBody public String hello(){ return "呵呵呵"; } public static void main(String[] args){ SpringApplication.run(HelloController.class,args); } }
运行结果:
PS:在application.properties中添加配置项server.port能够调整项目的端口号
1.添加启动程序BootRun.java,代码以下:
package com.rainman; import com.rainman.conf.ApplicationConf; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication @EnableConfigurationProperties({ApplicationConf.class}) public class BootRun { public static void main(String[] args){ SpringApplication.run(BootRun.class, args); } }
2. 添加配置文件ApplicationConf.java,代码以下:
package com.rainman.conf; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix="conf",locations = "classpath:application.properties") public class ApplicationConf { private String user; private String pw; public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPw() { return pw; } public void setPw(String pw) { this.pw = pw; } }
3.在配置文件application.properties中添加配置信息,以下
conf.user=hello conf.pw=hello
4.添加测试接口LoginController.java,代码以下
package com.rainman.controller; import com.rainman.conf.ApplicationConf; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class LoginController { @Autowired private ApplicationConf applicationConf; @RequestMapping("/login") @ResponseBody public String login(){ StringBuffer loginInfo = new StringBuffer("恭喜你,登录了!!<br>"); loginInfo.append("userName:"+applicationConf.getUser()+"<br>"); loginInfo.append("pw:"+applicationConf.getPw()+"<br>"); return loginInfo.toString(); } }
运行结果为: