1、实现ApplicationListener接口,并重写onApplicationEvent方法
1@Component
2public class RSAKeyInitListener implements ApplicationListener<ContextRefreshedEvent> {
3
4 @Autowired
5 BaseAppConfigDao baseAppConfigDao;
6
7 @Override
8 public void onApplicationEvent(ContextRefreshedEvent event) {
9 //具体操作
10 }
11}
2、创建spring的应用上下文(ApplicationContext.xml),并配置注解扫描
<context:component-scan base-package="com.xxx.xxx.facex.listener" />
3、配置web.xml
1<listener>
2 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
3 </listener><!-- 也可以与context-param标签一起使用 -->
4
5 <context-param>
6 <param-name>contextConfigLocation</param-name> <param-value> classpath:spring.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>