?
Spring在web应用中获得Bean的方法
-
博客分类:?
- Spring
一:使用ApplicationContext获得Bean
首先新建一个类,该类必须实现ApplicationContextAware接口,改接口有一个方法,public void setApplicationContext(ApplicationContext applicationContext)throws BeansException ,
也就是说框架会自动调用这个方法返回一个ApplicationContext对象。具体类如下:
?
- public?class?SpringContextUtils?implements?ApplicationContextAware{//Spring的工具类,用来获得配置文件中的bean??
- ??????
- ????private?static?ApplicationContext?applicationContext?=?null;??
- ??
- ????/***?
- ?????*?当继承了ApplicationContextAware类之后,那么程序在调用?
- ?????*?getBean(String)的时候会自动调用该方法,不用自己操作?
- ?????*/??
- ????public?void?setApplicationContext(ApplicationContext?applicationContext)throws?BeansException?{??
- ????????SpringContextUtils.applicationContext?=?applicationContext;??
- ????}??
- ????public?static?ApplicationContext?getApplicationContext()?{??
- ????????return?applicationContext;??
- ????}??
- ????/***?
- ?????*?根据一个bean的id获取配置文件中相应的bean?
- ?????*?@param?name?
- ?????*?@return?
- ?????*?@throws?BeansException?
- ?????*/??
- ????public?static?Object?getBean(String?name)?throws?BeansException?{??
- ????????return?applicationContext.getBean(name);??
- ????}??
- ????/***?
- ?????*?类似于getBean(String?name)只是在参数中提供了需要返回到的类型。?
- ?????*?@param?name?
- ?????*?@param?requiredType?
- ?????*?@return?
- ?????*?@throws?BeansException?
- ?????*/??
- ????public?static?Object?getBean(String?name,?Class?requiredType)?throws?BeansException?{??
- ????????return?applicationContext.getBean(name,?requiredType);??
- ????}??
- ???????????
- ??????????/**?
- ??????????*?如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true??
- ??????????*?@param?name?
- ??????????*?@return?boolean?
- ??????????*/??
- ????public?static?boolean?containsBean(String?name)?{??
- ?????????return?applicationContext.containsBean(name);??
- ????}??
- ???????????
- ??????????/**?
- ??????????*?判断以给定名字注册的bean定义是一个singleton还是一个prototype。?
- ??????????*?如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)????
- ??????????*?@param?name?
- ??????????*?@return?boolean?
- ??????????*?@throws?NoSuchBeanDefinitionException?
- ??????????*/??
- ????public?static?boolean?isSingleton(String?name)?throws?NoSuchBeanDefinitionException?{??
- ??????????return?applicationContext.isSingleton(name);??
- ????}??
- ???????????
- ??????????/**?
- ??????????*?@param?name?
- ??????????*?@return?Class?注册对象的类型?
- ??????????*?@throws?NoSuchBeanDefinitionException?
- ??????????*/??
- ????public?static?Class?getType(String?name)?throws?NoSuchBeanDefinitionException?{??
- ?????????return?applicationContext.getType(name);??
- ????}??
- ???????????
- ??????????/**?
- ??????????*?如果给定的bean名字在bean定义中有别名,则返回这些别名????
- ??????????*?@param?name?
- ??????????*?@return?
- ??????????*?@throws?NoSuchBeanDefinitionException?
- ??????????*/??
- ????public?static?String[]?getAliases(String?name)?throws?NoSuchBeanDefinitionException?{??
- ?????????return?applicationContext.getAliases(name);??
- ????}??
- }??
?
该类中有一个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。具体代码如下:
?
?
- public?class?SpringBeanFactoryUtils?implements?BeanFactoryAware?{??
- ??
- ????private?static?BeanFactory?beanFactory?=?null;??
- ????private?static?SpringBeanFactoryUtils?factoryUtils?=?null;??
- ??????
- ????public?void?setBeanFactory(BeanFactory?beanFactory)?throws?BeansException?{??
- ????????SpringBeanFactoryUtils.beanFactory?=?beanFactory;??
- ????}??
- ????public?static?BeanFactory?getBeanFactory()?{??
- ????????return?beanFactory;??
- ????}??
- ????public?static?SpringBeanFactoryUtils?getInstance(){??
- ????????if(factoryUtils==null){??
- ????????????//factoryUtils?=?(SpringBeanFactoryUtils)beanFactory.getBean("springBeanFactoryUtils");??
- ????????????factoryUtils?=?new?SpringBeanFactoryUtils();??
- ????????}??
- ????????return?factoryUtils;??
- ????}??
- ????public?static?Object?getBean(String?name){??
- ????????return?beanFactory.getBean(name);??
- ????}??
- }??
?
?
?
?
?
?
?
不过应该注意的是,改类中有一个getInstance方法,由于该代码是网上摘抄的,他提供了这么一个方法,目的是利用单例模式获得该类的一个实例,但由于getBean(String name)方法是静态的,所以用不用单例都无关紧要,经过测试,两种方法都行的通。另外一点就是必须把该类添加到applicationContext.xml的配置文件中<bean id="springBeanFactoryUtils" class="en.estar.utils.SpringBeanFactoryUtils"/>
?
三:在servlet中可以用WebApplicationContext类去获取Bean,具体做法是:
- 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包当中的一个类。
- private?static?WebApplicationContext?webApplication?=?ContextLoader.getCurrentWebApplicationContext();??
?
?用这种方法就能获取一个WebApplicationContext 的对象。
?
最后经过测试,在重复100000次的基础上,第一二中方法只用了16毫秒,而第三种方法消耗了62毫秒,所以推荐使用第一二种方法。