spring boot 是Pivotal team提供的spring全新整合框架,用于简化spring应用的初始化搭建和开发,使用特定的法国女士来配置,是开发员不再关心定义样板化配置,致力于快速开发领域repid application development. simplify the development of spring process.
spring boot使用Groocy语言编写,借助Groovy强大的metaObject协议,可拔插的AST转换协议和内置依赖解决方案引擎,在核心的编译模型中boot使用Groovy构建工程文件,所以它可以使用通用的导入和样板方法(mian)对类所生成的字节码进行装饰。
1.spring boot基于spring,他将我们日常开发spring框架进行整合,以最小的依赖配置完成我们开发过程中繁琐的过程。
pom.xml依赖:
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>0.5.0.BUILD-SNAPSHOT</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>
<!-- Allow access to Spring milestones and snapshots -->
<!-- (you don't need this if you are using anything after 0.5.0.RELEASE) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
<snapshots><enabled>true</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>
继承spring-boot-starter-parent后我们可以继承一些默认的依赖,这样就无需添加一堆相应的依赖,把依赖配置最小化;spring-boot-starter-web提供了对web的支持,spring-boot-maven-plugin提供了直接运行项目的插件,我们可以直接mvn spring-boot:run运行。
2.直接使用@RestController和 @EnableAutoConfiguration注解开启自动配置,SpringApplication.run(.class)启动。
@EnableAutoConfiguration
@RestController
@RequestMapping("/user")
public class TestController {
@RequestMapping("/{name}")
public String view(@PathVariable("name") String name) {
return name;
}
//public static void main(String[] args) {
// SpringApplication.run(UserController.class);
//}
}
本地启动后,浏览器打开http://localhost:8080/user/cw即可看到cw
3.使用@Configuration 和@ComponentScan自动查找扫描并注册相应的容器组建
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
我们看基础的spring boot提供的pom依赖可以发现,包含了所有springkaungjia基础基础的jar,如果我们项目中所需要的jar无需这么多我们可以自己手动写明,不引入parent.