spring 初始化ApplicationContext的几种方式

来源:互联网 发布:spss数据分析案例 编辑:程序博客网 时间:2024/05/21 09:11

最近做项目需要用到getBean(),在网上搜索之后发现有很多种方式获取ApplicationContext,做了些许尝试,解决了我的问题,决定把搜到的东西进行汇总,供大家学习。

方法一:FileSystemXmlApplicationContext

//1.默认为项目工作路径 即项目的根目录 ApplicationContext appCt2 = new FileSystemXmlApplicationContext("src/main/resources/app.spring.xml"); appCt2.getBean("beanId");
//2.前缀classpath:表示的是项目的classpath下相对路径 ApplicationContext appCt2 = new FileSystemXmlApplicationContext("classpath:app.spring.xml"); appCt2.getBean("beanId");
//3.使用前缀file 表示的是文件的绝对路径    ApplicationContext appCt2 = new FileSystemXmlApplicationContext("file:D:/app.spring.xml");    ApplicationContext appCt2 = new FileSystemXmlApplicationContext("D:/app.spring.xml");    appCt2.getBean("beanId");
//4.可以同时加载多个文件   String[] xmlCfg = new String[] { "src/main/resources/base.spring.xml","classpath:app.spring.xml"};   ApplicationContext appCt2 = new FileSystemXmlApplicationContext(xmlCfg);   appCt2.getBean("beanId");
//5.使用通配符加载所有符合要求的文件   ApplicationContext appCt2 = new FileSystemXmlApplicationContext("classpath:*.spring.xml");   appCt2.getBean("beanId");
//6.Resource ResourcePatternResolver     resolver=new PathmatchingResourceResolver(); Resource   res = resolver.getResource("classpath:com/hh/beans.xml"); Beanfactory    bf = new  XmlBeanFactory(res); Car  car= bf.getBean("car",Car.class) 

这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。

方法二:通过Spring提供的工具类WebApplicationContextUtils获取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。
方法三:ClassPathXmlApplicationContext (不推荐使用,比较费内存)
//1.没有前缀:默认为项目的classpath下相对路径    ApplicationContext appCt = new ClassPathXmlApplicationContext("app.spring.xml"); 
//2.前缀classpath:表示的是项目的classpath下相对路径    ApplicationContext appCt = new ClassPathXmlApplicationContext("classpath:app.spring.xml"); 
//3.使用前缀file 表示的是文件的绝对路径    ApplicationContext appCt = new ClassPathXmlApplicationContext("file:D:/app.spring.xml"); 
//4.可以同时加载多个文件   String[] xmlCfg = new String[] { "classpath:base.spring.xml","app.spring.xml"};   ApplicationContext appCt = new ClassPathXmlApplicationContext(xmlCfg); 
//5.使用通配符加载所有符合要求的文件   ApplicationContext appCt = new ClassPathXmlApplicationContext("*.spring.xml");
方法四:继承自抽象类ApplicationObjectSupport
抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。
Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。

方法五:继承自抽象类WebApplicationObjectSupport 
类似上面方法,调用getWebApplicationContext()获取WebApplicationContext 

方法六:实现接口ApplicationContextAware (推荐使用)
SpringContextsUtil 实现了接口ApplicationContextAware,setApplicationContext(ApplicationContext applicationContext)这个方法。Spring初始化的时候就将ApplicationContext 装配好了,那么项目中就可以直接使用applicationContext.getBean("beanId");
@Servicepublic class SpringContextsUtil implements ApplicationContextAware{private static ApplicationContext applicationContext;    //Spring应用上下文环境     /**  * 实现ApplicationContextAware接口的回调方法,设置上下文环境    * @param applicationContext  * @throws BeansException  */  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {    SpringContextsUtil.applicationContext = applicationContext;  }  /**  * @return ApplicationContext  */  public static ApplicationContext getApplicationContext() {    return applicationContext;  }  /**  * 获取对象    * @param name  * @return Object 一个以所给名字注册的bean的实例  * @throws BeansException  */  public static Object getBean(String name) throws BeansException {    return applicationContext.getBean(name);  }}


0 0
原创粉丝点击