基于@ConfigurationProperties的属性注入

1、在pom.xml中添加依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>

 

2、创建一个Spring Boot项目。在resources文件夹下创建application.yml配置文件,并在配置文件中填写配置信息,如下图:

SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 

3、在domain包下创建实体类Student,并在类的上方加入注解 @ConfigurationProperties(prefix="student")

注意实体类的属性要与对应的配置文件属性一致。

SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 

 4、测试。在控制类中注入Student,并通过方法进行调用

SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 

基于@Value的属性注入

1、将上面的Student实体类的 @ConfigurationProperties(prefix="student")去掉,在属性上方加上@Value("${student.xxx}")

SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 

2、测试。

SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 

@PropertySource注解指定自定义配置文件

1、在resources文件夹下新建自定义配置文件teacher.properties(PropertySource注解不支持yml文件加载)。

SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 

2、新建实体类Teacher,如下图所示:

注意:当配置文件中存在中文时,为了防止乱码需要在PropertySource注解中加入encoding参数。

SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 

3、测试。在控制类中注入Teacher ,并通过方法进行调用

SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 

@profile多环境配置

 1、在config文件夹中,创建DBConnector接口、DevDBConnector类、ProdDBConnector类

SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 

 2、在application.yml配置文件中添加spring: profiles: active: dev ,指明当前的配置环境

SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 

 3、在测试类DemoApplicationTests中测试多环境配置是否生效

SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 

 4、运行testConnect测试方法,查看运行结果

SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 

 5、将配置文件中的dev改为prod,并把装配的DevDBConnector改成ProdDBConnector,运行textConnect方法

SpringBoot--- #属性注入@ConfigurationProperties @Value(在配置文件中写属性的值,在类中引用该值)#自定义配置文件@PropertySource  #多环境配置@profile

 

相关文章: