当前位置:编程学习 > 网站相关 >>

WEB项目启动加载的实现方式整理

方法一:
实现org.springframework.beans.factory.config.BeanPostProcessor接口:
[java]  
public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor {    
      
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {      
        return bean;    
    }      
      
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {      
        return bean;      
    }      
}  
在spring配置文件中添加:
[html]  
<bean class="***.***.InstantiationTracingBeanPostProcessor"/>  
 
方法二:
实现org.springframework.beans.factory.InitializingBean接口:
[java]  
public class SysInitBean implements InitializingBean, ServletContextAware {  
    public void afterPropertiesSet() throws Exception {  
    }  
  
    @Override  
    public void setServletContext(ServletContext servletContext) {  
    }  
}  
在spring配置文件中添加:
[html 
<bean class="***.***.SysInitBean"/>  
 
方法三:
实现javax.servlet.ServletContextListener:
[java]  
public class RedisInitListener implements ServletContextListener {  
  
    @Override  
    public void contextDestroyed(ServletContextEvent sce) {  
  
    }  
  
    @Override  
    public void contextInitialized(ServletContextEvent sce) {  
        //WebApplicationContext wa = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());  
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");         
    }  
}  
在web.xml中添加listener:
[html] 
<listener>    
    <listener-class>***.***.RedisInitListener</listener-class>    
</listener>   
补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,