Spring启动后执行

方法一:

实现BeanPostProcessor接口:

[java] view plaincopy
 
  1. public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor {  
  2.     
  3.     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {    
  4.         return bean;  
  5.     }    
  6.     
  7.     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {    
  8.         return bean;    
  9.     }    
  10. }   

在配置文件中添加:

[html] view plaincopy
 
  1. <bean class="processor.InstantiationTracingBeanPostProcessor"/>  

方法二:

实现InitializingBean接口:

[java] view plaincopy
 
  1. public class SysInitBean implements InitializingBean, ServletContextAware {  
  2.     public void afterPropertiesSet() throws Exception {  
  3.     }  
  4.   
  5.     @Override  
  6.     public void setServletContext(ServletContext servletContext) {  
  7.     }  
  8. }  

在配置文件中添加:

[html] view plaincopy
 
  1. <bean class="processor.SysInitBean"/>  

方法三:

实现ServletContextListener:

[java] view plaincopy
 
  1. public class RedisInitListener implements ServletContextListener {  
  2.   
  3.     @Override  
  4.     public void contextDestroyed(ServletContextEvent sce) {  
  5.   
  6.     }  
  7.   
  8.     @Override  
  9.     public void contextInitialized(ServletContextEvent sce) {  
  10.         //WebApplicationContext wa = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());  
  11.         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");         
  12.     }  
  13. }  

在web.xml中添加listener:

[html] view plaincopy
 
    1. <listener>  
    2.     <listener-class>listener.RedisInitListener</listener-class>  
    3. </listener>  
原文地址:https://www.cnblogs.com/littleCode/p/4554107.html