xxxAwareを達成するために、カスタムコンポーネント;あなたは根本的なSpringコンテナ(ApplicationContextの、たBeanFactory、XXX)の構成要素のうちのいくつかを使用するカスタムコンポーネントメソッドオブジェクトを作成するとき、それはインターフェース指定インジェクション関連のコンポーネントを呼び出し、認識、基礎となる春にはいくつかのコンポーネントを注入しますビーンのカスタムに、xxxAware:関数はxxxProcessorを使用しています。ApplicationContextAware ==「ApplicationContextAwareProcessor。
パッケージcom.aware.demo.bean。 輸入org.springframework.beans.BeansException。 輸入org.springframework.beans.factory.BeanNameAware。 輸入org.springframework.context.ApplicationContext; 輸入org.springframework.context.ApplicationContextAware。 輸入org.springframework.context.EmbeddedValueResolverAware。 輸入org.springframework.stereotype.Component。 輸入org.springframework.util.StringValueResolver; @Component パブリック クラス AwareDemoは実装ApplicationContextAware、BeanNameAware、EmbeddedValueResolverAware { プライベートをApplicationContextのApplicationContextの; @Override 公共 ボイド setApplicationContext(ApplicationContextのApplicationContextの)はスローBeansException { するSystem.out.printlnを( "这是传入的IOC容器:" + のApplicationContext)。 この .applicationContext = のApplicationContext。 } @Override 公共 ボイドsetBeanName(文字列S){ するSystem.out.println( "当前豆的名字:" + S)。 } @Override 公共 ボイドsetEmbeddedValueResolver(StringValueResolver stringValueResolver){ 文字列の= StringValueResolver.resolveStringValue( "こんにちは$ {os.name}、Iは* 20 18}、{#です" ); のSystem.out.println(S); } }
パッケージcom.aware.demo.bean。 輸入org.springframework.context.annotation.AnnotationConfigApplicationContext; パブリック クラスAppMain { 公共 静的 ボイドメイン(文字列[]引数){ AnnotationConfigApplicationContextコンテキスト = 新しい AnnotationConfigApplicationContext(コンフィグ。クラス)。 文字列[]豆 = context.getBeanDefinitionNames()。 以下のための(列B:豆) { System.out.printlnは(B) } } }
結果:
当前豆的名字:awareDemo 10:53:03.196 [メイン] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - PropertySource 'systemProperties'に見出さキー'os.name' String型の値を持つ Windowsの你好 10 、我360是 这是传入的IOC容器:org.springframework.context.annotation.AnnotationConfigApplicationContext@544a2ea6は、日8月に開始した 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 設定 awareDemo