加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的
public void setApplicationContext(ApplicationContext context) throws BeansException
方法,获得ApplicationContext对象。
前提必须在Spring配置文件中指定该类
public class ApplicationContextRegister implements ApplicationContextAware {
private Log log = LogFactory.getLog(getClass());
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
ContextUtils.setApplicationContext(applicationContext);
log.debug("ApplicationContext registed");
}
}
public class ContextUtils {
private static ApplicationContext applicationContext;
private static Log log = LogFactory.getLog(ContextUtils.class);
public static void setApplicationContext(ApplicationContext applicationContext) {
synchronized (ContextUtils.class) {
log.debug("setApplicationContext, notifyAll");
ContextUtils.applicationContext = applicationContext;
ContextUtils.class.notifyAll();
}
}
public static ApplicationContext getApplicationContext() {
synchronized (ContextUtils.class) {
while (applicationContext == null) {
try {
log.debug("getApplicationContext, wait...");
ContextUtils.class.wait(60000);
if (applicationContext == null) {
log.warn("Have been waiting for ApplicationContext to be set for 1 minute", new Exception());
}
} catch (InterruptedException ex) {
log.debug("getApplicationContext, wait interrupted");
}
}
return applicationContext;
}
}
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
}
配置文件:<bean class="com.sinotrans.framework.core.support.ApplicationContextRegister" />
正常情况:
- publicclassAppManagerextendsContextLoaderListenerimplementsServletContextListener{
-
privateServletContextcontext;
-
privateWebApplicationContextwebApplicationContext;
-
publicvoidcontextInitialized(ServletContextEventsce){
-
this.context=sce.getServletContext();
-
this.webApplicationContext=WebApplicationContextUtils.getRequiredWebApplicationContext(context);
-
this.context.setAttribute("WEBAPPLICATIONCONTEXT",webApplicationContext);}
- HttpSessionsession=request.getSession();
-
WebApplicationContextwebApplicationContext=(WebApplicationContext)session.getServletContext().getAttribute("WEBAPPLICATIONCONTEXT");
-
UnsubscribeEmailFacadeunsubscribeEmailFacade=(UnsubscribeEmailFacade)webApplicationContext.getBean("unsubscribeEmailFacade");
分享到:
相关推荐
介绍了Java获取Bean容器的几种常见调用方式,其中详细写出了如何实现ApplicationContextAware 接口来获取Bean容器的工具类
springboot 通过 ApplicationContextAware、ApplicationContext获取spring管理的bean-附件资源
4.6.2 ApplicationContextAware 和BeanNameAware . 75 4.6.3 其它Aware 接口 75 4.7 Bean 定义的继承. 77 4.8 容器扩展点. 78 4.8.1 使用BeanPostProcessor 来自定义bean 78 4.8.1.1 示例:...
2.5.3.1 基于Java的bean元数据.................................................................................................. 25 2.5.3.2 使用组件定义bean的元数据.........................................
自定义标签中@Autowired的属性...1.新建一个类SpringContext,实现接口ApplicationContextAware; 2.spring.xml中添加<bean id="" class="com............SpringContext"> 3.使用SpingContext.getBean("bean名");获取
Aliasing a bean outside the bean definition ................................................ 28 Instantiating beans .......................................................................................
Aliasing a bean outside the bean definition ................................................ 28 Instantiating beans .......................................................................................
1. 目录 1. 2. 目录 .........................................................................................................................................................1 JVM ........................
(5)如果Bean实现了ApplicationContextAware接口,则Spring调用setApplicationContext()方法传入当前ApplicationContext实例的引用。 (6)如果BeanPostProcessor和Bean关联,则Spring将调用该接口的预初始化方法...
com-spring-ioc-demo:源码主要是学习Spring IOC的原理,以及对Bean的注册及控制,主要运用以下类对Spring进行扩展学习:BeanPostProcessor,BeanFactoryAware,BeanNameAware,ApplicationContextAware,FactoryBean...
主要介绍了Java编程实现Aware接口自定义获取bean的两种方式,通过BeanFactoryAware和ApplicationContextAware,具有一定参考价值,需要的朋友可以了解下。
使用了ApplicationContextAware接口,获取spring管理的bean; 多项目整合夸spring容器获取bean的实现方式。
1.继承ApplicationContextAware 2.代码如下 3.记得要把这个工具类加入到Spring的扫描路径下,这样才能在初始化时注入 4.使用方法
通过aware接口,可以对spring相应资源(可能包含相关核心资源)进行操作(一定要慎重) 首先创建一个类,实现ApplicationContextAware接口 , 该借口需要实现 setApplicationContext方法,该方法的参数由容器传递...
ApplicationContextAware ApplicationContextAwareProcessor ApplicationContextException ApplicationEvent ApplicationEventMulticaster ApplicationEventPublisher ApplicationEventPublisherAware ...
3.意识到弹簧-让bean获取spring容器的服务 BeanNameAware可以获取容器中bean的名称 ApplicationContextAware当前的applicationContext,这也可以调用容器的服务 4. Bean的自动装配 byName根据属性名称自动装配-set...
事件传播的一个典型应用是,当Bean中的操作发生异常(如数据库连接失败),则通过事件传播 机制通知异常监听器进行处理。在笔者的一个项目中,就曾经借助事件机制,较好的实现了当系统 异常时在监视终端上报警,...
代码如下:public class ServiceServiceImpl implements ServiceService, ApplicationContextAware { @Override public void setApplicationContext(org.springframework.context.ApplicationContext ...
通过设定jvm的spring.profiles.active参数 web项目设置在Servlet的context parameter中 事件Application Event 自定义事件,集成ApplicationEvent 定义事件监听器,实现ApplicationListener 使用容器...