spring扩展点

BeanDefinition

  • GenericBeanDefinition : 通用的bean实现,自2.5以后新加入的bean文件配置属性定义类,是ChildBeanDefinition和RootBeanDefinition更好的替代者。
  • ScannedGenericBeanDefinition : 被包扫描到的bean定义
  • AnnotatedGenericBeanDefinition : 查找类注解初始化的定义
  • RootBeanDefinition : 代表一个从配置源(XML,Java Config等)中生成的BeanDefinition
  • ChildBeanDefinition : 可以从父BeanDefinition中集成构造方法,属性等。

validateRequiredProperties spring启动校验一些必要参数

package com.example.demo.config;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

public class CustomApplicationContext extends AnnotationConfigApplicationContext {

    @Override
    public void setEnvironment(ConfigurableEnvironment environment) {
        super.setEnvironment(environment);
        getEnvironment().setRequiredProperties("detector.applicationName");
    }

}

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(DemoApplication.class);
        springApplication.setApplicationContextFactory((webApplicationType) -> new CustomApplicationContext());
        springApplication.run(args);
    }

}

XXXAware

实现XXXAware,获取XXX对象

@Component
public class AwareBean implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
//    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println(111);
        applicationContext = applicationContext;
    }
}

监听器

第一种方式
@Component
public class MyListener implements ApplicationListener {
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println("收到事件:" + event.toString());
    }
}
第二种方式
@Component
public class MyListener2 {

    @EventListener
    private void listen(ApplicationEvent event){
        System.out.println("收到事件2:" + event.toString());
    }

}

BeanFactoryPostProcessor与BeanDefinitionRegistryPostProcessor

扩展,可以做一些前期准备工作

@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("获取到工厂,无所不能+1");
    }
}

@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println("这一步可以注册beanDefinition");
//        registry.registerBeanDefinition();
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("获取到工厂,无所不能");
//        beanFactory.getBeanDefinitionCount()
    }
}

initMethod与destroyMethod

@Bean(initMethod = "init", destroyMethod = "destroy")
public TestBeanMethod testBeanMethod(){
    return new TestBeanMethod();
}
public class TestBeanMethod {

    public void say(){
        System.out.println("say TestBeanMethod");
    }

    public void init(){
        System.out.println("TestBeanMethod say init");
    }

    public void destroy(){
        System.out.println("TestBeanMethod say destroy");
    }

}

SmartInitializingSingleton接口

@Component
public class MySmartInitializingSingleton implements SmartInitializingSingleton {
    
    @Override
    public void afterSingletonsInstantiated() {
        System.out.println("MySmartInitializingSingleton afterSingletonsInstantiated");
    }
}

FactoryBean/InitializingBean/DisposableBean接口

@Component
public class MyFactoryBean implements FactoryBean, InitializingBean, DisposableBean {
    @Override
    public TestFactoryBean getObject() throws Exception {
        System.out.println("TestFactoryBean getObject");
        return new TestFactoryBean();
    }

    @Override
    public Class getObjectType() {
        return TestFactoryBean.class;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("MyFactoryBean afterPropertiesSet");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("MyFactoryBean destroy");
    }
}

InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation + ProxyFactory

@Component
public class BeforeInstantiation implements InstantiationAwareBeanPostProcessor {

    @Override
    public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException {
        if(!"testProxyFactory".equals(beanName)){
            return null;
        }

        try {
            Object o = beanClass.getConstructor().newInstance();
            ProxyFactory proxyFactory = new ProxyFactory(o);
            DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();
            defaultPointcutAdvisor.setAdvice(new MethodInterceptor() {
                @Override
                public Object invoke(MethodInvocation methodInvocation) throws Throwable {
                    System.out.println("ProxyFactory enhance before");
                    Object proceed = methodInvocation.proceed();
                    System.out.println("ProxyFactory enhance after");
                    return proceed;
                }
            });
            proxyFactory.addAdvisor(defaultPointcutAdvisor);
            return proxyFactory.getProxy();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        return null;
    }
}

你可能感兴趣的:(spring扩展点)