🎨领域:Java后端开发
🔥收录专栏: 框架
🐒个人主页:BreezAm
💖Gitee:https://gitee.com/BreezAm
✨个人标签:【后端】【大数据】【前端】【运维】
有这样一个业务场景,就是在项目运行的过程中动态修改邮箱的配置信息,目前面临的问题是项目运行以后不能动态修改邮箱发件人。
在上面的业务场景中说到不能在项目运行的过程中动态修改邮箱发件人,造成这个问题的原因是该系统的邮箱配置信息是在application.yml
中配置的,如下所示,一旦通过这种方式配置,想修改必须停掉项目,这就很麻烦,那么我们该如何解决呢?
spring:
mail:
host: smtp.163.com
username: iefox.163.com #发件人邮箱地址
password: # 密钥
properties:
mail:
smtp:
auth: true
ssl:
enable: true
starttls:
enable: true
required: true
port: 465
在spring中可以通过销毁bean和重新注册bean的方式实现邮箱配置信息的更改,从而实现动态修改发件人邮箱。下面是实现步骤。
@Component
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtil.applicationContext = applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
public static <T> T getBean(Class<T> beanClass) {
return applicationContext.getBean(beanClass);
}
public static <T> T getBean(String beanName, Class<T> beanClass) {
return applicationContext.getBean(beanName, beanClass);
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
}
@Component
@Slf4j
public class BeanUtil {
@Resource
private ApplicationContext applicationContext;
/**
* 注册bean
*
* @param property bean的属性
* @param beanName bean的名字
* @param clazz 类实例
*/
public void registerBean(Map<String, Object> property, String beanName, Class<?> clazz) {
destroyBean(beanName);//注册bean之前先销毁
ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) SpringUtil.getApplicationContext();
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();
BeanDefinitionBuilder bdb = BeanDefinitionBuilder.genericBeanDefinition(clazz);
property.forEach(bdb::addPropertyValue);
defaultListableBeanFactory.registerBeanDefinition(beanName, bdb.getBeanDefinition());
}
/**
* 销毁bean
*
* @param beanName bean的名字
*/
private void destroyBean(String beanName) {
DefaultListableBeanFactory beanFactory = getBeanFactory();
if (beanFactory.containsBeanDefinition(beanName)) {
beanFactory.destroySingleton(beanName);
beanFactory.removeBeanDefinition(beanName);
log.info("{}销毁成功", beanName);
} else {
log.info("{}销毁失败", beanName);
}
}
/**
* 获取bean工厂
*
* @return 实例
*/
private DefaultListableBeanFactory getBeanFactory() {
ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
return (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();
}
}
(1)注册bean
下面是注册bean的写法,当我们需要在项目运行过程中修改配置信息的时候,只需要写个controller方法调用修改即可。
@Autowired
private BeanUtil beanUtil;
Map<String, Object> property = new HashMap<>();
property.put("host","smtp.163.com");
property.put("port","465");
property.put("protocol","");
property.put("username","xiaoming@163.com");
property.put("password","");
beanUtil.registerBean(property,"mainBean",JavaMailSender.class);//注册bean
(2)发送邮件
JavaMailSender mailSender = (JavaMailSender) SpringUtil.getBean("mailBean");//通过名字获取bean
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("主题");
message.setFrom("xiaoming@163.com");
message.setTo("dashi@163.com");
message.setText("你好");
mailSender.send(message);
上面是一个通用的工具类,在这里只是拿邮箱发件人动态修改举例,其实在业务场景中还有很多类似于这样的场景,例如OSS对象存储服务配置信息的动态修改等等,都可以采用这种方式解决。
🔥收录专栏:框架