spring awre的理解
Posted mingyuan1031
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring awre的理解相关的知识,希望对你有一定的参考价值。
自定义组件想要使用Spring容器底层的一些组件(ApplicationContext,BeanFactory,xxx);自定义组件实现xxxAware;在创建对象的时候,会调用接口规定的方法注入相关组件;Aware;把Spring底层一些组件注入到自定义的Bean中;xxxAware:功能使用xxxProcessor;ApplicationContextAware==》ApplicationContextAwareProcessor;
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
以上是关于spring awre的理解的主要内容,如果未能解决你的问题,请参考以下文章