spring框架源码十三、spring ioc高级特性-后置处理器

spring提供了两种后置处理bean的扩展接口,
分别为BeanPostProcessor和BeanFactoryPostProcessor,

BeanPostProcessor

BeanPostProcessor是针对bean的处理,

BeanPostProcessor

public interface BeanPostProcessor {

	/**
	 * bean初始化之前
	 */
	@Nullable
	default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	/**
	 * bean初始化之后
	 */
	@Nullable
	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

}

BeanPostProcessor提供了两个方法,分别在初始化方法之前和之后执行,
这里的初始化方法就是bean的init-method。

如果我们定义了一个BeanPostProcessor的实现类,默认会对spring ioc容器中的所有bean进行处理,
如果要对某个具体的bean进行处理,
可以通过方法的参数(Object bean, String beanName)来区分,Object bean是bean的实例,String beanName是我们bean定义的id或name属性值。

实例

MyBeanPostProcessor

package com.duohoob.spring.processor;

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

/**
 * @author yangwei
 * @date 2022年10月26日
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		if ("testService".equals(beanName)) {
			System.out.println("init-method之前");
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		if ("testService".equals(beanName)) {
			System.out.println("init-method之后");
		}
		return bean;
	}

}

application-context.xml

<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: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/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"
       default-lazy-init="false">

	<!-- 扫描注解类 -->
	<context:component-scan base-package="com.example.duohoob"/>

	<!-- 引入外部配置 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>

	<!-- 第三方jar包中的bean -->
	<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	
    <bean id="testService" class="com.example.duohoob.service.TestServiceImpl" init-method="init">
    	<property name="dataSource" ref="druidDataSource"/>
    </bean>

</beans>

TestServiceImpl

package com.example.duohoob.service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.stereotype.Component;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * @Title: TestServiceImpl.java
 * @Package com.example.duohoob.service
 *
 * @author yangwei
 * @date 2022年10月21日
 */
@Component("testService")
public class TestServiceImpl implements TestService {

//	@Autowired
	private DruidDataSource dataSource;

	public void setDataSource(DruidDataSource dataSource) {
		this.dataSource = dataSource;
	}

	@PostConstruct
	public void init() {
		System.out.println("创建bean");
	}
	
	@PreDestroy
	public void destroy() {
		System.out.println("销毁bean");
	}
	
	@Override
	public String test() {
		// TODO Auto-generated method stub
		System.out.println(dataSource);
		return "success";
	}

}

测试

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-context.xml");
		Object bean = context.getBean("testService");
		System.out.println(bean);
		((AbstractApplicationContext) context).close();
	}
	

在这里插入图片描述

BeanFactoryPostProcessor

BeanFactoryPostProcessor是BeanFactory级别的处理,
在工厂产生之后、对象创建之前执行,

它只有一个方法,

void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

参数为ConfigurableListableBeanFactory,该类型定义了一些方法,
其中有个getBeanDefinition可以获取我们的beandefinition,然后可以对属性进行修改,

应用案例:PlaceholderConfigurerSupport,

它的作用就是读取外部的配置,

<context:property-placeholder location="classpath:jdbc.properties"/>

然后替换配置中的占位符,

<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值