核心功能
1.可直接运行jar包
2.内嵌servlet容器(如tomcat)
3.提供很多starter pom简化maven依赖加载
4.自动配置spring
5.具有生产级的应用监控
6.基于注解,而不是自动生成代码,无xml配置
优点
1·可快速构建项目并运行起来
2·与大多主流框架无配置集成
3·内嵌servlet容器
4.应用监控
5·极大提高开发效率
6·与云计算天然集成
创建springboot项目
使用maven直接创建:添加公共依赖,插件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.M1</version>
<relativePath/>
</parent>
添加starter pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
添加插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
程碑版的Spring Boot配置,若使用的是正式版则不需要下面的配置。
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
直接使用idea 创建
新建Spring Initializr项目
1·新建
2·填写项目信息
3·选择使用技术
4·填写项目名
5·项目设置为Maven项目,若无提示,可忽略该步骤
等待加载
6·项目结构及依赖树如图5-15所示。
springboot全局配置文件
application.properties
book.author=wangyunfei
book.name =spring boot
Bean
package com.wisely.ch6_2_3.config;
import org.springframework.boot.context.properties.ConfigurationProperties; //加载配置文件
@Component
@ConfigurationProperties(prefix = "author") //1
public class AuthorSettings {
private String name;
private Long age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getAge() {
return age;
}
public void setAge(Long age) {
this.age = age;
}
}
测试
@RestController
@SpringBootApplication
public class Ch623Application {
@Autowired
private AuthorSettings authorSettings; //1
@RequestMapping("/")
public String index(){
return "author name is "+ authorSettings.getName()+" and author age is "+authorSettings.getAge();
}
public static void main(String[] args) {
SpringApplication.run(Ch623Application.class, args);
}
}
运行,访问:http://localhost:8080/,效果如图6-5所示。