从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 ...
随机推荐
- 【POJ】3648 Wedding
http://poj.org/problem?id=3648 题意:n对人(编号0-n-1,'w'表示第一个人,'h'表示第二个人),每对两个,人坐在桌子两侧.满足:1.每对人中的两个人不能坐在同一侧 ...
- SRM 595 DIV2 1000
数位DP的感觉,但是跟模版不是一个套路的,看的题解,代码好理解,但是确实难想. #include <cstdio> #include <cstring> #include &l ...
- URAL 1223. Chernobyl’ Eagle on a Roof
题目链接 以前做过的一题,URAL数据强点,优化了一下. #include <iostream> #include <cstdio> #include <cstring& ...
- Android -- ProgressBar(进度条的使用)
我们在开发程序是经常会需要软件全屏显示.自定义标题(使用按钮等控件)和其他的需求,今天这一讲就是如何控制Android应用程序的窗体显示. requestWindowFeature可以设置的值有:(具 ...
- 项目管理gitflow的用法(转)
在这里主要讲一下我在项目中用到的关于gitflow的用法. 公司的项目中,专门有一台用来存放版本库的服务器,路径是在默认的安装目录/opt/git/,那么在使用的时候,如果你是一个功能模块或者是一 ...
- window下 配置gitlab ssh非端口22端口
git config --global user.name "jack" git config --global user.email "jackluo@xxx.com& ...
- windows2008 IIS下配置FTP服务
一.服务器管理器 1.2008的系统使用服务器管理器,选择角色,因为我之前已经开启了IIS服务器角色,所以我现在只要添加角色服务即可,如果你没有开启过的话,直接添加角色即可. 2.选择WEB服务器,打 ...
- Nodejs操作redis
//npm install redis //首先加载node_redis模块 var redis = require('redis'); // 创建redis连接 var client = redis ...
- js鼠标滑轮滚动事件绑定(兼容主流浏览器)
/** Event handler for mouse wheel event. *鼠标滚动事件 */ var wheel = function(event) { var delta = 0; if ...
- 启动tomcat,报java.lang.NoClassDefFoundError
用的Build Path加进来的jar包,没有读取到,应该讲jar包放在lib目录下