23、自动装配-Aware注入Spring底层组件&原理

package com.lei.study_09_18.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

/**
 *
 *
 * @author LeiLei
 * @date 2019/9/18
 */
@Component
public class Dog implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware {

    public void setBeanName(String name) {
        System.out.println("当前bean的id为:"+name);
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("applicationContext:"+applicationContext);

    }

    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        String aa = resolver.resolveStringValue("测试#{20-18},${os.name}");
        System.out.println(aa);
    }
} 

测试结果:

 当前bean的id为:dog
测试2,Windows 10
applicationContext:org.springframework.context.annotation.AnnotationConfigApplicationContext@277c0f21: startup date [Wed Sep 18 21:28:41 CST 2019]; root of context hierarchy
com.lei.study_09_18.bean.Dog@7c0c77c7

 

总结:

1.Aware接口内部实现类,可以让我们得到IOC容器中的某些功能。

2.Aware接口源码是放在BeanPostProcessor,before方法中进行实现处理的。

你可能感兴趣的:(spring)