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资源,并未对该对象进行修改更新操作,所以觉得这个在应用过程中还是安全的。
出处: http://www.cnblogs.com/maoan/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。