自定义监听器 创立BeanITeye - 千亿集团

自定义监听器 创立BeanITeye

2019-01-12 19:00:13 | 作者: 笑天 | 标签: 调用,这样,能够 | 浏览: 2204

 listener 
 listener-class com.listener.TestApplicationContextLoaderListener /listener-class 
 /listener 

 

public class TestApplicationContextLoaderListener implements ServletContextListener{
 @Override
 public void contextDestroyed(ServletContextEvent servletcontextevent) {
 System.out.println("调用:TestApplicationContextLoaderListener11");
 TestApplicationContext.initTspApplicationContext(WebApplicationContextUtils.getRequiredWebApplicationContext(servletcontextevent.getServletContext()));
 @Override
 public void contextInitialized(ServletContextEvent servletcontextevent) {
 System.out.println("调用:TestApplicationContextLoaderListener22");
 //TestApplicationContext.initTspApplicationContext(WebApplicationContextUtils.getRequiredWebApplicationContext(servletcontextevent.getServletContext()));
 //或这样也能够
 ApplicationContext factory = new ClassPathXmlApplicationContext("classpath*:applicationContext-*.xml");
 TestApplicationContext.initTspApplicationContext(factory);
}

 

import org.springframework.context.ApplicationContext;
public class TestApplicationContext {
 private static ApplicationContext _applicationContext;
 public static void initTspApplicationContext(
 ApplicationContext applicationContext) {
 _applicationContext = applicationContext;
 public static Object getBean(String beanid) {
 Object bean = null;
 if (null == _applicationContext) {
 throw new RuntimeException("使用上下文未正常初始化。");
 try {
 bean = _applicationContext.getBean(beanid);
 } catch (Exception e) {
 e.printStackTrace();
 return bean;
}

 

 @RequestMapping(value = "/getBean.do", method = RequestMethod.GET )
 public void getBean(HttpServletRequest request){
 //获取类的姓名
 String objName = this.getClass().getName();
 //回来bean目标引证
 TestBean cacheManager = (TestBean)TestApplicationContext.getBean("testBean");
 cacheManager.refBean();
 }

 

 bean id="testBean" 
 /bean 

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表千亿集团立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章