Spring bean 实现了如下 Aware 接口,可以注入相关对象:
BeanFactoryAware, //获取 IoC 容器 BeanFactory 的能力
ApplicationContextAware, //获取 Spring 应用上下文 ApplicationContext 对象的能力
EnvironmentAware, //获取 Environment 对象的能力
ResourceLoaderAware, //获取资源加载器对象 ResourceLoader 的能力
BeanClassLoaderAware, //获取加载当前 Bean Class 的 ClassLoader 的能力
BeanNameAware, //获取当前 Bean 名称的能力
MessageSourceAware, //获取用于国际化 MessageSource 对象的能力
ApplicationEventPublisherAware, //获取事件发布对象 ApplicationEventPublisher 的能力
EmbeddedValueResolverAware //获取占位符处理对象 StringValueResolver 的能力
示例代码:
spring xml 配置
<bean name="user1" class="constxiong.User">
<property name="id" value="1"/>
</bean>
User 类
public class User implements
BeanFactoryAware, //获取 IoC 容器 BeanFactory 的能力
ApplicationContextAware, //获取 Spring 应用上下文 ApplicationContext 对象的能力
EnvironmentAware, //获取 Environment 对象的能力
ResourceLoaderAware, //获取资源加载器对象 ResourceLoader 的能力
BeanClassLoaderAware, //获取加载当前 Bean Class 的 ClassLoader 的能力
BeanNameAware, //获取当前 Bean 名称的能力
MessageSourceAware, //获取用于国际化 MessageSource 对象的能力
ApplicationEventPublisherAware, //获取事件发布对象 ApplicationEventPublisher 的能力
EmbeddedValueResolverAware //获取占位符处理对象 StringValueResolver 的能力
{
private BeanFactory beanFactory;
private ApplicationContext applicationContext;
private Environment environment;
private ResourceLoader resourceLoader;
private ClassLoader beanClassLoader;
private String beanName;
private MessageSource messageSource;
private ApplicationEventPublisher applicationEventPublisher;
private StringValueResolver embeddedValueResolver;
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "User{" +
"beanFactory=" + beanFactory + '
' +
"applicationContext=" + applicationContext + '
' +
"environment=" + environment + '
' +
"resourceLoader=" + resourceLoader + '
' +
"beanClassLoader=" + beanClassLoader + '
' +
"beanName='" + beanName + ''' + '
' +
"messageSource=" + messageSource + '
' +
"applicationEventPublisher=" + applicationEventPublisher + '
' +
"embeddedValueResolver=" + embeddedValueResolver + '
' +
"id=" + id +
'}';
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@Override
public void setBeanClassLoader(ClassLoader beanClassLoader) {
this.beanClassLoader = beanClassLoader;
}
@Override
public void setBeanName(String name) {
this.beanName = name;
}
@Override
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
@Override
public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
this.embeddedValueResolver = stringValueResolver;
}
}
测试代码
/**
* 测试 Bean 通过接口方式回调注入系统内置 bean
* @author ConstXiong
*/
@Configuration
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring-interface-callback-inject.xml");
User user = context.getBean("user1", User.class);
System.out.println(user);
}
}
打印结果
User{beanFactory=org.springframework.beans.factory.support.DefaultListableBeanFactory@1a407d53: defining beans [user1]; root of factory hierarchy
applicationContext=org.springframework.context.support.ClassPathXmlApplicationContext@6193b845, started on Thu Feb 04 14:21:00 CST 2021
environment=StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[PropertiesPropertySource {name='systemProperties'}, SystemEnvironmentPropertySource {name='systemEnvironment'}]}
resourceLoader=org.springframework.context.support.ClassPathXmlApplicationContext@6193b845, started on Thu Feb 04 14:21:00 CST 2021
beanClassLoader=sun.misc.Launcher$AppClassLoader@18b4aac2
beanName='user1'
messageSource=org.springframework.context.support.ClassPathXmlApplicationContext@6193b845, started on Thu Feb 04 14:21:00 CST 2021
applicationEventPublisher=org.springframework.context.support.ClassPathXmlApplicationContext@6193b845, started on Thu Feb 04 14:21:00 CST 2021
embeddedValueResolver=org.springframework.beans.factory.config.EmbeddedValueResolver@520a3426
id=1}
完整代码:017-spring-interface-callback-inject
【Java学习资源】整理推荐
- 自定义 Bean 作用域
- Bean 的作用域
- maven 集成 tomcat 以 jar 的方式打包 web 应用
- Spring 依赖注入与依赖查找来源的区别
- Spring 依赖注入的处理过程与 DependencyDescriptor 的说明
- Spring 各种 Aware 接口回调注入
- Spring Bean 生命周期内的 Exception 复现
- Spring 内建 Bean
- Spring Bean 的别名
- Spring Bean 未指定名称的命名规则
- Bean 何时被 GC
- Spring Bean 延迟加载
- ObjectFactory 与 BeanFactory 的区别
- Bean 销毁的方式与顺序
- Bean 初始化的方式与顺序
- Bean 的实例化方式
- Bean的注册方式
- 什么是 BeanDefinition?
- Spring IoC 容器启动时做了什么?
- BeanFactory 与 FactoryBean 的区别
- BeanFactory 与 ApplicationContext 的区别
- Spring IoC 依赖注入的实现方式
- Spring IoC 依赖注入(支持哪些数据类型?)
- Spring bean 依赖查找
- Spring-IoC
- Spring 的核心特性