@PropertySource注解介绍
@PropertySource 是spring的注解:
加载指定的配置文件(非application开头的文件,如admin.properties)。可以配合 @Value 和 @ConfigurationProperties 使用。
用法如下:
(1)@Configuration+ @PropertySource+Environment
(2)@Configuration+ @PropertySource+@Value
(3)@Configuration+ @PropertySource+@ConfigurationProperties
@Configuration本质也是告诉spring这是一个bean.
@PropertySources注解介绍:在Spring 4中,Spring提供了一个新的注解——@PropertySources,从名字就可以猜测到它是为多配置文件而准备的。
用法如下:
@Configuration
@PropertySources({
@PropertySource("classpath:config.properties"),
@PropertySource("classpath:db.properties")
})
public class AppConfig {
//something
}
(1)传统做法:@Configuration+ @PropertySource+Environment
@ProtertySource@PropertySouce是spring3.1开始引入的基于java config的注解。通过@PropertySource注解将properties配置文件中的值存储到Spring的 Environment中,Environment接口提供方法去读取配置文件中的值,参数是properties文件中定义的key值。
1.1例子:@Configuration+ @PropertySource+Environment
下面展示一些 hellword.properties
。
my.name=helloworld
my.age=8
下面展示一些 HelloWorldConfig.java
。
@Configuration
@PropertySource("classpath:hellword.properties")
public class HelloWorldConfig {
@Autowired
private Environment env;
}
(2)@Configuration+ @PropertySource+@Value
下面展示一些 jdbc.properties
。
jdbc.driver = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:mysql://localhost:3306/myschool?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
jdbc.username= sassy
jdbc.password = password
1.2 用法1- @PropertySource和@Value
创建java配置类
下面展示一些 内联代码片
。
@Configuration
@PropertySource("classpath:jdbc.properties")
public class PropertiesWithJavaConfig {
@Value(${jdbc.driver})
private String driver;
@Value(${jdbc.url})
private String url;
@Value(${jdbc.username})
private String username;
@Value(${jdbc.password})
private String password;
//要想使用@Value 用${}占位符注入属性,这个bean是必须的,这个就是占位bean,另一种方式是不用value直接用Envirment变量直接getProperty('key')
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
(3)现在做法:我们基本用的是 通过@PropertySource和@ConfigurationProperties来加载(读取)自定义配置文件
(1)@PropertySource指定自定义的配置文件(非application开头的配置文件)用法:
@PropertySource(“classpath:admin.properties”)
(2)@ConfigurationProperties作用:
将配置文件中配置的每一个属性的值,映射到这个组件中;
告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
参数 prefix = “person” : 将配置文件中的person下面的所有属性一一对应
下面展示一些 Admin.java
。
package com.fan.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
//其他配置资源文件的位置,@PropertySources是可以包含多个PropertySource的
@PropertySources({
@PropertySource("classpath:admin.properties"),
@PropertySource("classpath:application.properties")
})
@ConfigurationProperties(prefix = "admin")//配置资源的前缀名
@Validated
public class Admin {
@NotNull
private Integer id;
@NotBlank(message = "用户名不能为空")
@Size(min=4, max=20, message="用户名长度必须在4-20之间")
private String username;
@NotBlank(message = "密码不能为空")
@Size(min = 1,max = 20,message = "用户名长度不合法")
@NotNull
private String password;
}
下面展示 admin.properties
。
admin.id=1
admin.username="张三"
admin.password="111"
测试结果