通过监听器方式监听配置是否发生变化,发生变化后进行更新(创建事件处理器->发布事件->监听事件)
-
创建配置变更事件类
点击查看代码
import org.springframework.context.ApplicationEvent;
import java.util.Properties;
public class PropertiesChangeEven extends ApplicationEvent {
private Properties properties;
private Properties oldProperties;
private Properties newProperties;
public PropertiesChangeEven(Properties source) {
super(source);
this.properties = source;
}
@Override
public Properties getSource() {
return properties;
}
public Properties getOldProperties() {
return oldProperties;
}
public void setOldProperties(Properties oldProperties) {
this.oldProperties = oldProperties;
}
public Properties getNewProperties() {
return newProperties;
}
public void setNewProperties(Properties newProperties) {
this.newProperties = newProperties;
}
}
-
创建配置变更处理
点击查看代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Service;
import java.util.Enumeration;
import java.util.Properties;
@Slf4j
@Service
public class PropertiesChangeHandle {
@Autowired
private ConfigurableEnvironment configurableEnvironment;
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public void changeProperties(Properties properties){
if (properties == null){
return;
}
log.info("通过使用@EventListener注解或实现ApplicationListener接口监听配置变更");
Properties oldProperties = new Properties();
Properties newProperties = new Properties();
Enumeration enumeration = properties.propertyNames();
while(enumeration.hasMoreElements()){
String keyName = (String) enumeration.nextElement();
Object oldValue = configurableEnvironment.getProperty(keyName);
Object newValue = properties.getProperty(keyName);
if (oldValue != null){
oldProperties.put(keyName, oldValue);
}
if (newValue != null && !newValue.equals(oldValue)){
newProperties.put(keyName, newValue);
}
}
PropertiesChangeEven propertiesChangeEven = new PropertiesChangeEven(properties);
propertiesChangeEven.setOldProperties(oldProperties);
propertiesChangeEven.setNewProperties(newProperties);
applicationEventPublisher.publishEvent(propertiesChangeEven);
}
}
-
监听配置变更事件,实现ApplicationListener接口或实现@EventListener注解
点击查看代码
import com.crazy.practice.boot.even.PropertiesChangeEven;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.stereotype.Component;
import java.util.Map;
@Slf4j
@Component
public class PropertiesChangeListener implements ApplicationListener<PropertiesChangeEven>, ApplicationContextAware {
private ApplicationContext applicationContext;
@Autowired(required = false)
private ContextRefresher contextRefresher;
@Override
public void onApplicationEvent(PropertiesChangeEven propertiesChangeEven) {
log.info("通过实现ApplicationListener.onApplicationEvent监听配置变更");
Map propertiesMap = propertiesChangeEven.getNewProperties();
ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) applicationContext.getEnvironment();
MutablePropertySources mutablePropertySources = configurableEnvironment.getPropertySources();
mutablePropertySources.addFirst(new MapPropertySource("refreshProperties", propertiesMap));
contextRefresher.refresh();
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
或
点击查看代码
import com.crazy.practice.boot.even.PropertiesChangeEven;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class PropertiesChangeListener2 {
@EventListener(PropertiesChangeEven.class)
public void propertiesListener(PropertiesChangeEven propertiesChangeEven){
log.info("通过实现@EventListener注解监听配置变更");
log.info("变更配置:{}", propertiesChangeEven.getSource());
log.info("旧配置:{}", propertiesChangeEven.getOldProperties());
log.info("新配置:{}", propertiesChangeEven.getNewProperties());
}
}
-
发布事件,在需要动态刷新配置的类上加上@RefreshScope
点击查看代码
@Autowired
private PropertiesChangeHandle propertiesChangeHandle;
@GetMapping("/updateProperties")
public String updateProperties(@RequestParam(value = "key")String key, @RequestParam(value = "value")String value, @RequestParam(value = "comments", required = false)String comments){
Map map = new HashMap();
map.put(key, value);
Properties properties = new Properties();
properties.put(key, value);
propertiesChangeHandle.changeProperties(properties);
return value;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)