您的位置:首页 > 移动开发

Spring获取ApplicationContext的方法

2015-12-27 15:55 316 查看
感谢:Spring MVC 教程,快速入门,深入分析   


spring获取webapplicationcontext,applicationcontext几种方法详解

1.简单的使用ApplicationContext做测试,获取Spring中定义的bean可以用如下方式

package com.lxz.test;

import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.lxz.oa.dao.UserDao;

public class TestSpring {
@Test
public void test(){
//ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory");
//sessionFactory--->org.hibernate.internal.SessionFactoryImpl@1255313
//System.out.println("sessionFactory--->" + sessionFactory);

//其中bean.xml与UserDao.java在同一个目录下
//ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{ "bean.xml"}, UserDao.class);
//SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory");
//System.out.println("sessionFactory--->" + sessionFactory);

/*
如果有两个以上的配置文件:
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml", "bean.xml"});
或者用通配符:
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml");

classpath:前缀是不需要的,默认就是指项目的classpath路径下面;
如果要使用绝对路径,需要加上file:前缀表示这是绝对路径;

对于FileSystemXmlApplicationContext:
默认表示的是两种:
1.没有盘符的是项目工作路径,即项目的根目录;
2.有盘符表示的是文件绝对路径.
*/

//ClassPathXmlApplicationContext使用了file前缀是可以使用绝对路径的
//ApplicationContext ac = new ClassPathXmlApplicationContext("file:E:/Workspace/MyEclipse/OA/WebContent/WEB-INF/class/bean.xml");

//用文件系统的路径,默认指项目的根路径
//ApplicationContext ac = new FileSystemXmlApplicationContext("config/bean.xml");
//ApplicationContext ac = new FileSystemXmlApplicationContext("WebContent/WEB-INF/class/bean.xml");

//使用了classpath:前缀,这样,FileSystemXmlApplicationContext也能够读取classpath下的相对路径
//ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:bean.xml");
//ApplicationContext ac = new FileSystemXmlApplicationContext("file:E:/Workspace/MyEclipse/OA/WebContent/WEB-INF/class/bean.xml");

//不加file前缀
ApplicationContext ac = new FileSystemXmlApplicationContext("E:/Workspace/MyEclipse/OA/WebContent/WEB-INF/class/bean.xml");

SessionFactory sessionFactory = (SessionFactory)ac.getBean("sessionFactory");
//sessionFactory--->org.hibernate.internal.SessionFactoryImpl@52a2e3
System.out.println("sessionFactory--->" + sessionFactory);

}
}

2.实现ApplicationContextAware接口,同时在xml文件中进行配置(或采用注解的方式)

package com.lxz.oa.mvc;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class MyApplicationContextUtil implements ApplicationContextAware{
private static ApplicationContext applicationContext;
@Override
//实现了ApplicationContextAware接口,必须实现该方法
//Spring初始化时,会通过该方法将ApplicationContext对象注入
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
MyApplicationContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext(){
return applicationContext;
}
}
xml配置
<bean id="myApplicationContextUtil" class="com.lxz.oa.mvc.MyApplicationContextUtil"></bean>
3.继承ApplicationObjectSupport抽象类

package com.lxz.oa.mvc;

import org.springframework.context.support.ApplicationObjectSupport;

public class MyApplicationContextUtil2 extends ApplicationObjectSupport{
//继承类的方式,是调用父类的getApplicationContext()方法,获取Spring容器对象
public Object getBean(String beanName){
return getApplicationContext().getBean(beanName);
}
}
xml配置
<bean id="myApplicationContextUtil2" class="com.lxz.oa.mvc.MyApplicationContextUtil2"></bean>
测试文件
@Test
public void testSpring(){
//Spring手动初始化
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//MyApplicationContextUtil util = (MyApplicationContextUtil) ac.getBean("myApplicationContextUtil");
//util--->com.lxz.oa.mvc.MyApplicationContextUtil@da9067
//System.out.println("util--->" + util);

//SessionFactory sessionFactory = (SessionFactory) MyApplicationContextUtil.getApplicationContext().getBean("sessionFactory");
//System.out.println("sessionFactory--->" + sessionFactory);

MyApplicationContextUtil2 util2 = (MyApplicationContextUtil2) ac.getBean("myApplicationContextUtil2");
SessionFactory sessionFactory2 = (SessionFactory) util2.getBean("sessionFactory");
//sessionFactory--->org.hibernate.internal.SessionFactoryImpl@13ec758
System.out.println("sessionFactory--->" + sessionFactory2);
}


4.通过Spring提供的工具类获取ApplicationContext对象

/*
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");
说明:
这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。
上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。
其中 servletContext sc 可以具体 换成 servlet.getServletContext()
或者 this.getServletContext()
或者 request.getSession().getServletContext();
*/

/*
//说明:这种获取webApplicationContext的方式不依赖于servlet,不需要注入
//注意一点,在服务器启动时,Spring容器初始化时,不能通过以下方法获取Spring容器
//此方式在启动过程中获取的方式会出错
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
System.out.println("servletContext--->" + servletContext);
SessionFactory sessionFactory = (SessionFactory) webApplicationContext.getBean("sessionFactory");
System.out.println("sessionFactory---->" + sessionFactory);
*/

/*
如果在web.xml中使用了listener监听器来加载配置。如下
<listener>
<listener-
4000
class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Spring就会创建一个WebApplicationContext上下文,称为父上下文(父容器),
保存在 ServletContext中,key是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE的值。
此时可以使用如下方式取出上下文对象:
WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
*/
5.配置一个监听器
web.xml中配置

<!-- 配置Spring的用于初始化容器对象的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>com.lxz.oa.mvc.SpringInit</listener-class>
</listener>
package com.lxz.oa.mvc;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SpringInit implements ServletContextListener{
private static WebApplicationContext webApplicationContext;

@Override
public void contextDestroyed(ServletContextEvent event) {

}

@Override
public void contextInitialized(ServletContextEvent event) {
webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
//webApplicationContext--->--->Root WebApplicationContext: startup date [Sun Dec 27 21:53:56 CST 2015]; root of context hierarchy
System.out.println("webApplicationContext--->--->" + webApplicationContext);
}

public static ApplicationContext getApplicationContext(){
return webApplicationContext;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息