唐僧喜欢小龙女

导航

自定义组件实现Aware接口来获取spring容器底层的组件

1、aware 意思

翻译过来是知道的,已感知的,意识到的,所以这些接口从字面意思应该是能感知到所有Aware前面的含义

2、举例说明

@Component
public class Red implements ApplicationContextAware,BeanNameAware,EmbeddedValueResolverAware {

    private ApplicationContext applicationContext;




    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        this.applicationContext = applicationContext;




        System.out.println(applicationContext);
    }

    public void test(){
        Car car = applicationContext.getBean(Car.class);
        car.setColor("yellow");

    }

    @Override
    public void setBeanName(String name) {
        System.out.println("当前bean的名字是"+name);

    }


    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        System.out.println(resolver.resolveStringValue("你好${os.name}"));
    }


}

3、测试

    @RequestMapping("/hello4")
    @ResponseBody
    public String hello4(){

        red.test();
        Car car = color.getCar();
        System.out.println(car.getColor());

        return "success";
    }

 


posted on 2021-07-10 15:07  与时具进&不忘初心  阅读(79)  评论(0编辑  收藏  举报