ApplicationContextAware的作用
ApplicationContextAware的作用:(1):通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法,可以通过这个上下文环境对象得到Spring容器中的Bean(2):实现代码如下:/*** Spring容器上下文环境Holder* **/@Componentpub...
ApplicationContextAware的作用:
(1):通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法,可以通过这个上下文环境对象得到Spring容器中的Bean
(2):实现代码如下:
/**
* Spring容器上下文环境Holder
* **/
@Component
public class SpringContentHolder implements ApplicationContextAware{
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
if(SpringContentHolder.applicationContext==null){
SpringContentHolder.applicationContext=applicationContext;
}
}
/**
* 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
*/
public static <T> T getBean(Class<T> requiredType) {
return getApplicationContext().getBean(requiredType);
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
}
(5):ServletContextListener接口作用:
ServletContextListener是对ServeltContext的一个监听.servelt容器启动,serveltContextListener就会调用contextInitialized方法.在方法里面调用event.getServletContext()可以获取ServletContext,ServeltContext是一个上下文对象,他的数据供所有的应用程序共享,进行一些业务的初始化.servelt容器关闭,serveltContextListener就会调用contextDestroyed.
实际上ServeltContextListener是生成ServeltContext对象之后调用的.生成ServeltContext对象之后,这些代码在我们业务实现之前就写好,它怎么知道我们生成类的名字.实际上它并不需要知道我们的类名,类里面有是方法.他们提供一个规范,就一个接口,ServeltContextListner,只要继承这个接口就必须实现这个方法.然后这个类在web.xml中Listener节点配置好.Servelt容器先解析web.xml,获取Listener的值.通过反射生成对象放进缓存.然后创建ServeltContext对象和ServletContextEvent对象.然后在调用ServletContextListener的contextInitialized方法,然后方法可以把用户的业务需求写进去.struts和spring框架就是类似这样的实现,我们以后写一些框架也可以在用
(4):使用ApplicationContextAware接口和ServletContextListener实现项目业务Servlet容器初始化
@Component
public class InitAction implements ServletContextListener,ApplicationContextAware{
private static ApplicationContext applicationContext;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
// TODO Auto-generated method stub
ServletContext application=servletContextEvent.getServletContext();
ProductBigTypeService productBigTypeService=(ProductBigTypeService)applicationContext.getBean("productBigTypeService");
List<ProductBigType> bigTypeList=productBigTypeService.findAllBigTypeList();
application.setAttribute("bigTypeList", bigTypeList);
TagService tagService=(TagService)applicationContext.getBean("tagService");
List<Tag> tagList=tagService.findTagList(null,null);
application.setAttribute("tagList", tagList);
NoticeService noticeService=(NoticeService)applicationContext.getBean("noticeService");
List<Notice> noticeList=noticeService.findNoticeList(null, new PageBean(1,7));
application.setAttribute("noticeList", noticeList);
NewsService newsService=(NewsService)applicationContext.getBean("newsService");
List<News> newsList=newsService.findNewsList(null, new PageBean(1,7));
application.setAttribute("newsList", newsList);
ProductService productService=(ProductService)applicationContext.getBean("productService");
Product s_product=new Product();
s_product.setSpecialPrice(1);
List<Product> specialPriceProductList=productService.findProductList(s_product, new PageBean(1,8));
application.setAttribute("specialPriceProductList", specialPriceProductList);
s_product=new Product();
s_product.setHot(1);
List<Product> hotProductList=productService.findProductList(s_product, new PageBean(1,6));
application.setAttribute("hotProductList", hotProductList);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
// TODO Auto-generated method stub
this.applicationContext=applicationContext;
}
}
更多推荐
所有评论(0)