Spring 各种 Aware 接口回调注入

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学习资源】整理推荐

 

 


【Java面试题与答案】整理推荐

上一篇:Spring 中的 Aware 相关接口


下一篇:论文阅读:Relation Structure-Aware Heterogeneous Information Network Embedding