加载Spring配置文件时,如果Spring配置文件中所定义的Bean类,如果该类实现了ApplicationContextAware
接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware
接口中的
public void setApplicationContext(ApplicationContext context) throws BeansException
方法,并且自动可获得ApplicationContext
对象。前提必须在Spring配置文件中指定改类。
一个Demo程序如下:
Spring配置文件中配置:
1
|
<
bean
id
=
"springContext"
class
=
"com.shine.spring.SpringContextHelper"
></
bean
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
/**
* ApplicationContext的帮助类
* 自动装载ApplicationContext
*
* @author ChenST
* @create 2010-6-24
*
*/
public
class
SpringContextHelper
implements
ApplicationContextAware {
private
static
ApplicationContext context ;
@Override
public
void
setApplicationContext(ApplicationContext context)
throws
BeansException {
SpringContextHelper.context = context;
System.out.println(SpringContextHelper.context);
}
public
static
Object getBean(String beanName){
return
context.getBean(beanName);
}
}
|
分享到:
相关推荐
com-spring-ioc-demo:源码主要是学习Spring IOC的原理,以及对Bean的注册及控制,主要运用以下类对Spring进行扩展学习:BeanPostProcessor,BeanFactoryAware,BeanNameAware,ApplicationContextAware,FactoryBean...
springboot 通过 ApplicationContextAware、ApplicationContext获取spring管理的bean-附件资源
通过aware接口,可以对spring相应...首先创建一个类,实现ApplicationContextAware接口 , 该借口需要实现 setApplicationContext方法,该方法的参数由容器传递进来。 这样,bean 就获得了ApplicationContext这个资源
可以创建一个帮助类,实现ApplicationContextAware接口,并在其中保存Spring ApplicationContext。然后,在需要使用service的地方,可以使用该帮助类来获取对应的bean。 在帮助类中,我们可以使用静态变量来保存...
本文将深入探讨几种常见的获取Spring容器的方法,包括使用`ApplicationContext`、通过`ServletContext`、利用`ApplicationObjectSupport`、`WebApplicationObjectSupport`以及实现`ApplicationContextAware`接口等。...
在Spring中,监听器是通过实现特定接口或者继承抽象类来定义的,这些接口包括ApplicationContextAware、ApplicationListener等。本资料"spring监听器共20页.pdf.zip"可能详细阐述了Spring监听器的概念、工作原理以及...
`ApplicationContextAware`是其中的一个典型接口,当我们想要在非托管类(即非Spring Bean)中获取Spring容器中的其他Bean时,可以使用这个接口。 `ApplicationContextAware`接口提供了`setApplicationContext`方法...
为了管理和使用这些bean,文件中还定义了一个`ApplicationContextHolder`类,它实现了`ApplicationContextAware`和`BeanPostProcessor`接口。`ApplicationContextAware`接口使类能够获取Spring应用上下文,从而可以...
`ApplicationContextAware`是Spring提供的一个接口,它允许我们在类中注入ApplicationContext。首先,我们需要在静态服务类或者一个非静态的辅助类中实现这个接口。当Spring容器初始化完成后,会自动调用`...
4. **ApplicationContextAware**:让线程处理类实现`ApplicationContextAware`接口,Spring会在启动时自动为其注入`ApplicationContext`。然后,可以通过`ApplicationContext`的`getBean()`方法来获取所需的bean。...
import org.springframework.context.ApplicationContextAware; public class MyBean implements ApplicationContextAware { private static ApplicationContext context; @Override public void ...
3. **实现ApplicationContextAware**:为了在`Timer`中使用Spring管理的Bean,我们需要实现`ApplicationContextAware`接口,以便获取Spring上下文。 ```java @Component public class TimerConfig implements ...
此外,`@Component`注解使这个类成为了一个Spring Bean,`ApplicationContextAware`接口则允许我们获取并设置`ApplicationContext`,从而能够在`loadXmlConfigurations`方法中加载XML配置。 在实际开发中,你还需要...
一种常见的做法是创建一个工具类,如`SpringUtil`,该类实现`ApplicationContextAware`接口。这个接口是Spring提供的,它的`setApplicationContext`方法会在Spring初始化时自动调用,传入当前的ApplicationContext。...
首先,在`spring-context.xml`中添加一个名为`ApplicationContextUtil`的Bean,该Bean用于实现`ApplicationContextAware`接口,以便Spring容器能够自动注入`ApplicationContext`。 ```xml ``` ##### 2. 实现`...
为了实现这一功能,该类需要实现`ApplicationContextAware`接口,以便能够访问Spring容器中的资源。 ```java package application.comet; import java.util.Date; import org.springframework.context....
首先,Spring通过`ApplicationContextAware`接口提供了一种方式,使得Bean能够获取到ApplicationContext的引用。当一个Bean实现了这个接口,Spring容器会在初始化Bean后自动调用`setApplicationContext`方法,传入...
2. 使用 ApplicationContextAware 接口:使用 ApplicationContextAware 接口可以在 Bean 创建时获取 ApplicationContext 对象,从而解决循环依赖的问题。 3. 使用依赖注入容器:使用依赖注入容器可以自动解决循环...
- 如果Bean实现了特定的Spring接口,如`BeanNameAware`,`BeanFactoryAware`,`ApplicationContextAware`,Spring会在适当的时间调用相应的方法。 - `BeanNameAware`的`setBeanName()`方法用于将Bean的ID传递给...