SpringBoot之自定义注解(基于BeanPostProcessor接口实现)

49 篇文章 1 订阅

步骤

  • 使用@interface 自定义注解
  • 编写注解处理类,实现BeanPostProcessor接口

原理

实现BeanPostProcessor接口的类即为Bean后置处理器,Spring加载机制会在所有Bean初始化的时候遍历调用每个Bean后置处理器。
其顺序为:Bean实例化-》依赖注入-》Bean后置处理器-》@PostConstruct

缺陷

只有在会示例化成Bean的类中使用,注解才会生效。(因为是使用了Bean后置处理器实现的嘛)

代码示例

完整参考代码github

自定义注解接口

import org.springframework.stereotype.Component;
import java.lang.annotation.*;

@Target({ElementType.FIELD}) //声明应用在属性上
@Retention(RetentionPolicy.RUNTIME) //运行期生效
@Documented //Java Doc
@Component
public @interface Boy {
    String value() default "";
}

注解处理类

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component //注意:Bean后置处理器本身也是一个Bean
public class BoyAnnotationBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        /**
         * 利用Java反射机制注入属性
         */
        Field[] declaredFields = bean.getClass().getDeclaredFields();
        for (Field declaredField : declaredFields) {
            Boy annotation = declaredField.getAnnotation(Boy.class);
            if (null == annotation) {
                continue;
            }
            declaredField.setAccessible(true);
            try {
                declaredField.set(bean, annotation.value());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        return o; //这里要返回o,不然启动时会报错
    }
}

注解使用类

import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;

@Service
public class HelloBoy {

    @Boy("小明")
    String name = "world";

    public void sayHello() {
        System.out.println("hello, " + name);
    }
}

测试类

import com.markey.annotations.Boy.HelloBoy;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloBoyTest {

    @Autowired
    HelloBoy helloBoy;

    @Test
    public void HelloBoyTest() {
        helloBoy.sayHello();
    }

}

è¿è¡ç»æ

注解无效示例

测试类

import com.markey.annotations.Boy.HelloBoy;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloBoyTest {

    HelloBoy helloBoy = new HelloBoy(); //新建的对象,不是容器中的Bean
    @Test
    public void HelloBoyTest() {
        helloBoy.sayHello();
    }
}

è¿è¡ç»æ

Tips
其实Spring很多原生注解(例如@Autowired,其处理类是AutowiredAnnotationBeanPostProcessor),也是通过实现BeanPostProcessor接口这种方式来实现的。

总结

沿着上文的思路我们可以发现,自定义注解的重点在于 根据注解要达到的目的,考虑如何处理注解。

例如: 本文的需求是通过注解来注入一个属性,考虑到属性可以为static,所以通过Spring配置加反射的方式; 如果属性不能为static,则可以通过构造方法来实现。

在实际开发中,处理注解还有其他多种方式,各有优缺点。

例如:

  • 在Bean的后置处理器中处理注解
    通过实现Bean的后置处理器方法,配合Java反射。
    这种注解可以使用在类、方法、属性上。
    局限性是这种注解只能在Spring Bean中使用。
    参考SpringBoot之自定义注解(基于BeanPostProcessor接口实现)

  • 通过Spring AOP处理
    原理是通过注解来定义切点。
    优点是编写简单。
    但这种注解也只能使用在Spring Bean中,且只能作用于方法上
    参考SpringBoot之自定义注解(基于AOP实现)

  • 通过AspectJ切面处理
    通过SspectJ切面编程实现,类似SPring AOP切面。而已没有只能作用在方法上的局限

  • 通过Spring配置类处理
    通过配置类实现在Spring启动过程中处理注解,配置Java反射机制来注入静态变量。
    局限性是只能注入静态属性。
    这也是本文所使用的方法。

资源网址:https://blog.csdn.net/u012578322/article/details/87487062

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值