public class ConfigListern implements ApplicationListener { public void onApplicationEvent(ApplicationEvent event) { if (ContextRefreshedEvent.class.getName().equals(event.getClass().getName())) { 业务代码 } } }
实现接口ApplicationListern,重写方法,if判断是用来避免重复执行,但个人觉得并没有起到作用
但是可以通过加上状态来判断,如下文
public class ConfigListern implements ApplicationListener { public void onApplicationEvent(ApplicationEvent event) { if (ContextRefreshedEvent.class.getName().equals(event.getClass().getName())) { if(!Config.isConfig){ 业务代码 } } } }
2.在spring配置文件中加上<bean id="configListern" class="com.jiuxun.bb.listern.ConfigListern"/>