Spring啓動後執行

方法一:html

實現BeanPostProcessor接口:java

 

[java]  view plain copy
 
  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. }   

在配置文件中添加:web

 

 

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

 

方法二:app

實現InitializingBean接口:
ide

 

[java]  view plain copy
 
  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. }  

在配置文件中添加:post

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

方法三:spa

實現ServletContextListener:.net

 

[java]  view plain copy
 
  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:xml

 

 

[html]  view plain copy
 
    1. <listener>  
    2.     <listener-class>listener.RedisInitListener</listener-class>  
    3. </listener>  
相關文章
相關標籤/搜索