spring-自定义注解

#spring注解

理解spring bean 生命周期

  1. 实例化:spring对bean进行实例化
  2. 填充属性:spring将值和Bean的引用注入到Bean对应属性
  3. 调用BeanNameAware的setBeanName():如果Bean实现了该接口,Spring将Bean的ID传递给setBeanName()
  4. 调用BeanFactoryAware的setBeanFactory():如果Bean实现了该接口,Spring将BeanFactory容器实例传入
  5. 调用ApplicationContextAware的setApplicationContext():如果Bean实现了该接口,Spring将bean所在的上下文引用传入
  6. 调用BeanPostProcessor的预初始化方法:Spring调用他们的 postProcessBeforeInitialization()
  7. 调用InitializingBean的afterPropertiesSet():Spring调用他们的afterPropertiesSet().类似的bean使用init-method,该方法被调用
  8. 调用自定义的初始化方法
  9. 调用BeanPostProcessor的初始化方法 :Spring调用他们的 postProcessAfterInitialization()
  10. bean使用
  11. 容器关闭
  12. 调用DisposableBean的destroy()
  13. 调用自定义的销毁方法

自定义注解接口

spring 通过此类扫描:org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider。
默认并将带@Component扫描,通过方法registerDefaultFilters(),并注入到spring容器中。

示例:

@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface MyComponent {
    String value() default "";
}
@Configuration
@ComponentScan(includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = ComponentAnnotationTest.MyComponent.class)})

注解处理类

    @Component
    public class BoyAnnotationBeanPostProcessor implements BeanPostProcessor {
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            return bean;
        }

        @Override
        public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
            return o;
        }
    }

添加BeanScannerConfigurer

  • 继承ApplicationContextAware接口,Spring会读取ApplicationContextAware类型的的JavaBean,并调用setApplicationContext(ApplicationContext applicationContext)传入Spring的applicationContext。
  • 继承BeanFactoryPostProcessor接口,Spring会在BeanFactory的相关处理完成后调用postProcessBeanFactory方法,进行定制的功能
package com.nbd.ocp.study.springboot.annotation;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;


@Component
public class BeanScannerConfigurer implements BeanFactoryPostProcessor, ApplicationContextAware {
    private ApplicationContext applicationContext;

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

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }
}

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值