基于Spring进行注解开发

基于Spring进行注解开发

Spring的BeanPostProcessor接口

BeanPostProcessor的作用

Spring容器bean实例化、配置以及其他初始化方法前后,进行一些逻辑处理.就需要实现BeanPostProcessor接口

public interface BeanPostProcessor {
   //实例化、依赖注入完毕,在调用显示的初始化之前完成一些定制的初始化任务  
    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
   //实例化、依赖注入、初始化完毕时执行
   Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}

声明SpringAnnotationScanner实现BeanPostProcessor接口

public class AutomaticSpringAnnotationsScanner implements BeanPostProcessor {
    private static final Logger log = LoggerFactory.getLogger(AutomaticSpringAnnotationsScanner.class);
    //注解的集合
    private final List<Class<? extends Annotation>> annotations = Arrays.asList(Config.class,Custom.class);
    private Class originalBeanClass;
    public AutomaticSpringAnnotationsScanner() {
    }
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (this.originalBeanClass != null) {
            //得到被注解的对象,保存。执行一些操作

            log.info("{} bean listeners added", beanName);
            this.originalBeanClass = null;
        }

        return bean;
    }
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        final AtomicBoolean add = new AtomicBoolean();
        ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallback() {
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                add.set(true);
            }
        }, new ReflectionUtils.MethodFilter() {
            public boolean matches(Method method) {
                Iterator i$ = AutomaticSpringAnnotationsScanner.this.annotations.iterator();
                Class annotationClass;
                do {
                    if (!i$.hasNext()) {
                        return false;
                    }

                    annotationClass = (Class)i$.next();
                } while(!method.isAnnotationPresent(annotationClass));

                return true;
            }
        });
        if (add.get()) {
            this.originalBeanClass = bean.getClass();
        }

        return bean;
    }
}

自定义的一些注解

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Config { //自定义的注解,作用在方法上
    String value() default "" ;
}
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Custom {//自定义的注解,作用在方法上

    String value() default "";
}

使用

//Springboot的注入方式:在启动的Application类中添加
@Bean
public AutomaticSpringAnnotationsScanner  automaticSpringAnnotationsScanner (){
    return new SpringAnnotationScanner();
}


//springMvc的注入方式
  @Component   //声明式注解
  public class AutomaticSpringAnnotationsScanner implements BeanPostProcessor{

  } 
//* 通过配置,或者注解都可以,方式很多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值