Bean后处理器

本文介绍了Spring中的Bean后处理器,包括AutowiredAnnotationBeanPostProcessor处理@Autowired和@Value注解,CommonAnnotationBeanPostProcessor解析@Resource、@PostConstruct和@PreDestroy,以及ConfigurationPropertiesBindingPostProcessor用于@ConfigurationProperties的解析。通过实例演示了如何添加和使用这些后处理器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备一个Bean

public class Bean3 {
    private Bean2 bean2;

    @Autowired
    public void setBean2(Bean2 bean2) {
        System.out.println("@Autowired生效");
        this.bean2 = bean2;
    }


    @Resource
    public void setBean3(Bean3 bean3) {
        System.out.println("@Resource生效");
        this.bean3 = bean3;
    }

    private Bean3 bean3;

    private String home;

    @Autowired
    public void setHome(@Value("${java_home}") String home) {
        System.out.println("@Value生效" + home);
        this.home = home;
    }

    @PostConstruct
    public void init() {
        System.out.println("@PostConstruct生效");
    }

    @PreDestroy
    public void destory() {
        System.out.println("@PreDestory生效");
    }

    @Override
    public String toString() {
        return "Bean3{" +
                "bean2=" + bean2 +
                ", bean3=" + bean3 +
                ", home='" + home + '\'' +
                '}';
    }
}

准备一个容器,这里选用GenericApplicationContext,因为它比较干净,添加工厂后处理器也很简单

        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean("bean1",Bean1.class);
        context.registerBean("bean2",Bean2.class);
        context.registerBean("bean3",Bean3.class);
        context.refresh();
        context.close();

1.AutowiredAnnotationBeanPostProcessor 

解析@Autowired @Value

添加后处理器

        context.registerBean(AutowiredAnnotationBeanPostProcessor.class);//解析@Autowired @Value

打印: 

@Autowired生效,但是报错,无法识别@Value注解,原因是beanFactory默认的处理器无法处理,手动添加

        context.getDefaultListableBeanFactory().setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());

2.CommonAnnotationBeanPostProcessor

解析@Resource @PostConstruct @PreDestory

添加后处理器

        context.registerBean(CommonAnnotationBeanPostProcessor.class);//解析@Resource @PostConstruct @PreDestory

打印:

 

3.ConfigurationPropertiesBindingPostProcessor

 解析@ConfigurationProperties

准备一个类

@ConfigurationProperties(prefix = "java")
public class Bean4 {
    String home;

    String version;

    public void setHome(String home) {
        this.home = home;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    @Override
    public String toString() {
        return "Bean4{" +
                "home='" + home + '\'' +
                ", version='" + version + '\'' +
                '}';
    }
}

 添加后处理器

        ConfigurationPropertiesBindingPostProcessor.register(context.getDefaultListableBeanFactory());//解析@ConfigurationProperties
        System.out.println(context.getBean(Bean4.class))

打印: 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值