问题
- @value 的用法
- @value 数据来源
- @value 动态刷新
@Value使用步骤
步骤一:使用@PropertySource注解引入配置文件
@Component
@PropertySource({"classpath:com/javacode2018/lesson002/demo18/db.properties"})
public class DbConfig {
}
步骤二:使用@Value注解引用配置文件的值
@Value("${配置文件中的key:默认值}")
@Value("${配置文件中的key}")
案例
来个配置文件db.properties:
jdbc.url=jdbc:mysql://localhost:3306/javacode2018?characterEncoding=UTF-8
jdbc.username=javacode
jdbc.password=javacode
来个配置类,使用@PropertySource引入上面的配置文件
@Configurable
@ComponentScan
@PropertySource({"classpath:db.properties"})
public class MainConfig1 {
}
来个类,使用@Value来使用配置文件中的信息
@Component
public class DbConfig {
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "DbConfig{" +
"url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
测试:
@Test
public void test1() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(MainConfig1.class);
context.refresh();
DbConfig dbConfig = context.getBean(DbConfig.class);
System.out.println(dbConfig);
}
运行结果:
DbConfig{url='jdbc:mysql://localhost:3306/javacode2018?characterEncoding=UTF-8', username='javacode', password='javacode'}
上面用起来比较简单,很多用过的人看一眼就懂了,这也是第一个问题,多数人都是ok的,下面来看@Value中数据来源除了配置文件的方式,是否还有其他方式。
@Value数据来源
先了解一下@Value中数据来源于spring的什么地方。
spring中有个类:org.springframework.core.env.PropertySource
:
可以将其理解为一个配置源,里面包含了key->value的配置信息,可以通过这个类中提供的方法获取key对应的value信息
内部有个方法:public abstract Object getProperty(String name);
:
通过name获取对应的配置信息。
有个比较重要的接口:org.springframework.core.env.Environment
:用来表示环境配置信息
// 这个接口有几个方法比较重要
//resolvePlaceholders用来解析${text}的,@Value注解最后就是调用这个方法来解析的。
String resolvePlaceholders(String text);
//getPropertySources返回MutablePropertySources对象
MutablePropertySources getPropertySources();
public class MutablePropertySources implements PropertySources {
//内部包含一个propertySourceList列表。
private final List<PropertySource<?>> propertySourceList = new CopyOnWriteArrayList<>();
}
解析@value的过程:
- 将@value注解的value参数值作为Environment.resolvePlaceholders方法参数进行解析
- Environment内部会访问MutablePropertySources来解析
- MutablePropertySources内部有多个PropertySource,此时会遍历PropertySource列表,调用PropertySource.getProperty方法来解析key对应的值
因此,如果我们想改变@value数据的来源,只需要将配置信息包装为PropertySource对象,丢到Environment中的MutablePropertySources内部就可以了。
案例
@Component
public class MailConfig {
@Value("${mail.host}")
private String host;
@Value("${mail.username}")
private String username;
@Value("${mail.password}")
private String password;
//get set方法,以及toString省略。。
}
public class DbUtil {
/**
* 模拟从db中获取邮件配置信息
*
* @return
*/
public static Map<String, Object> getMailInfoFromDb() {
Map<String, Object> result = new HashMap<>();
result.put("mail.host", "smtp.qq.com");
result.put("mail.username", "路人");
result.put("mail.password", "123");
return result;
}
}
来个测试:
@Test
public void getValue(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//把map包装为MapPropertySource,其是PropertySource的子类
MapPropertySource mapPropertySource = new MapPropertySource("mail", DbUtil.getMailInfoFromDb());
//获取getEnvironment信息,然后把MapPropertySource放到MutablePropertySources#propertySourceList中
context.getEnvironment().getPropertySources().addFirst(mapPropertySource);
//注册,刷新,获取这个bean
context.register(com.spring.value.MainConfig2.class);
context.refresh();
MailConfig mailConfig = context.getBean(MailConfig.class);
System.out.println(mailConfig);
}
结果:
MailConfig{host='smtp.qq.com', username='路人', password='123'}
实现@value动态刷新
在bean的作用域中,看一下@Scope这个注解的源码,有个参数:
ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT;
这个参数的值有下面4种:
public enum ScopedProxyMode {
DEFAULT,
NO,
INTERFACES,
TARGET_CLASS;
}
当@Scope中proxyMode为TARGET_CLASS的时候,会给当前创建的bean通过cglib生成一个代理对象,通过这个代理对象来访问目标bean对象。
自定义Scope案例
自定义一个bean作用域的注解
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
//使用了@Scope注解,value为引用了一个常量,值为my,一会下面可以看到。
@Scope(BeanMyScope.SCOPE_MY)
public @interface MyScope {
/**
* @see Scope#proxyMode()
*/
ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS;
}
@MyScope 注解对应的Scope实现如下:
/**
* @see MyScope 作用域的实现
*/
public class BeanMyScope implements Scope {
//定义了一个常量,作为作用域的值
public static final String SCOPE_MY = "my";
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
//这个get方法是关键,自定义作用域会自动调用这个get方法来创建bean对象
System.out.println("BeanMyScope >>>>>>>>> get:" + name);
//通过objectFactory.getObject()获取bean实例返回
return objectFactory.getObject();
}
@Nullable
@Override
public Object remove(String name) {
return null;
}
@Override
public void registerDestructionCallback(String name, Runnable callback) {
}
@Nullable
@Override
public Object resolveContextualObject(String key) {
return null;
}
@Nullable
@Override
public String getConversationId() {
return null;
}
}
下面来创建个类,作用域为上面自定义的作用域:
@Component
@MyScope //@1
public class User {
private String username;
public User() {
System.out.println("---------创建User对象" + this);
this.username = UUID.randomUUID().toString();
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
来个spring配置类,加载上面@Compontent标注的组件:
@ComponentScan
@Configuration
public class MainConfig3 {
}
测试用例:
@Test
public void test3() throws InterruptedException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
//将自定义作用域注册到spring容器中
context.getBeanFactory().registerScope(BeanMyScope.SCOPE_MY, new BeanMyScope());
context.register(MainConfig3.class);
context.refresh();
System.out.println("从容器中获取User对象");
User user = context.getBean(User.class);
System.out.println("user对象的class为:" + user.getClass());
System.out.println("多次调用user的getUsername感受一下效果\n");
for (int i = 1; i <= 3; i++) {
System.out.println(String.format("********\n第%d次开始调用getUsername", i));
System.out.println(user.getUsername());
System.out.println(String.format("第%d次调用getUsername结束\n********\n", i));
}
}
运行输出:
从容器中获取User对象
user对象的class为:class com.javacode2018.lesson002.demo18.test3.User$$EnhancerBySpringCGLIB$$80233127
多次调用user的getUsername感受一下效果
********
第1次开始调用getUsername
BeanMyScope >>>>>>>>> get:scopedTarget.user
---------创建User对象com.javacode2018.lesson002.demo18.test3.User@6a370f4
7b41aa80-7569-4072-9d40-ec9bfb92f438
第1次调用getUsername结束
********
********
第2次开始调用getUsername
BeanMyScope >>>>>>>>> get:scopedTarget.user
---------创建User对象com.javacode2018.lesson002.demo18.test3.User@1613674b
01d67154-95f6-44bb-93ab-05a34abdf51f
第2次调用getUsername结束
********
********
第3次开始调用getUsername
BeanMyScope >>>>>>>>> get:scopedTarget.user
---------创建User对象com.javacode2018.lesson002.demo18.test3.User@27ff5d15
76d0e86f-8331-4303-aac7-4acce0b258b8
第3次调用getUsername结束
********
从输出的前2行可以看出:
- 调用context.getBean(User.class)从容器中获取bean的时候,此时并没有调用User的构造函数去创建User对象
- 第二行输出的类型可以看出,getBean返回的user对象是一个cglib代理对象。
通过上面的案例可以看出,当自定义的Scope中proxyMode=ScopedProxyMode.TARGET_CLASS的时候,会给这个bean创建一个代理对象,调用代理对象的任何方法,都会调用这个自定义的作用域实现类(上面的BeanMyScope)中get方法来重新来获取这个bean对象。
动态刷新@Value具体实现
那么我们可以利用上面讲解的这种特性来实现@Value的动态刷新,可以实现一个自定义的Scope,这个自定义的Scope支持@Value注解自动刷新,需要使用@Value注解自动刷新的类上面可以标注这个自定义的注解,当配置修改的时候,调用这些bean的任意方法的时候,就让spring重启初始化一下这个bean,这个思路就可以实现了,下面我们来写代码。
先来自定义一个Scope:RefreshScope
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Scope(BeanRefreshScope.SCOPE_REFRESH)
@Documented
public @interface RefreshScope {
ScopedProxyMode proxyMode() default ScopedProxyMode.TARGET_CLASS;
}
自定义Scope对应的解析类
public class BeanRefreshScope implements Scope {
public static final String SCOPE_REFRESH = "refresh";
private static final BeanRefreshScope INSTANCE = new BeanRefreshScope();
//来个map用来缓存bean
private ConcurrentHashMap<String, Object> beanMap = new ConcurrentHashMap<>(); //@1
private BeanRefreshScope() {
}
public static BeanRefreshScope getInstance() {
return INSTANCE;
}
/**
* 清理当前
*/
public static void clean() {
INSTANCE.beanMap.clear();
}
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
Object bean = beanMap.get(name);
if (bean == null) {
bean = objectFactory.getObject();
beanMap.put(name, bean);
}
return bean;
}
}
来个邮件配置类,使用@Value注解注入配置,这个bean作用域为自定义的@RefreshScope
@Component
@RefreshScope
public class MailConfig {
@Value("${mail.username}")
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "MailConfig{" +
"username='" + username + '\'' +
'}';
}
}
再来个普通的bean,内部会注入MailConfig
@Component
public class MailService {
@Autowired
private MailConfig mailConfig;
@Override
public String toString() {
return "MailService{" +
"mailConfig=" + mailConfig +
'}';
}
}
来个类,用来从db中获取邮件配置信息
public class DbUtil {
/**
* 模拟从db中获取邮件配置信息
*
* @return
*/
public static Map<String, Object> getMailInfoFromDb() {
Map<String, Object> result = new HashMap<>();
result.put("mail.username", UUID.randomUUID().toString());
return result;
}
}
来个spring配置类,扫描加载上面的组件
@Configuration
@ComponentScan
public class MainConfig4 {
}
来个工具类
public class RefreshConfigUtil {
/**
* 模拟改变数据库中都配置信息
*/
public static void updateDbConfig(AbstractApplicationContext context) {
//更新context中的mailPropertySource配置信息
refreshMailPropertySource(context);
//清空BeanRefreshScope中所有bean的缓存
BeanRefreshScope.getInstance().clean();
}
public static void refreshMailPropertySource(AbstractApplicationContext context) {
Map<String, Object> mailInfoFromDb = DbUtil.getMailInfoFromDb();
//将其丢在MapPropertySource中(MapPropertySource类是spring提供的一个类,是PropertySource的子类)
MapPropertySource mailPropertySource = new MapPropertySource("mail", mailInfoFromDb);
context.getEnvironment().getPropertySources().addFirst(mailPropertySource);
}
}
来个测试用例
@Test
public void test4() throws InterruptedException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getBeanFactory().registerScope(BeanRefreshScope.SCOPE_REFRESH, BeanRefreshScope.getInstance());
context.register(MainConfig4.class);
//刷新mail的配置到Environment
RefreshConfigUtil.refreshMailPropertySource(context);
context.refresh();
MailService mailService = context.getBean(MailService.class);
System.out.println("配置未更新的情况下,输出3次");
for (int i = 0; i < 3; i++) {
System.out.println(mailService);
TimeUnit.MILLISECONDS.sleep(200);
}
System.out.println("模拟3次更新配置效果");
for (int i = 0; i < 3; i++) {
RefreshConfigUtil.updateDbConfig(context);
System.out.println(mailService);
TimeUnit.MILLISECONDS.sleep(200);
}
}
来看效果:
配置未更新的情况下,输出3次
MailService{mailConfig=MailConfig{username='df321543-8ca7-4563-993a-bd64cbf50d53'}}
MailService{mailConfig=MailConfig{username='df321543-8ca7-4563-993a-bd64cbf50d53'}}
MailService{mailConfig=MailConfig{username='df321543-8ca7-4563-993a-bd64cbf50d53'}}
模拟3次更新配置效果
MailService{mailConfig=MailConfig{username='6bab8cea-9f4f-497d-a23a-92f15d0d6e34'}}
MailService{mailConfig=MailConfig{username='581bf395-f6b8-4b87-84e6-83d3c7342ca2'}}
MailService{mailConfig=MailConfig{username='db337f54-20b0-4726-9e55-328530af6999'}}