从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 ...
随机推荐
- HDU 2825 Wireless Password(AC自动机+DP)
题目链接 做题, #include <cstdio> #include <string> #include <cstring> using namespace st ...
- docker 报错Failed to start Docker Storage Setup. 的处理基本都是容器满了
:: localhost docker-storage-setup: Volume group extents): required. Apr :: localhost systemd: docker ...
- java中instanceof和getClass()的区别分析
class A { } class B extends A { } Object o1 = new A(); Object o2 = new B(); o1 instanceof A => t ...
- 多态(Java)
一.多态 1.什么是多态? 解析:不同的对象对于同一个操作,做出的响应不同 具有表现多种形态的能力的特征 2.使用多态的优点 解析:为了实现统一调用 一个小例子:<父类类型作为参数> 父类 ...
- html中a标签做容器的问题
今天试验了一下a标签当作容器的问题,若a包含的容器中没有a标签的话,a标签是可以被当作成容器使用的,在谷歌浏览器这种浏览器中是可行的,但是在低版本的IE中会有bug出现,就是浏览器在解析的时候会把a标 ...
- Oracle索引简单介绍与示例
索引的三大特性 1索引高度 在SQL检索数据(SELECT)的时候,索引的高度的不同对检索的效率有明显的差别,数据库访问索引需要读取的数据块通常是索引的高度+1个数据块数,也就是说索引的高度越高,访问 ...
- 20145337《Java程序设计》第四周学习总结
20145337<Java程序设计>第四周学习总结 教材学习内容总结 继承与多态 继承 继承,避免多个类间重复定义共同行为 符合DRY(Don't Repeat Yourself)原则,多 ...
- IOS第二天多线程-04简化单例模式
******HMSingleton-ARC.h // .h文件 #define HMSingletonH(name) + (instancetype)shared##name; // .m文件 #de ...
- 【iCore3 双核心板】例程三十五:HTTP_IAP_ARM实验——更新升级STM32
实验指导书及代码包下载: http://pan.baidu.com/s/1eRgzSPW iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- JSTL标签出错:<c:forEach var="book" items="${requestScope.books}" varStatus="status">
今天在运行书里的JSTL标签代码的时候出错,总结一下: 问题1.The JSP specification requires that an attribute name is preceded by ...