spring之BeanPostProcessor

本文介绍Spring框架中BeanPostProcessor接口的作用及其实现原理。详细解释了BeanPostProcessor的执行时机,即在Bean实例化后且初始化方法之前进行操作。通过示例代码展示了如何创建自定义BeanPostProcessor实现类,并在实际项目中应用。

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

  • BeanPostProcessor : bean后处理器

  • 执行的时机 : bean实例化后(属性已经注入了),在bean的init方法(bean配置了init-method)之前调用postProcessBeforeInitialization , init方法调用后在调用postProcessBeforeInitialization

  • 注意::如果注册多个BeanPostProcessor实现类,并且需要按顺序执行,那么需要实现Order接口(org.springframework.core.Ordered)

  • 示例 :

  • BeanPostProcessor实现类:

package com.study.beanPost;

import com.study.annotation.MyBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

/**
 * @author Jun
 * data  2019-04-07 19:53
 * bean后处理器
 * 注意::如果注册多个BeanPostProcessor实现类,并且需要按顺序执行,
 *      那么需要实现Order接口(org.springframework.core.Ordered)
 *
 * 执行的时机 : bena实例化后(属性已经注入了),在bean的init方法(bean配置了init-method)之前调用postProcessBeforeInitialization ,
 *            init方法调用后在调用postProcessBeforeInitialization
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    /**
     * @param bean     需要处理的bean
     * @param beanName 需要处理的bean的名称
     */
    @Nullable
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        Class<?> aClass = bean.getClass();
        MyBean annotation = aClass.getAnnotation(MyBean.class);

        if (annotation == null) return bean;

        System.out.println("postProcessAfterInitialization : " + beanName);

        return bean;
    }

    @Nullable
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        Class<?> aClass = bean.getClass();
        MyBean annotation = aClass.getAnnotation(MyBean.class);
        if (annotation == null) return bean;

        System.out.println("postProcessBeforeInitialization : " + beanName);

        return bean;
    }
}
  • bean类
package com.study.beanPost;

import com.study.annotation.MyBean;

/**
 * @author Jun
 * data  2019-04-07 19:58
 */
@MyBean
public class Person {
    private int age ;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
        System.out.println(age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private void init() {
        System.out.println("Person init");
    }
}

  • 注解
package com.study.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyBean {
    String value() default "myBean";
}
  • spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
       xmlns:p="http://www.springframework.org/schema/p"
       default-autowire="byName"
>

    <context:component-scan base-package="com.study"/>
	
	<bean id="person" class="com.study.beanPost.Person" init-method="init">
        <property name="name" value="大宝"></property>
        <property name="age" value="18"></property>
    </bean>


    <!--开启AOP对注解的支持持-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>
  • 测试:
package com.study.beanPost;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Jun
 * data  2019-04-07 20:00
 */
public class TestBean {


    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        Person person = applicationContext.getBean("person", Person.class);
    }
}

  • 结果
18
postProcessBeforeInitialization : person
Person init
postProcessAfterInitialization : person
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值