Spring 配置文件

本文最后更新于:2024年9月8日 晚上

Spring 配置文件

Spring

  • Spring使用一个全局的配置文件applicationContext.xml

alias

  • alias 设置别名,为bean设置别名,可以设置多个别名。
1
2
<!--设置别名:在获取Bean的时候可以使用别名获取-->
<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 方法。

实例

  • resource/application.yml
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
  • Person.java
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;
}
  • prefix:将配置文件中的prefix指定的字段下面的所有属性一一对应。

  • 除了使用@Component将配置类加载到Spring的Context中之外还可以使用@EnableConfiguratonProperties`

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使用。

实例

  1. 在resources目录下新建一个person.properties文件。
1
2
3
user.name=test
user.age=18
user.sex=男。
  1. 在代码中指定加载person.properties文件。
1
2
3
4
5
6
7
8
9
10
11
@Component
@PropertySource(value = "classpath:person.properties")
public class User {
// 直接使用@value注解注入值。
@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} # 随机uuid
age: ${random.int} # 随机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

// 启动指定类的ConfigurationProperties功能。
// 进入这个HttpProperties查看,将配置文件中对应的值和HttpProperties绑定起来,并把HttpProperties加入到ioc容器中。
@EnableConfigurationProperties({HttpProperties.class})

//Spring底层@Conditional注解。
// 根据不同的条件判断,如果满足指定的条件,整个配置类里面的配置就会生效。
// 这里的意思就是判断当前应用是否是web应用,如果是,当前配置类生效。
@ConditionalOnWebApplication(
type = Type.SERVLET
)

// 判断当前项目有没有这个类CharacterEncodingFilter(Spring MVC中进行乱码解决的过滤器)
@ConditionalOnClass({CharacterEncodingFilter.class})

// 判断配置文件中是否存在某个配置:spring.http.encoding.enabled;
// 如果不存在,判断也是成立的。
// 即使我们配置文件中不配置spring.http.encoding.enabled=true,也是默认生效的。
@ConditionalOnProperty(
prefix = "spring.http.encoding",
value = {"enabled"},
matchIfMissing = true
)

public class HttpEncodingAutoConfiguration {
// 已经和Spring Boot的配置文件映射了。
private final Encoding properties;
// 只有一个有参构造器的情况下,参数的值就会从容器中拿。
public HttpEncodingAutoConfiguration(HttpProperties properties) {
this.properties = properties.getEncoding();
}

// 给容器中添加一个组件,这个组件的某些值需要从properties中获取。
@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
// 从配置文件中获取指定的值和bean的属性进行绑定。
@ConfigurationProperties(prefix = "spring.http")
public class HttpProperties {
// .....
}