`

一:使用ApplicationContext获得Bean 首先新建一个类,该类必须实现ApplicationContextAware接口,改接口有一个方法,p

 
阅读更多

一:使用ApplicationContext获得Bean

首先新建一个类,该类必须实现ApplicationContextAware接口,改接口有一个方法,public void setApplicationContext(ApplicationContext applicationContext)throws BeansException ,

也就是说框架会自动调用这个方法返回一个ApplicationContext对象。具体类如下:

 

Java代码  收藏代码
  1. public   class  SpringContextUtils  implements  ApplicationContextAware{ //Spring的工具类,用来获得配置文件中的bean   
  2.       
  3.     private   static  ApplicationContext applicationContext =  null ;  
  4.   
  5.     /***  
  6.      * 当继承了ApplicationContextAware类之后,那么程序在调用  
  7.      * getBean(String)的时候会自动调用该方法,不用自己操作  
  8.      */   
  9.     public   void  setApplicationContext(ApplicationContext applicationContext) throws  BeansException {  
  10.         SpringContextUtils.applicationContext = applicationContext;  
  11.     }  
  12.     public   static  ApplicationContext getApplicationContext() {  
  13.         return  applicationContext;  
  14.     }  
  15.     /***  
  16.      * 根据一个bean的id获取配置文件中相应的bean  
  17.      * @param name  
  18.      * @return  
  19.      * @throws BeansException  
  20.      */   
  21.     public   static  Object getBean(String name)  throws  BeansException {  
  22.         return  applicationContext.getBean(name);  
  23.     }  
  24.     /***  
  25.      * 类似于getBean(String name)只是在参数中提供了需要返回到的类型。  
  26.      * @param name  
  27.      * @param requiredType  
  28.      * @return  
  29.      * @throws BeansException  
  30.      */   
  31.     public   static  Object getBean(String name, Class requiredType)  throws  BeansException {  
  32.         return  applicationContext.getBean(name, requiredType);  
  33.     }  
  34.            
  35.           /**  
  36.           * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true   
  37.           * @param name  
  38.           * @return boolean  
  39.           */   
  40.     public   static   boolean  containsBean(String name) {  
  41.          return  applicationContext.containsBean(name);  
  42.     }  
  43.            
  44.           /**  
  45.           * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。  
  46.           * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)     
  47.           * @param name  
  48.           * @return boolean  
  49.           * @throws NoSuchBeanDefinitionException  
  50.           */   
  51.     public   static   boolean  isSingleton(String name)  throws  NoSuchBeanDefinitionException {  
  52.           return  applicationContext.isSingleton(name);  
  53.     }  
  54.            
  55.           /**  
  56.           * @param name  
  57.           * @return Class 注册对象的类型  
  58.           * @throws NoSuchBeanDefinitionException  
  59.           */   
  60.     public   static  Class getType(String name)  throws  NoSuchBeanDefinitionException {  
  61.          return  applicationContext.getType(name);  
  62.     }  
  63.            
  64.           /**  
  65.           * 如果给定的bean名字在bean定义中有别名,则返回这些别名     
  66.           * @param name  
  67.           * @return  
  68.           * @throws NoSuchBeanDefinitionException  
  69.           */   
  70.     public   static  String[] getAliases(String name)  throws  NoSuchBeanDefinitionException {  
  71.          return  applicationContext.getAliases(name);  
  72.     }  
  73. }  

 

该类中有一个getBean(String name)方法,该方法用applicationContext去获得Bean实例,而applicationContext

是由系统启动时自动设置的。注意,在applicationContext.xml配置文件中需要把该类加上。<bean id="springUtils" class="en.estar.utils.SpringContextUtils"/>

 

二:通过BeanFactory接口获得Bean

      也是新建一个类,不过该类需要实现BeanFactoryAware接口,该接口有一个方法public void setBeanFactory(BeanFactory beanFactory) throws BeansException;该方法是为了设置BeanFactory对象,系统会在启动的时候自动设置BeanFactory。具体代码如下:

 

Java代码  收藏代码
  1. public   class  SpringBeanFactoryUtils  implements  BeanFactoryAware {  
  2.   
  3.     private   static  BeanFactory beanFactory =  null ;  
  4.     private   static  SpringBeanFactoryUtils factoryUtils =  null ;  
  5.       
  6.     public   void  setBeanFactory(BeanFactory beanFactory)  throws  BeansException {  
  7.         SpringBeanFactoryUtils.beanFactory = beanFactory;  
  8.     }  
  9.     public   static  BeanFactory getBeanFactory() {  
  10.         return  beanFactory;  
  11.     }  
  12.     public   static  SpringBeanFactoryUtils getInstance(){  
  13.         if (factoryUtils== null ){  
  14.             //factoryUtils = (SpringBeanFactoryUtils)beanFactory.getBean("springBeanFactoryUtils");   
  15.             factoryUtils = new  SpringBeanFactoryUtils();  
  16.         }  
  17.         return  factoryUtils;  
  18.     }  
  19.     public   static  Object getBean(String name){  
  20.         return  beanFactory.getBean(name);  
  21.     }  
  22. }  

 

 

 

 

 

不过应该注意的是,改类中有一个getInstance方法,由于该代码是网上摘抄的,他提供了这么一个方法,目的是利用单例模式获得 该 类的一个实例,但由于getBean(String name)方法是静态的,所以用不用单例都无关紧要,经过测试,两种方法都行的通。另外一点就是必须把该类添加到 applicationContext.xml的配置文件中<bean id="springBeanFactoryUtils" class="en.estar.utils.SpringBeanFactoryUtils"/>

 

三:在servlet中可以用WebApplicationContext类去获取Bean,具体做法是:

Java代码  收藏代码
  1. WebApplicationContext webcontext = (WebApplicationContext)context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);  

 

 其中 context 是servlet的上下文,在servlet中可以通过this.getServletContext()或者 request.getSession().getServletContext();获得servletContext。但是一点,spring的配置 文件必须在WEB-INF下。 WebApplicationContext 有一个方法getBean(String name);其实WebApplicationContext 就是ApplicationContext的另一种形式而已。

       另外,在普通的java类中,即不是在servlet中可以用ContextLoader获得。ContextLoader是org.springframework.web.context包当中的一个类。

Java代码  收藏代码
  1. private   static  WebApplicationContext webApplication = ContextLoader.getCurrentWebApplicationContext();  

 

 用这种方法就能获取一个WebApplicationContext 的对象。

 

最后经过测试,在重复100000次的基础上,第一二中方法只用了16毫秒,而第三种方法消耗了62毫秒,所以推荐使用第一二种方法

尊重版主,原文地址:http://littie1987.iteye.com/blog/937877

分享到:
评论
1 楼 babyhhcsy 2014-10-27  
spring产生的对象就是单例的,楼主为什么还有讲util工具设置成单例模式呢?

相关推荐

Global site tag (gtag.js) - Google Analytics