1. 核心
Spring Boot 核心是入口类和@SpringBootApplication。入口类的名称是artifactId+Application,@SpringBootApplication注解是一个组合注解,主要MetaAnnotation为@Configuration和@EnableAutoConfiguration,以及@ComponentScan。其中@EnableAutoConfiguration让Spring Boot根据类路径中的jar包依赖为当前项目自动配置。
2. 配置文件
Spring Boot 的配置文件形式为application.properties或application.yml,后一种文件格式的后缀全称是yet another mark language。配置文件位于src/main/resources(如果是maven形式)下。
.properteis形式:
server.port=9009
server.servlet.context-path=/hello
.yml形式:
server:
port: 9009
servlet:
context-path: /hello
spring boot 的properties设置参考:
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html。application.properties和application.yml中的属性可对属性的默认值进行覆盖,并可添加新的属性值对。
3. stater pom
采用Spring Tool Suit 或直接使用https://start.spring.io创建项目会自动建立一个start pom。创建一个基于web的项目,pom内容示例如下:
<?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>com-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>com-springboot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
其中parent子元素spring-boot-starter-parent(artifactId)申明包含了多个jar包,通过@SpringBootApplication注解的@EnableAutoConfiguration注解,可对各个jar包中的Bean进行自动配置。
除了官方的start pom外,还有第三方为Spring Boot编写的pom文件。在pom形式之外,还可以通过在类上添加@ImportSource注解来加载xml配置文件。如:
@ImportSource({“classpath:contextA.xml”, “classpath:contextB.xml”})
public class DemoClass {
@Inject
//注入contextA.xml,contextB.xml配置的类
}
4. 属性使用方法
无论是application.properties还是自建的properties文件中属性值对,都可以采用两种方法进行使用。
第一种:
在field上使用@Value(“${属性名}”)注入,形式与Spring中注入形式相同。
第二种:
通过类型安全配置,即新建一个属性名称与配置文件属性名称一致的关联类(A),在该类上用@ConfigurationProperties注解,再在需要使用属性的类(B)上用@EnableConfigurationProperties(A类.class)在Container中注册关联类,最后在使用属性的类中用@Autowired注入关联类的Bean。示例如下:
a. application.properties和自定义属性配置文件
application.properties:
book.name=springboot
book.author=cruise
自定义属性文件,位置为config/cruise.properties:
cruise.name=apple
cruise.address=The first street.
b .类型安全的Bean
通过application.properties配置的Bean:
package com.example.configproperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="book") //使用配置文件中前缀为book的属性
public class BookSetting {
private String name;
private String author;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
通过自定义properties文件配置的Bean:
package com.example.configproperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="cruise")
public class CruiseSetting {
private String name;
private String address;
public String getAddress() {
return address;
}
public String getName() {
return name;
}
public void setAddress(String address) {
this.address = address;
}
public void setName(String name) {
this.name = name;
}
}
c. 使用属性的类
该类对直接使用application.properties和自定义配置文件,以及使用类型安全的Bean等多种属性使用方式进行了测试。
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.configproperty.BookSetting;
import com.example.configproperty.CruiseSetting;
//使用非applicaiton.properties的配置文件需要什么位置
@PropertySource("classpath:/config/cruise.properties")
//向容器注册类型安全配置的类,自此这些类可以用于注入
@EnableConfigurationProperties(value = {BookSetting.class, CruiseSetting.class})
@RestController
@SpringBootApplication
public class ComSpringbootApplication {
// 直接使用application.properties
@Value(value = "${book.name}")
private String bookName;
@Value("${book.author}")
private String bookAuthor;
//使用自定义properties文件
@Value("${cruise.name}")
private String cruiseName;
@Value("${cruise.address}")
private String cruiseAddress;
//注入通过application.properties文件使用类型安全配置的Bean
@Autowired
private BookSetting bookSetting;
//注入通过自定义properties文件使用类型安全配置的Bean
@Autowired
private CruiseSetting cruiseSetting;
@RequestMapping(value = {"/index", "/"})
public String index() {
return "Use properties Directly from application.properties: book name is " + bookName
+ ", and book author is " + bookAuthor + "<br/>"
+ "Use properties from Self definition properties file: cruise name is " + cruiseSetting.getName()
+ ", and cruise address is " + cruiseSetting.getAddress() + "<br/>"
+ "Use properties from application.properties with Bean: book name is " + bookSetting.getName()
+ ", and book author is " + bookSetting.getAuthor() + "<br/>"
+ "Use properties from Self definition properties file with Bean: cruise name is " + cruiseSetting.getName()
+ ", and cruise address is " + cruiseSetting.getAddress();
}
public static void main(String[] args) {
SpringApplication.run(ComSpringbootApplication.class, args);
}
}
5. 测试结果
浏览器显示:
Use properties Directly from application.properties: book name is springboot, and book author is cruise
Use properties from Self definition properties file: cruise name is apple, and cruise address is The first street.
Use properties from application.properties with Bean: book name is springboot, and book author is cruise
Use properties from Self definition properties file with Bean: cruise name is apple, and cruise address is The first street.