Spring获取ApplicationContext
来源:互联网 发布:120网络歌曲 打包下载 编辑:程序博客网 时间:2024/05/29 04:38
第一类:主动获取
1. FileSystemXmlApplicationContext
此方法根据配置文件的文件系统路径进行加载,它的起始路径是项目路径,所以可以直接这样写WebRoot/WEB-INF,如果前边加了file:则说明后边的路径就要写全路径了,就是绝对路径。例如:
ApplicationContext context = new FileSystemXmlApplicationContext("D:/dev/applicationcontext.xml");
这种采用绝对路径加载的方式,灵活性很差,特别是win和linux等系统的文件系统不一样,如果跨系统部署会增加复杂度。
2. ClassPathXmlApplicationContext
此方法是根据相对路径加载配置文件。即从classpath下加载,也就是默认跟路径为WEB-INF/classes,并不是项目的根路径。例如:
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationcontext.xml");
3. XmlWebApplicationContext
这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。
ServletContext servletContext = request.getSession().getServletContext();
以上三种方式在web项目中不推荐,因为在web项目中,系统一旦启动,web服务器会初始化Spring的上下文的,如果用上述方法相当于又进行了一次初始化,这样子做会非常耗时。推荐使用第二类方式。
第二类:自动注入
Spring 提供两种方式,一种方式实现ApplicationContextAware接口,另种办法继承ApplicationObjectSupport。
1. 实现ApplicationContextAware接口
@Componentpublic class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtil.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { return applicationContext; } @SuppressWarnings("unchecked") public static <T> T getBean(String name) throws BeansException { return (T) applicationContext.getBean(name); }}
注意:这个地方使用了Spring的注解@Component,如果不是使用annotation的方式,而是使用xml的方式管理Bean,记得写入配置文件
<bean id="springContextUtil" class="com.***.utils.SpringContextUtil" />
2. 继承ApplicationObjectSupport类
该抽象类提供getApplicationContext()方法,可以方便的获取到ApplicationContext。Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContextcontext)方法将ApplicationContext 对象注入。
@Servicepublic class SpringContextHelper2 extends ApplicationObjectSupport { //提供一个接口,获取容器中的Bean实例,根据名称获取 public Object getBean(String beanName) { return getApplicationContext().getBean(beanName); }}
- Spring获取ApplicationContext
- spring获取ApplicationContext
- Spring MVC获取ApplicationContext
- 获取spring ApplicationContext
- spring 获取上下文applicationContext
- 怎么获取Spring的ApplicationContext
- 怎么获取Spring的ApplicationContext
- Spring +quartz获取ApplicationContext上下文
- spring 获取 ApplicationContext的方法
- Spring Bean获取applicationcontext方法
- Spring获取ApplicationContext的方法
- Spring,通过ApplicationContextAware获取ApplicationContext
- 获取Spring上下文即ApplicationContext
- JavaWeb中获取Spring的ApplicationContext
- 普通Java类获取Spring的ApplicationContext
- Spring获取bean方式(BeanFactory和ApplicationContext)
- spring获取ApplicationContext的方法-持续更新
- 普通Java类获取Spring的ApplicationContext
- ISP模块之RAW DATA去噪(二)--BM3D算法
- Javascript(一)-17-(JS函数_动态函数)
- C++ - 操作符的优先级
- ural 1106. Two Teams
- 设计模式之Bridge模式
- Spring获取ApplicationContext
- Android稀疏矩阵SparseArray
- Javascript(一)-18-(JS函数_匿名函数)
- jvm垃圾回收算法
- Java基础 笔记(四)
- org.apache.subversion.javahl.ClientException: Previous operation has not finished
- Javascript(一)-19-(JS函数_练习1)
- 成功从时间管理开始
- CGI