从Spring容器中获取Bean。ApplicationContextAware
引言:我们从几个方面有逻辑的讲述如何从Spring容器中获取Bean。(新手勿喷)
1、我们的目的是什么?
2、方法是什么(可变的细节)?
3、方法的原理是什么(不变的本质)?
1、我们的目的是什么?
从Spring容器中获取Bean。这里是指配置文件中注册的bean,比如dubbo类型的bean。另外一大类是通过注解获取的Bean。
2、 方法是什么?
ApplicationContext的主要实现类是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,前者默认从类路径加载配置文件,后者默认从文件系统中装载配置文件,前者用的比较多,比如:
if(mq==null){
			ApplicationContext context = new ClassPathXmlApplicationContext("activemq.xml");
			mq=(ActiveMQUtil)context.getBean("activeMQUtil");
		}
或者是:
private DubboContext(){
		context = new ClassPathXmlApplicationContext(new String[]{"dubbo-nmim-consumer.xml"});
		context.start();
	}
另外一种更为常见规范,用一个类(如SpringContextUtil)实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。
第一,因为spring要建立容器,就要加载配置文件。这个时候,需要注册ContextLoadListener或者这个类的子类。在web.xml中启动这个Listener类。(web.xml是项目的入口,一开始就启动加载的,dispatcher.xml是扫描配置的,比如那些注解配置)
<listener>
<listener-class>com.enjoyor.soa.traffic.server.nmim.listener.ContextListener</listener-class>
</listener>
这样的话会去读取默认的配置文件application.xml,如果需要特定的配置文件那么再配置一句:
context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:application.xml,
			classpath*:dubbo-nmim-consumer.xml
		</param-value>
	</context-param>
这样就可以了。
多嘴一句:dispatcher启动也是在web.xml中配置的
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:dispatcher.xml
</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
第二,对ApplicationContextAware接口的实现
public class SpringContextUtil implements ApplicationContextAware
再为SpringContextUtil添加一个静态的成员ApplicationContext类型的对象,以后方法类SpringContextUtil获取ApplicationContext就通过读取这个成员变量。
private static ApplicationContext applicationContext; //Spring应用上下文环境
实现ApplicationContextAware接口的默认方法:
/**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
* @param applicationContext
* @throws BeansException
*/
public void setApplicationContext(ApplicationContext applicationContextPara) throws BeansException {
SpringContextUtil.applicationContext = applicationContextPara;
}
第三,注册刚才的方法类,使其执行。(要么直接用id配置文件到容器中,要么用注解),setApplicationContext会默认执行的。(配置文件里面写就行,无论是dispatcher还是application.xml都一样,只要被web加载,本质都一样)
<bean id="SpringContextUtil " class="com.enjoyor.soa.traffic.util.frame.spring.SpringContextUtil"/>
第四,根据id获取bean
/**
* 获取对象
* @param name
* @return Object 一个以所给名字注册的bean的实例
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
第五:DubboRightService dubboRightService= (DubboRightService)SpringContextUtil.getBean("rightService"); rightService当然要已经注册到spring容器之中。
参考文章:http://blog.csdn.net/kaiwii/article/details/6872642
从Spring容器中获取Bean。ApplicationContextAware的更多相关文章
- FastJson序列化Json自定义返回字段,普通类从spring容器中获取bean
		前言: 数据库的字段比如:price:1 ,返回需要price:1元. 这时两种途径修改: ① 比如sql中修改或者是在实体类转json前遍历修改. ②返回json,序列化时候修改.用到的是fastj ... 
- Spring容器中获取bean实例的方法
		// 得到上下文环境 WebApplicationContext webContext = ContextLoader .getCurrentWebApplicationContext(); // 使 ... 
- Tomcat启动后,从spring容器中获取Bean和ServletContext
		public static Object getBean(String beanName){ ApplicationContext context = ContextLoader.getCurrent ... 
- java 从spring容器中获取注入的bean对象
		java 从spring容器中获取注入的bean对象 CreateTime--2018年6月1日10点22分 Author:Marydon 1.使用场景 控制层调用业务层时,控制层需要拿到业务层在 ... 
- 【String注解驱动开发】面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?
		写在前面 在前面的文章中,我们知道可以通过多种方式向Spring容器中注册bean.可以使用@Configuration结合@Bean向Spring容器中注册bean:可以按照条件向Spring容器中 ... 
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
		7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ... 
- SpringBoot 之 普通类获取Spring容器中的bean
		[十]SpringBoot 之 普通类获取Spring容器中的bean 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ... 
- 获取Spring容器中的Bean协助调试
		在使用Spring进行开发时,有时调bug真的是很伤脑筋的一件事,我们可以通过自定义一个监听器来获取Spring容器中的Bean实例来协助我们调试. 第一步:编写自定义监听器 /** * 监听serv ... 
- 7 -- Spring的基本用法 -- 5... Spring容器中的Bean;容器中Bean的作用域;配置依赖;
		7.5 Spring容器中的Bean 7.5.1 Bean的基本定义和Bean别名 <beans.../>元素是Spring配置文件的根元素,该元素可以指定如下属性: default-la ... 
随机推荐
- 【BZOJ1503】 [NOI2004]郁闷的出纳员 splay
			splay模板题,都快把我做忧郁了. 由于自己调两个坑点. 1.删除时及时updata 2.Kth 考虑k满足该点的条件即r->ch[1]->size+1<=k && ... 
- 【JAVA】Quartz中时间表达式的设置
			Quartz中时间表达式的设置-----corn表达式 时间格式: <!-- s m h d m w(?) y(?) -->, 分别对应: 秒>分>小时>日>月 ... 
- 直接用Qt写soap
			直接用Qt写soap 最近的项目里用到了webservice, 同事用的是`gSoap`来搞的. 用这个本身没什么问题, 但这货生成的代码实非人类可读, 到处都是`__`和`_`, 看得我眼晕.... ... 
- Json数据格式事例查看
			最简单的一种: [{"ProvinceID":1,"ProvinceName":"北京市","DateCreated": ... 
- Hashtable在ViewState中无法增加值
			在我调试程序的时候,我发现WebForm 2.0和MVC3解析ViewState的方式不同,同样的代码,在Weorm中管用,在MVC中不起作用. private Hashtable ht { get ... 
- Spring 整合 Redis(二)
			pom构建: <modelVersion>4.0.0</modelVersion> <groupId>com.x.redis</groupId> < ... 
- java之redis篇(spring-data-redis整合一)
			redis的知识:官网 1.利用spring-data-redis整合 项目使用的pom.xml: <project xmlns="http://maven.apache.org/PO ... 
- CSS Hack汇总快查(CSS兼容代码演示)
			文章出处和来源网址:http://www.divcss5.com/css-hack/c284.shtml 以下是常用CSS HACK问题及解决代码-DIV+CSS网支持 1.屏蔽IE浏览器(也就是IE ... 
- html中隐藏域hidden的作用介绍及使用示例
			基本语法: <input type="hidden" name="field_name" value="value"> 作用: ... 
- c#语句 随堂练习1
			1.请输入两个整数a,b,若a²+b²>100,则打印出a²+b²的结果,否则打印出a+b的结果. 2.有一组函数,y=x(x<1),y=2x-1(1<=x<10),y=3x- ... 
