Spring中获取bean的方式总结

众所周知,Spring框架将DI模式发挥到了极至,因此,系统里面用Spring管理的Bean相互之间的获取是非常方便的,只要使用者提供一个setter方法并在配置文件中配置该属性就可以。

但是,对于系统中非Spring框架管理的类,如果需要获取Spring管理的类,或者,程序中需要动态的根据Beanid来获取Bean实例,不可能事先为该类提供所有需要的Bean属性的setter方法,在类似这样的情况下,获取Spring框架管理的类实例的方法有多种,现在简单总结如下:

 

方法一:在初始化时保存ApplicationContext对象

ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
 
  ac.getBean("beanId"); 
    这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。 
方法二:通过Spring提供的工具类获取ApplicationContext对象 
 import org.springframework.web.context.support.WebApplicationContextUtils;

 
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc) 
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);ac1.getBean("beanId");
ac2.getBean("beanId"); 
这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。
这个类提供了方便的功能,这样你就不必去记 ServletContext 中属性的名字。 它的 getWebApplicationContext() 方法在 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 这个键值不对应任何对象的时候将返回 null。不过,为了避免在应用中得到 NullPointerExceptions ,我们推荐你使用 getRequiredWebApplicationContext() 方法。这个方法在ApplicationContext 缺失的时候会抛出一个异常。 
上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。 
  方法三:继承自抽象类ApplicationObjectSupport 
抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。
 

方法四:继承自抽象类WebApplicationObjectSupport 类似上面方法,调用getWebApplicationContext()获取WebApplicationContext 方法五:实现接口ApplicationContextAware 实现该接口的 setApplicationContext( ApplicationContext context)方法,并保存 ApplicationContext 对象。Spring初始化时,会通过该方法将 ApplicationContext 对象注入。
我自己是在web.xml中配置,然后用一个监听器调用一个类直接读取,在tomcat启动时执行
web.xml
<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/config/spring/applicationContext.xml</param-value>
 </context-param>
<listener>
  <listener-class>com.wzw.listener.SpringListener</listener-class>
 </listener>
SpringListener package com.wzw.listener;
 import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.wzw.spring.common.SpringBeanFactory;
 public class SpringListener implements ServletContextListener {
  public void contextInitialized(ServletContextEvent sce) {
  // TODO Auto-generated method stub
String relativePath = sce.getServletContext().getInitParameter(
"contextConfigLocation");
String realPath = sce.getServletContext().getRealPath(relativePath); 
SpringBeanFactory.init(realPath);
 }
 
 public void contextDestroyed(ServletContextEvent arg0) {
  // TODO Auto-generated method stub
 SpringBeanFactory.clear();
}

}
监听器调用的类

package com.wzw.spring.common;

import java.util.Locale;
import org.apache.struts.action.ActionMessage;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.ApplicationContext;


public class SpringBeanFactory {
 
  private static ApplicationContext context;


   /**
    * 在应用程序启动时配置spring框架
    *
    * @param filePath
    */
   public static void init(String filePath) {

    if (context == null) {
       context = new FileSystemXmlApplicationContext(filePath);
    }
  }
   public static ApplicationContext getContext(){
    return context;
   }

   /**
    * 方法用于获取业务对象。
    *
    * @param beanName
    * @return
    */
   public static Object getBusinessOjbect(String beanName) {
    return context.getBean(beanName);
   }
   /**
    * 在应用程序关闭时,清空spring框架配置信息。
    */
   public static void clear() {
    if (context != null) {
     context = null;
    }
   }
}

这样就可以取到application,只要配置正确,再通过application取bean那是轻而易举的事!

猜你喜欢

转载自2594082lhj.iteye.com/blog/2059848