本文最后更新于:2024年9月8日 晚上
Spring 配置文件
Spring
- Spring使用一个全局的配置文件
applicationContext.xml
alias
- alias 设置别名,为bean设置别名,可以设置多个别名。
1 2
| <alias name="userT" alias="userNew"/>
|
import
1
| <import resource="{path}/beans.xml"/>
|
context:annotation-config
- 而使用
<context:annotation-config/>
就可以隐式地自动向Spring容器注册4个BeanPostProcessor
1 2 3 4
| AutowiredAnnotationBeanPostProcessor CommonAnnotationBeanPostProcessor PersistenceAnnotationBeanPostProcessor RequiredAnnotationBeanPostProcessor
|
- 这样就可以使用
@Resource
,@PostConstruct
, @PreDestroy
,@PersistenceContext
,@Autowired
,@Required
等注解了,就可以实现自动注入。
- 注册这4个
BeanPostProcessor
的作用,就是为了你的系统能够识别相应的注解。
context:component-scan
1
| <context:component-scan base-package="com.**.impl"/>
|
- Spring给我们提供
<context:annotation-config>
的简化的配置方式,自动帮助你完成声明,并且还自动搜索@Component
,@Controller
,@Service
,@Repository
等标注的类。
<context:component-scan>
除了具有<context:annotation-config>
的功能之外, <context:component-scan>
还可以在指定的package下扫描以及注册javabean ,还具有自动将带有@component
,@service
,@Repository
等注解的对象注册到Spring容器中的功能。
- 因此当使用
<context:component-scan>
后,就可以将<context:annotation-config>
移除。
Spring Boot
- Spring Boot使用一个全局的配置文件,配置文件名称是固定的。
- application.properties
- application.yml
- 配置文件的作用:修改Spring Boot自动配置的默认值,因为Spring Boot在底层自动配置好了。
- 比如我们可以在配置文件中修改Tomcat 默认启动的端口号。
1 2 3 4 5 6 7
| spring: application: name:项目名。 server: port: 8080 servlet: context-path: /项目相对路径。
|
@ConfigurationProperties
- 前缀定义了哪些外部属性将绑定到类的字段上。
- 根据 Spring Boot 宽松的绑定规则,类的属性名称必须与外部属性的名称匹配。
- 类本身可以是包私有的。
- 类的字段必须有公共 setter 方法。
实例
1 2 3 4 5 6 7 8 9 10 11 12 13
| person: name: test age: 3 happy: false birth: 2000/01/01 maps: {k1: v1,k2: v2} lists: - code - girl - music dog: name:旺财。 age: 1
|
1 2 3 4 5 6 7 8 9 10 11 12
| @Component @Data @ConfigurationProperties(prefix = "person") public class Person { private String name; private Integer age; private Boolean happy; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; }
|
1 2 3 4 5 6 7 8 9
| @EnableConfiguratonProperties(Person.class) @SpringBootApplication public class AppDemoApplication {
public static void main(String[] args) { SpringApplication.run(AppDemoApplication.class, args); }
}
|
注意:使用Spring Boot Configuration Processor
提供配置文件的自动补全提示功能。
1 2 3 4 5
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
|
@PropertySource
- 加载指定的属性文件的配置到 Spring 的 Environment 中,可以配合
@Value
和@ConfigurationProperties
使用。
实例
- 在resources目录下新建一个
person.properties
文件。
1 2 3
| user.name=test user.age=18 user.sex=男。
|
- 在代码中指定加载
person.properties
文件。
1 2 3 4 5 6 7 8 9 10 11
| @Component @PropertySource(value = "classpath:person.properties") public class User { @Value("user.name") private String name; @Value("user.age") private int age; @Value("user.sex") private String sex; }
|
@ConfigurationProperties与@Value对比

@ConfigurationProperties
只需要写一次即可, @Value
则需要每个字段都添加。
- 松散绑定:不同的命名格式都是相等的,例如
last-name
,lastName
,lastname
,LAST_NAME
,last-name
是一样的。
- JSR303数据校验:可以在字段是增加一层过滤器验证,可以保证数据的合法性。
- 复杂类型封装:yaml中可以封装对象,使用
@Value
就不支持。
- 总结
- 配置yml和配置properties都可以获取到值,推荐yml
- 如果在某个业务中,只需要获取配置文件中的某个值,可以使用一下
@value
- 如果专门编写了一个JavaBean来和配置文件进行一一映射,建议使用
@configurationProperties
配置文件占位符
1 2 3 4 5 6 7 8 9 10 11 12 13
| person: name: test${random.uuid} age: ${random.int} happy: false birth: 2000/01/01 maps: {k1: v1,k2: v2} lists: - code - girl - music dog: name: ${person.hello:other}_dog age: 1
|
多环境切换
- profile是Spring对不同环境提供不同配置功能的支持,可以通过激活不同的环境版本,实现快速切换环境。
- 注意:如果yml和properties同时配置,并且没有激活其他环境,默认会使用properties文件的配置。
多配置文件
- 在主配置文件编写的时候,文件名可以是
application-{profile}.properties/yml
,用来指定多个环境版本。
application-test.properties
代表测试环境配置。
application-dev.properties
代表开发环境配置。
- 但是Spring Boot并不会直接启动这些配置文件,它默认使用
application.properties
主配置文件。
- 我们需要通过一个配置来选择需要激活的环境:
1
| spring.profiles.active=dev
|
yaml的多文档块
- 类似properties配置文件,但是使用yml去实现不需要创建多个配置文件,而是在一个配置文件中创建多个文档块。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| spring: profiles: active: prod
--- spring: config: activate: on-profile: prod server: port: 8081
--- spring: config: activate: on-profile: dev server: port: 8082
|
配置文件加载顺序
- Spring Boot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件。
- 项目路径下的config文件夹配置文件。
- 项目路径下配置文件。
- 资源路径下的config文件夹配置文件。
- 资源路径下配置文件。
- 优先级由高到底,高优先级的配置会覆盖低优先级的配置。
指定全局配置文件位置
- 通过
spring.config.location
来改变默认的全局配置文件位置。
实例
- 项目打包好以后,可以使用命令行参数的形式,启动项目的时候来指定默认的全局配置文件的新位置。
- 这种情况,相同配置,外部指定的配置文件优先级最高。
1
| java -jar spring-boot-config.jar --spring.config.location=F:/application.properties
|
自动配置原理
- Spring Boot启动会加载大量的自动配置类,需要的功能有没有在Spring Boot默认写好的自动配置类当中。
- 再来看这个自动配置类中到底配置了哪些组件,只要要用的组件存在在其中,就不需要再手动配置了。
- 给容器中自动配置类添加组件的时候,会从properties类中获取某些属性,只需要在配置文件中指定这些属性的值即可。
xxxxAutoConfiguration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| @Configuration
@EnableConfigurationProperties({HttpProperties.class})
@ConditionalOnWebApplication( type = Type.SERVLET )
@ConditionalOnClass({CharacterEncodingFilter.class})
@ConditionalOnProperty( prefix = "spring.http.encoding", value = {"enabled"}, matchIfMissing = true )
public class HttpEncodingAutoConfiguration { private final Encoding properties; public HttpEncodingAutoConfiguration(HttpProperties properties) { this.properties = properties.getEncoding(); }
@Bean @ConditionalOnMissingBean public CharacterEncodingFilter characterEncodingFilter() { CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); filter.setEncoding(this.properties.getCharset().name()); filter.setForceRequestEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.REQUEST)); filter.setForceResponseEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.RESPONSE)); return filter; } }
|
- 根据当前不同的条件判断,决定这个配置类是否生效。
- 一但这个配置类生效,这个配置类就会给容器中添加各种组件。
- 这些组件的属性是从对应的properties类中获取的,这些类里面的每一个属性又是和配置文件绑定的。
- 所有在配置文件中能配置的属性都是在xxxxProperties类中封装着。
- 配置文件能配置什么就可以参照某个功能对应的这个属性类。
xxxxProperties
1 2 3 4 5
| @ConfigurationProperties(prefix = "spring.http") public class HttpProperties { }
|