一、 引言

  工作之余,在看一下当年学的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的更多相关文章

  1. 获取applicationContext对象的方法

    方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext(&quo ...

  2. 在Spring应用中创建全局获取ApplicationContext对象

    在Spring应用中创建全局获取ApplicationContext对象 1.需要创建一个类,实现接口ApplicationContextAware的setApplicationContext方法. ...

  3. Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式

    转自:http://chinazhaokeke.blog.163.com/blog/static/109409055201092811354236  Spring获取ApplicationContex ...

  4. SpringBoot项目中获取applicationContext对象

    ApplicationContext 对象是Spring开源框架的上下文对象实例,也就是我们常说的Spring容器,一般情况下我们是不用手动来管理它,而是由Spring框架自己来维护bean之间的关系 ...

  5. springMVC 使用WebApplicationContext获取ApplicationContext对象

    主要用于从application中获取bean 1.applicationContext 在web.xml中使用listener配置 <context-param> <param-n ...

  6. 获取InputStream对象的方法

    获取InputStream对象的方法 getResourceAsStream(String path) 默认path路径位于Class所在Module的src目录下 . InputStream is ...

  7. spring中获取ApplicationContext对象的技巧,含源码说明

    第一步,实现接口ApplicationContextAware,重写setApplicationContext方法,下方代码标红的地方,绿色部分 可以通过声明来进行存储到本类中. @Component ...

  8. Spring获取ApplicationContext

    在Spring+Struts+Hibernate中,有时需要使用到Spring上下文.项目启动时,会自动根据applicationContext配置文件初始化上下文,可以使用ApplicationCo ...

  9. 【转】SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法

    转自:http://www.coderli.com/junit-spring-test-applicationcontext JUnit单元测试用例中使用Spring框架,直接方式如下. @RunWi ...

随机推荐

  1. java9-7 成员内部类的修饰符

    1. 成员内部类的修饰符: private 为了保证数据的安全性 static 为了方便访问数据 注意:静态内部类访问的外部类数据必须用静态修饰. 案例:我有一个人(人有身体,身体内有心脏.) cla ...

  2. [转] 国外程序员整理的 C++ 资源大全

    关于 C++ 框架.库和资源的一些汇总列表,由 fffaraz 发起和维护. 内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包括了S ...

  3. HASHKILL

    6ac66ed89ef9654cf25eb88c21f4ecd0是flag的MD5码,(格式为ctf{XXX_XXXXXXXXXXX_XXXXX})由一个0-1000的数字,下划线,纽约的一个区,下划 ...

  4. Android 动态加载 (一) 态加载机制 案例一

    在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优势.本 ...

  5. Linq中查询List组合相同值数量大于1

     List< select g.Key).ToList();

  6. (转载)关于Apache 的两种工作模式

    今天在查看服务器的时候,发现服务器http请求数 每天增长越来越多,在优化集群服务器的时候,查看到Apache 的工作模式是prefork,于是想到了worker 模式, 想暂时的把当前运行模式改成w ...

  7. no.4 抽奖测试

    #-*-coding=gbk-*- import sys import random a=[] try: for x in range(1,20+1,1): #打印20人数编号 a.append(x) ...

  8. Caffe学习系列(4):激活层(Activiation Layers)及参数

    在激活层中,对输入数据进行激活操作(实际上就是一种函数变换),是逐元素进行运算的.从bottom得到一个blob数据输入,运算后,从top输入一个blob数据.在运算过程中,没有改变数据的大小,即输入 ...

  9. Linux常用指令---kill | killall(终止进程)

    kill Linux中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管理的常用命令.通常,终止一个前台进程可以使用Ctrl+C键,但是,对于一个后 ...

  10. 20135220谈愈敏Linux Book_4

    进程调度 进程:程序的运行态表现形式 进程调度程序:确保进程能有效工作的一个内核子系统,决定将哪个进程投入运行.何时运行以及运行多长时间,在可运行态进程之间分配有限的处理器时间资源. 最大限度的利用处 ...