spring awre的理解

  自定义组件想要使用Spring容器底层的一些组件(ApplicationContext,BeanFactory,xxx);自定义组件实现xxxAware;在创建对象的时候,会调用接口规定的方法注入相关组件;Aware;把Spring底层一些组件注入到自定义的Bean中;xxxAware:功能使用xxxProcessor;ApplicationContextAware==》ApplicationContextAwareProcessor;spring继承aware接口的接口

package com.aware.demo.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;

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

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

        System.out.println("这是传入的ioc容器:"+applicationContext);
        this.applicationContext=applicationContext;
    }

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

    @Override
    public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
        String s = stringValueResolver.resolveStringValue("你好${os.name},我是#{20*18}");
        System.out.println(s);
    }
}
package com.aware.demo.bean;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AppMain {


    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        String[] bean = context.getBeanDefinitionNames();
        for (String b:bean)
        {
            System.out.println(b);
        }
    }
}

运行结果:

当前bean的名字:awareDemo
10:53:03.196 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'os.name' in PropertySource 'systemProperties' with value of type String
你好Windows 10,我是360
这是传入的ioc容器:org.springframework.context.annotation.AnnotationConfigApplicationContext@544a2ea6, started on Sun Aug 18 10:53:02 CST 2019
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
config
awareDemo

 

转载于:https://www.cnblogs.com/mingyuan1031/p/spring.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值