wordpress 网站变慢,高端网络建站,网站建设人工智能,成都网页制作公司#x1f3a8;领域#xff1a;Java后端开发#x1f525;收录专栏#xff1a; 框架 #x1f412;个人主页#xff1a;BreezAm #x1f496;Gitee#xff1a;https://gitee.com/BreezAm ✨个人标签#xff1a;【后端】【大数据】【前端】【运维】 文章目录业务场景场景分… 领域Java后端开发 收录专栏 框架 个人主页BreezAm Giteehttps://gitee.com/BreezAm ✨个人标签【后端】【大数据】【前端】【运维】 文章目录业务场景场景分析三、解决方案3.1 编写一个获取bean的工具类SpringUtil3.2 编写销毁bean和注册bean的工具类3.3 使用案例总结业务场景
有这样一个业务场景就是在项目运行的过程中动态修改邮箱的配置信息目前面临的问题是项目运行以后不能动态修改邮箱发件人。
场景分析
在上面的业务场景中说到不能在项目运行的过程中动态修改邮箱发件人造成这个问题的原因是该系统的邮箱配置信息是在application.yml中配置的如下所示一旦通过这种方式配置想修改必须停掉项目这就很麻烦那么我们该如何解决呢 spring:mail:host: smtp.163.comusername: iefox.163.com #发件人邮箱地址password: # 密钥properties:mail:smtp:auth: truessl:enable: truestarttls:enable: truerequired: trueport: 465在spring中可以通过销毁bean和重新注册bean的方式实现邮箱配置信息的更改从而实现动态修改发件人邮箱。下面是实现步骤。
三、解决方案
3.1 编写一个获取bean的工具类SpringUtil
Component
public class SpringUtil implements ApplicationContextAware {private static ApplicationContext applicationContext;Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {SpringUtil.applicationContext applicationContext;}public static Object getBean(String beanName) {return applicationContext.getBean(beanName);}public static T T getBean(ClassT beanClass) {return applicationContext.getBean(beanClass);}public static T T getBean(String beanName, ClassT beanClass) {return applicationContext.getBean(beanName, beanClass);}public static ApplicationContext getApplicationContext() {return applicationContext;}
}3.2 编写销毁bean和注册bean的工具类
Component
Slf4j
public class BeanUtil {Resourceprivate ApplicationContext applicationContext;/*** 注册bean** param property bean的属性* param beanName bean的名字* param clazz 类实例*/public void registerBean(MapString, 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();}
}3.3 使用案例
1注册bean
下面是注册bean的写法当我们需要在项目运行过程中修改配置信息的时候只需要写个controller方法调用修改即可。
Autowired
private BeanUtil beanUtil;MapString, Object property new HashMap();property.put(host,smtp.163.com);property.put(port,465);property.put(protocol,);property.put(username,xiaoming163.com);property.put(password,);beanUtil.registerBean(property,mainBean,JavaMailSender.class);//注册bean2发送邮件 JavaMailSender mailSender (JavaMailSender) SpringUtil.getBean(mailBean);//通过名字获取beanSimpleMailMessage message new SimpleMailMessage();message.setSubject(主题);message.setFrom(xiaoming163.com);message.setTo(dashi163.com);message.setText(你好);mailSender.send(message);总结
上面是一个通用的工具类在这里只是拿邮箱发件人动态修改举例其实在业务场景中还有很多类似于这样的场景例如OSS对象存储服务配置信息的动态修改等等都可以采用这种方式解决。 收录专栏框架