spring获取ApplicationContext对象的方法——ApplicationContextAware
一、 引言
工作之余,在看一下当年学的spring时,感觉我们以前都是通过get~ set~方法去取spring的Ioc取bean,今天就想能不能换种模型呢?因为我们在整合s2sh时,也许有那么一天就不用再遵循model-dao-service-action了,所以还是可以通过其他方法获取applicationContext,然后再获取相应的bean的。
二、 方法
如何获取ApplicationContext对象呢?
1. 可通过ClassPathXmlApplicationContext, FileSystemClassPathXmlApplicationContext, FileSystemXmlApplicationContext 去加载applicationContext.xml等配置文件去加载;
2. 可通过实现ApplicationContextAware接口获取;
3. 也许还有其他.....
区别:
笔者暂且只比较一下1与2吧。第一种会产生一种新的一个ApplicationContext对象,而这个对象与由web启动时生成的ApplicationContext对象是持有一样的资源,而第2种则是取当前运行环境下的ApplicationContext的对象。
三、 示例
1. SpringHelper
package com.swyma.spring.core; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; public class SpringHelper implements ApplicationContextAware{ public static SpringHelper springHelper = new SpringHelper(); private static ApplicationContext applicationContext; public final static SpringHelper getInstance() {
return springHelper;
} private SpringHelper() {
super();
} @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
} public ApplicationContext getApplicationContext() {
return applicationContext;
}
}
2. ISpringContext
package com.swyma.spring.core;
import org.springframework.context.ApplicationContext;
public interface ISpringContext {
ApplicationContext getContext();
<T> T lookup(String type);
<T> T lookup(Class cls);
boolean isSingleton(Class cls);
}
3. AbstractSpringContext
package com.swyma.spring.core;
import org.springframework.context.ApplicationContext;
public abstract class SpringContext implements ISpringContext {
@Override
public ApplicationContext getContext() {
return SpringHelper.getInstance().getApplicationContext();
}
}
4. basicSpringContext
package com.swyma.spring.service; import com.swyma.spring.core.SpringContext;
import com.swyma.spring.core.StringUtils; public class BasicSpringContext extends SpringContext{ static BasicSpringContext springContext = new BasicSpringContext(); public static BasicSpringContext getSpringContext() {
return springContext;
} public static void setSpringContext(BasicSpringContext springContext) {
BasicSpringContext.springContext = springContext;
} public <T> T lookup(String type) {
if(getContext().containsBean(type)) {
return (T) getContext().getBean(type);
}
return null;
} public <T> T lookup(Class cls) {
String type = cls.getSimpleName();
return lookup(StringUtils.toLowerInitial(type, true));
} public boolean isSingleton(Class cls) {
return getContext().isSingleton(StringUtils.toLowerInitial(cls.getSimpleName(),true));
} }
5. applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config />
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.swyma.spring"/>
<bean id="springhelper" class="com.swyma.spring.core.SpringHelper" lazy-init="false"/>
</beans>
6. 测试用例
package com.swyma.spring.test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.swyma.spring.core.ISpringContext;
import com.swyma.spring.entity.User;
import com.swyma.spring.service.BasicSpringContext;
import com.swyma.spring.service.LoginService;
import com.swyma.spring.service.RegisterService;
import com.swyma.spring.service.ServiceFacade;
import com.swyma.spring.service.UserService; /**
* juint test
* @author yemaoan
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class TestSpringEnv { @Test
public void testLookup() {
ISpringContext context = BasicSpringContext.getSpringContext();
LoginService loginService = context.lookup(LoginService.class);
loginService.handle();
} @Test
public void testAspect() {
ISpringContext context = new BasicSpringContext();
UserService userService = context.lookup(UserService.class);
RegisterService registerService = context.lookup(RegisterService.class);
userService.create(new User());
registerService.create();
} @Test
public void testFacade() {
ServiceFacade serviceFacade = new ServiceFacade();
LoginService loginService = serviceFacade.getLoginService();
loginService.handle();
} }
6. 其他的各种service就不再罗列了......
四、 总结
1. 利用这种取ApplicationContext会不会造成混乱呢,也即是资源不同步呢?其实笔者认为,我们也只是取到这个ApplicationContext对象,利用这个对象来获取我们所需要的Bean资源,并未对该对象进行修改更新操作,所以觉得这个在应用过程中还是安全的。
spring获取ApplicationContext对象的方法——ApplicationContextAware的更多相关文章
- 获取applicationContext对象的方法
方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext(&quo ...
- 在Spring应用中创建全局获取ApplicationContext对象
在Spring应用中创建全局获取ApplicationContext对象 1.需要创建一个类,实现接口ApplicationContextAware的setApplicationContext方法. ...
- Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式
转自:http://chinazhaokeke.blog.163.com/blog/static/109409055201092811354236 Spring获取ApplicationContex ...
- SpringBoot项目中获取applicationContext对象
ApplicationContext 对象是Spring开源框架的上下文对象实例,也就是我们常说的Spring容器,一般情况下我们是不用手动来管理它,而是由Spring框架自己来维护bean之间的关系 ...
- springMVC 使用WebApplicationContext获取ApplicationContext对象
主要用于从application中获取bean 1.applicationContext 在web.xml中使用listener配置 <context-param> <param-n ...
- 获取InputStream对象的方法
获取InputStream对象的方法 getResourceAsStream(String path) 默认path路径位于Class所在Module的src目录下 . InputStream is ...
- spring中获取ApplicationContext对象的技巧,含源码说明
第一步,实现接口ApplicationContextAware,重写setApplicationContext方法,下方代码标红的地方,绿色部分 可以通过声明来进行存储到本类中. @Component ...
- Spring获取ApplicationContext
在Spring+Struts+Hibernate中,有时需要使用到Spring上下文.项目启动时,会自动根据applicationContext配置文件初始化上下文,可以使用ApplicationCo ...
- 【转】SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法
转自:http://www.coderli.com/junit-spring-test-applicationcontext JUnit单元测试用例中使用Spring框架,直接方式如下. @RunWi ...
随机推荐
- JavaWeb学习之Servlet(一)----MyEclipse及Tomcat的配置
[声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4134921.html [开发环境] 物理机版本:Win 7旗舰版(64位 ...
- java 21-11 数据输入、输出流和内存操作流
IO数据流: 可以读写基本数据类型的数据 数据输入流:DataInputStream DataInputStream(InputStream in) 数据输出流:DataOutputStream ...
- 没有什么好神秘的: wait_on_page_bit
文件系统中经常会有wait_on_page_bit函数的封装,比如f2fs中就会有如下的代码: 1431 void f2fs_wait_on_page_writeback(struct page *p ...
- [DE2i-150] 重建PCIe_Fundmental範例說明
以下資料的整理主要是做備忘錄,避免以後忘了,順便留給需要的人. ========================================== 本文主要是參考友晶科技的DE2i-150光碟裡面的 ...
- C# == equals 本质理解
using System; using System.Diagnostics; using System.Text; using System.Collections; using System.Co ...
- php基础19:文件
<?php //1.打开文件的更好的方法是通过 fopen() 函数.此函数为您提供比 readfile() 函数更多的选项. //fopen() 的第一个参数包含被打开的文件名,第二个参数规定 ...
- 2015年新版C#从入门到精通(第2版)视频教学录像【无水印版】
<c#从入门到精通(第2版)>以零基础讲解为宗旨,用实例引导读者学习,深入浅出地介绍了c#的相关知识和实战技能.<c#从入门到精通(第2版)>第1篇[c#语言基础]主要讲解c# ...
- js学习第二篇简单语法
字符串(String)字面量 可以使用单引号或双引号 数组(Array)字面量 定义一个数组: [40, 100, 1, 5, 25, 10] 对象(Object)字面量 定义一个对象: {first ...
- struct2cell
函数功能:把结构体转换为元胞数组. 语法格式: c = struct2cell(s) 如果s是m*n(m行n列)的二维的结构体数组,每个结构体含有p个域,则转换得到一个p*m*n的元胞数组c. 如果s ...
- Java系列:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
Javaweb工程类中没有添加Tomcat运行时相关类导致. 下面是具体的解决方法: 1.右击web工程->属性或Build Path->Java Build Path->Libra ...