Spring扩展点(Aware接口)

date [2019-06-02]

SpringContextAware.java

package edu.cninfo.aware;

import org.springframework.context.ApplicationContext;

public interface SpringContextAware {
    public void setApplicationContext(ApplicationContext applicationContext);
}

Dog.java

package edu.cninfo.aware;

import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class Dog implements SpringContextAware {
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public void show(){
        System.out.println("show["+applicationContext.getClass()+"]");
    }
}

ContextBeanProcess.java

package edu.cninfo.aware;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class ContextBeanProcess implements BeanPostProcessor {
    @Autowired
    private ApplicationContext applicationContext;
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof SpringContextAware){
            SpringContextAware sca=(SpringContextAware)(bean);
            sca.setApplicationContext(applicationContext);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

AwareProcessorApp.java

package edu.cninfo.aware;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AwareProcessorApp {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext acc = new AnnotationConfigApplicationContext("edu.cninfo.aware");
        acc.getBean(Dog.class).show();
        acc.close();
    }
}

运行结果

C:\soft\jdk1.8.0_202\bin\java.exe -javaagent:C:\soft\IDEA2018.3\lib\idea_rt.jar=64171:C:\soft\IDEA2018.3\bin -Dfile.encoding=UTF-8 -classpath C:\soft\jdk1.8.0_202\jre\lib\charsets.jar;C:\soft\jdk1.8.0_202\jre\lib\deploy.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\access-bridge-64.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\cldrdata.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\dnsns.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\jaccess.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\jfxrt.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\localedata.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\nashorn.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\sunec.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\sunjce_provider.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\sunmscapi.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\sunpkcs11.jar;C:\soft\jdk1.8.0_202\jre\lib\ext\zipfs.jar;C:\soft\jdk1.8.0_202\jre\lib\javaws.jar;C:\soft\jdk1.8.0_202\jre\lib\jce.jar;C:\soft\jdk1.8.0_202\jre\lib\jfr.jar;C:\soft\jdk1.8.0_202\jre\lib\jfxswt.jar;C:\soft\jdk1.8.0_202\jre\lib\jsse.jar;C:\soft\jdk1.8.0_202\jre\lib\management-agent.jar;C:\soft\jdk1.8.0_202\jre\lib\plugin.jar;C:\soft\jdk1.8.0_202\jre\lib\resources.jar;C:\soft\jdk1.8.0_202\jre\lib\rt.jar;D:\code\IdeaProjects\spring\learn-spring4-02\target\classes;C:\Users\Chen\.m2\repository\com\github\ulisesbocchio\jasypt-spring-boot-starter\2.1.0\jasypt-spring-boot-starter-2.1.0.jar;C:\Users\Chen\.m2\repository\com\github\ulisesbocchio\jasypt-spring-boot\2.1.0\jasypt-spring-boot-2.1.0.jar;C:\Users\Chen\.m2\repository\org\jasypt\jasypt\1.9.2\jasypt-1.9.2.jar;C:\Users\Chen\.m2\repository\org\springframework\spring-context\5.0.12.RELEASE\spring-context-5.0.12.RELEASE.jar;C:\Users\Chen\.m2\repository\org\springframework\spring-aop\5.0.12.RELEASE\spring-aop-5.0.12.RELEASE.jar;C:\Users\Chen\.m2\repository\org\springframework\spring-expression\5.0.12.RELEASE\spring-expression-5.0.12.RELEASE.jar;C:\Users\Chen\.m2\repository\org\springframework\spring-web\5.0.12.RELEASE\spring-web-5.0.12.RELEASE.jar;C:\Users\Chen\.m2\repository\org\springframework\spring-beans\5.0.12.RELEASE\spring-beans-5.0.12.RELEASE.jar;C:\Users\Chen\.m2\repository\org\springframework\spring-core\5.0.12.RELEASE\spring-core-5.0.12.RELEASE.jar;C:\Users\Chen\.m2\repository\org\springframework\spring-jcl\5.0.12.RELEASE\spring-jcl-5.0.12.RELEASE.jar;C:\Users\Chen\.m2\repository\org\apache\logging\log4j\log4j-core\2.6.2\log4j-core-2.6.2.jar;C:\Users\Chen\.m2\repository\org\apache\logging\log4j\log4j-api\2.6.2\log4j-api-2.6.2.jar;C:\Users\Chen\.m2\repository\org\apache\logging\log4j\log4j-jcl\2.6.2\log4j-jcl-2.6.2.jar;C:\Users\Chen\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\Chen\.m2\repository\org\apache\logging\log4j\log4j-slf4j-impl\2.6.2\log4j-slf4j-impl-2.6.2.jar;C:\Users\Chen\.m2\repository\org\slf4j\slf4j-api\1.7.21\slf4j-api-1.7.21.jar;C:\Users\Chen\.m2\repository\org\projectlombok\lombok\1.18.8\lombok-1.18.8.jar;C:\Users\Chen\.m2\repository\javax\annotation\jsr250-api\1.0\jsr250-api-1.0.jar edu.cninfo.aware.AwareProcessorApp
21:22:47.020 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
21:22:47.036 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
21:22:47.052 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
21:22:47.067 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
21:22:47.145 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
21:22:47.145 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
21:22:47.145 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references
21:22:47.270 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
21:22:47.270 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
21:22:47.270 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
21:22:47.270 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references
21:22:47.270 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
21:22:47.286 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
21:22:47.286 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
21:22:47.286 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' to allow for resolving potential circular references
21:22:47.302 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
21:22:47.302 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'contextBeanProcess'
21:22:47.302 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'contextBeanProcess'
21:22:47.333 [main] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Registered injected element on class [edu.cninfo.aware.ContextBeanProcess]: AutowiredFieldElement for private org.springframework.context.ApplicationContext edu.cninfo.aware.ContextBeanProcess.applicationContext
21:22:47.333 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'contextBeanProcess' to allow for resolving potential circular references
21:22:47.333 [main] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected element of bean 'contextBeanProcess': AutowiredFieldElement for private org.springframework.context.ApplicationContext edu.cninfo.aware.ContextBeanProcess.applicationContext
21:22:47.349 [main] DEBUG org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - Autowiring by type from bean name 'contextBeanProcess' to bean named 'org.springframework.context.annotation.AnnotationConfigApplicationContext@e320068'
21:22:47.349 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'contextBeanProcess'
21:22:47.349 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5a955565: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,contextBeanProcess,dog]; root of factory hierarchy
21:22:47.349 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
21:22:47.349 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
21:22:47.349 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
21:22:47.349 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
21:22:47.349 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
21:22:47.349 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
21:22:47.349 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.event.internalEventListenerProcessor' to allow for resolving potential circular references
21:22:47.368 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
21:22:47.368 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
21:22:47.368 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
21:22:47.369 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.event.internalEventListenerFactory' to allow for resolving potential circular references
21:22:47.376 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
21:22:47.377 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'contextBeanProcess'
21:22:47.377 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'dog'
21:22:47.377 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'dog'
21:22:47.378 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'dog' to allow for resolving potential circular references
21:22:47.383 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'dog'
21:22:47.383 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
21:22:47.422 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
21:22:47.438 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'dog'
show[class org.springframework.context.annotation.AnnotationConfigApplicationContext]
21:22:47.438 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
21:22:47.438 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5a955565: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,contextBeanProcess,dog]; root of factory hierarchy

猜你喜欢

转载自blog.csdn.net/weixin_33963189/article/details/90859687