Spring中ApplicationContextAware接口用法
加载Spring配置文件时,如果Spring配置文件中所定义的Bean类,如果该类实现了ApplicationContextAware接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware接口中的 public void setApplicationContext(ApplicationContext context) throws BeansException方法,并且自动可获得ApplicationContext 对象。前提必须在Spring配置文件中指定改类。
程序如下:
Spring配置文件中配置: <bean id="springContext" class="com.shine.spring.SpringContextHelper"></bean>
/**
* ApplicationContext的帮助类
* 自动装载ApplicationContext
*/
public class SpringContextHelper implements ApplicationContextAware {
private static ApplicationContext context ;
/*
* 注入ApplicationContext
*/
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
//在加载Spring时自动获得context
SpringContextHelper.context = context;
System.out.println(SpringContextHelper.context);
}
public static Object getBean(String beanName){
return context.getBean(beanName);
}
}
|
分享到:
相关推荐
通过aware接口,可以对spring相应...首先创建一个类,实现ApplicationContextAware接口 , 该借口需要实现 setApplicationContext方法,该方法的参数由容器传递进来。 这样,bean 就获得了ApplicationContext这个资源
使用了ApplicationContextAware接口,获取spring管理的bean; 多项目整合夸spring容器获取bean的实现方式。
com-spring-ioc-demo:源码主要是学习Spring IOC的原理,以及对Bean的注册及控制,主要运用以下类对Spring进行扩展学习:BeanPostProcessor,BeanFactoryAware,BeanNameAware,ApplicationContextAware,FactoryBean...
(5)如果Bean实现了ApplicationContextAware接口,则Spring调用setApplicationContext()方法传入当前ApplicationContext实例的引用。 (6)如果BeanPostProcessor和Bean关联,则Spring将调用该接口的预初始化方法...
springboot 通过 ApplicationContextAware、ApplicationContext获取spring管理的bean-附件资源
4.3.2.2 使用静态工厂方法来实例化.. 36 4.3.2.3 使用实例工厂方法来实例化.. 37 4.4 依赖 38 4.4.1 依赖注入. 38 4.4.1.1 基于构造方法的依赖注入 39 4.4.1.2 基于setter 方法的依赖注入...
spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................
Spring boot 基础 1 1.什么是spring boot 1 2.spring boot的优点 2 3.环境搭建 3 3.1 添加依赖 3 ...11.8 ApplicationContextAware接口 28 11.9 过滤器\监听器\拦截器 29 12. 注解 31 13 . 延伸 32
主要介绍了Java编程实现Aware接口自定义获取bean的两种方式,通过BeanFactoryAware和ApplicationContextAware,具有一定参考价值,需要的朋友可以了解下。
自定义标签中@Autowired的属性...1.新建一个类SpringContext,实现接口ApplicationContextAware; 2.spring.xml中添加<bean id="" class="com............SpringContext"> 3.使用SpingContext.getBean("bean名");获取
web项目设置在Servlet的context parameter中 事件Application Event 自定义事件,集成ApplicationEvent 定义事件监听器,实现ApplicationListener 使用容器发布事件 Spring高级话题 Spring Aware ...
Spring学习 介绍 Spring轻松学习demo 1.弹簧IOC容器 1.弹簧注入方式 set方法注入 构造方法注入 2.Bean的装配 1.豆的作用域 单例单例获取 原型每次请求都创建实例 2.豆的生命周期 定义,初始化,使用,销毁 待补充......
1.继承ApplicationContextAware 2.代码如下 3.记得要把这个工具类加入到Spring的扫描路径下,这样才能在初始化时注入 4.使用方法
ApplicationContextAware ApplicationContextAwareProcessor ApplicationContextException ApplicationEvent ApplicationEventMulticaster ApplicationEventPublisher ApplicationEventPublisherAware ...
为何@Autowired可以注入ApplicationContext, 一般来说,我们可以通过实现ApplicationContextAware接口来获取ApplicationContext的引用。但是根据官方文档,发现也可以通过 @Autowired来注入ApplicationContext,这...
介绍了Java获取Bean容器的几种常见调用方式,其中详细写出了如何实现ApplicationContextAware 接口来获取Bean容器的工具类
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
主要介绍了Spring注解@Resource和@Autowired区别对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
public class LoginAction implements ApplicationContextAware { private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws ...