Spring Boot是Spring框架的一个新的子项目,用于创建Spring 4.0项目,它可以自动配置Spring的各种组件,并不依赖代码生成和XML配置文件。Spring Boot也提供了对于常见场景的推荐组件配置。Spring Boot可以大大提升使用Spring框架时的开发效率。
二:Spring Boot 应用
通过Spring Boot,创建新的Spring应用变得非常容易,而且创建出的Spring应用符合通用的最佳实践,下面通过一个简单的例子讲解Spring Boot的应用。
1、pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <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.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.5.RELEASE</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、执行应用的java类:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class Application { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }
3、启动程序:
直接运行Application.java
4、访问应用程序:
http://localhost:8080
在页面输出Hello World!