spring 容器扩展之BeanPostProcessor

本文介绍Spring框架中的BeanPostProcessor接口,该接口允许开发者在Bean初始化前后进行操作,如修改Bean属性值或实现动态代理。通过具体代码示例展示了如何实现并应用BeanPostProcessor。

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

                      spring 容器扩展之BeanPostProcessor

spring的BeanPostProcessor接口给开发者提供了一个容器扩展的入口,

在Spring容器完成Bean实例化和属性设置后,并且在bean调用初始化方法之前或之后。因此BeanPostProcessor(Bean后置处理器)常用在:对bean内部的值进行修改;实现Bean的动态代理等。

可以定义一个或者多个BeanPostProcessor接口的实现,然后注册到容器中。那么该容器里管控的所有Bean在调用初始化方法之前或之后,都会调用BeanPostProcessor接口中对应的方法。

看代码

package com.spring.model;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class Person implements DisposableBean, InitializingBean {

	private String name;

	Person() {
		System.out.println("Constructor of person bean is invoked!");
	}

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

	//Bean initialization code
	public void afterPropertiesSet() throws Exception {
		System.out.println("Initializing method of person bean is invoked!");
	}

	
	//Bean destruction code
	public void destroy() throws Exception {
		System.out.println("Destroy method of person bean is invoked!");
	}

	public void customDestroy() throws Exception {
		System.out.println("Custom destroy method of person bean is invoked!");
	}

	public void customInit() throws Exception {
		System.out.println("Custom initializing method of person bean is invoked!");
	}
}
package com.spring.model;

public class Student {
	String id;
	String name;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
package com.spring.main;

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

import com.spring.model.Person;
import com.spring.model.Student;

public class Demoapp {

	public static void main(String[] args) {

		// Reading configuration from the spring configuration file.
		ConfigurableApplicationContext   context = new ClassPathXmlApplicationContext("spring-config.xml");

		Person myperson = context.getBean("personBean", Person.class);
		Student student = context.getBean("student", Student.class);
		System.out.println("student id= " + student.getId());
		System.out.println("Name= " + myperson.getName());

		// Closing the context object.
		context.close();
	}
}
package com.spring.processer;

import org.springframework.beans.factory.config.BeanPostProcessor;

import com.spring.model.Student;

public class InstantiationTracingBeanPostProcessor implements  BeanPostProcessor{
	// simply return the instantiated bean as-is
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        System.out.println("Bean '" + beanName + "' before created : " + bean.toString());
        if(Student.class.isInstance(bean)){
        	Student student = (Student) bean;
        	student.setId("111");
        }
        return bean; // we could potentially return any object reference here...
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) {
        System.out.println("Bean '" + beanName + "' created : " + bean.toString());
        return bean;
    }
}

xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- <bean name="personBean" class="com.spring.model.Person"> <property 
		name="name" value="Jason Clarke" /> </bean> -->
	<bean name="personBean" class="com.spring.model.Person"
		init-method="customInit" destroy-method="customDestroy">
		<property name="name" value="Jason Clarke" />
	</bean>
	
	<bean name="student" class="com.spring.model.Student" />

	<!-- when the above bean (messenger) is instantiated, this custom BeanPostProcessor 
		implementation will output the fact to the system console -->
	<bean class="com.spring.processer.InstantiationTracingBeanPostProcessor" />
</beans>

spring中的RequiredAnnotationBeanPostProcessor就是实现了BeanPostProcessor的这么一个类,它的作用是用来保证java bean中使用了注解标签的属性的都已经被注入依赖。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值