一、启动监听ApplicationListener
spring mvc 启动监听,实现ApplicationListener<ContextRefreshedEvent>接口,然后重载 onApplicationEvent(ContextRefreshedEvent arg0);在spring配置文件中添加对应的bean即可!
public class startBeanPostProcessor implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent arg0) {
if (arg0.getApplicationContext().getParent() == null) {
System.out.println("加载初始化方法");
}
}
}
在spring中添加bean
<bean class="com.xxx.xxxx.startBeanPostProcessor "/>
或通过注解@Service注入spring中。
二、继承spring中InitializingBean类实现其中的afterPropertiesSet方法,在方法体中写相关业务即可;
@Service public class IniMQTTServer implements InitializingBean { @Override public void afterPropertiesSet() throws Exception { } }
即可。