spring 第7天 Bean,BeanFactory处理器,配置器

本文介绍了Spring框架中的两种后处理器:Bean后处理器与容器后处理器。Bean后处理器可以在Bean初始化前后进行增强处理,而容器后处理器则可以对BeanFactory进行定制化配置。此外,还详细解释了如何使用属性占位符配置器和重写占位符配置器。

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

spring 两种后处理器
第一种,Bean 后处理器
对容器中bean进行处理,对bean的功能进行额外的增强

package cn.sh.processor;

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

import cn.sh.springmvc.applicationContextAware.MyContent;
import cn.sh.springmvc_java.American;

/**
 * Spring Bean的后置处器
 * @author Bin
 *
 */
public class MyBeanPostProcessor implements BeanPostProcessor {
	
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		
		System.out.println("Bean后处理器在初始化之前对"+beanName+"进行增强处理....");
		//bean 容器中的Bean
		//返回的Bean 可以和原Bean截然不同
		return bean;
	}

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

		System.out.println("Bean后处理器在初始化之后对"+beanName+"进行增强处理...");
		if(bean instanceof MyContent){
			System.out.println("修改BeanName的名字");
			MyContent a=(MyContent)bean;
			a.setBeanName("Struts2权威指南");
		}
		return bean;
	}

}
  <!--Bean 后置处理器  -->
   <bean id="beanPostProcessor" class="cn.sh.processor.MyBeanPostProcessor"/>


//如果采用ApplicationContext 作为容器,不需要手动注入Bean后处理器
//测试 BeanPostProcessor 
	@Test
	public void test13() {  
		AbstractApplicationContext act=new ClassPathXmlApplicationContext("classpath*:applicationContent.xml");
		MyContent p=act.getBean("ZG",MyContent.class);
		//获取容器
		System.out.println(p.getContext());
		System.out.println(act==p.getContext());

		//获取配置的bean id
		System.out.println("BeanName:"+p.getBeanName());
		//销毁
		act.registerShutdownHook();
	}
//如果采用 BeanFactory 作为spring的容器,需要手动注入bean后处理器
@Test
	public void test18() { 
		ClassPathResource isr=new ClassPathResource("applicationContent.xml");
		XmlBeanFactory beanFactory=new XmlBeanFactory(isr);
		MyBeanPostProcessor proc=beanFactory.getBean("beanPostProcessor", MyBeanPostProcessor.class);
		//如果采用beanfactory, bean后处理器必须显示的注入到 factory中
		beanFactory.addBeanPostProcessor(proc);
		
		MyContent p=beanFactory.getBean("ZG",MyContent.class);
		//获取容器
		System.out.println(p.getContext());
		//获取配置的bean id
		System.out.println("BeanName:"+p.getBeanName());
		
	}



容器后处理器

package cn.sh.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.Ordered;

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor,Ordered {

	@Override
	public void postProcessBeanFactory(
			ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("程序对Spring所做的BeanFactory的初始化没有改变..");
		System.out.println("Spring容器是:"+beanFactory);

	}

	@Override
	public int getOrder() {
		System.out.println("采用Ordered 来指定 后处理器的order属性");
		return 0;
	}
	

}

<bean id="beanFactoryProcessor" class="cn.sh.processor.MyBeanFactoryPostProcessor"/>


//如果采用 BeanFactory 作为spring的容器,需要手动注入beanFactory后处理器
	@Test
	public void test19() { 
		ClassPathResource isr=new ClassPathResource("applicationContent.xml");
		XmlBeanFactory beanFactory=new XmlBeanFactory(isr);
		MyBeanFactoryPostProcessor bfprocessor=beanFactory.getBean("beanFactoryProcessor", MyBeanFactoryPostProcessor.class);
		bfprocessor.postProcessBeanFactory(beanFactory);
		
		MyContent p=beanFactory.getBean("ZG",MyContent.class);
		//获取容器
		System.out.println(p.getContext());
		//获取配置的bean id
		System.out.println("BeanName:"+p.getBeanName());
		
	}
	
//如果采用 ApplicationContext作为spring的容器,则不用进行手动注入


spring已经提供了如下几个容器后处理器

PropertyPlaceHolderConfigurer:属性占位符配置器
PropertyOverrideConfigurer:重写占位符配置器
CustomAutowireConfigurer:自定义自动装配的配置器
CustomScopeConfigurer:自定义作用配置器


PropertyPlaceHolderConfigurer 属性占位符配置器

<!--两种配置方式 属性占位符 后处理器  如果导入了context Schema 可以用cotent: -->
   <!--  <context:property-placeholder location="classpath:dbconn.properties"/> -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	<property name="locations">
    		<list>
    			<value>dbconn.properties</value>
    		</list>
    	</property>
    </bean>
   <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    	<property name="driverClass" value="${jdbc.driverClassName}"/>
    	<property name="jdbcUrl" value="${jdbc.url}"/>
    	<property name="user" value="${jdbc.username}"/>
    	<property name="password" value="${jdbc.password}"/>
    	<property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
    	<property name="minPoolSize" value="${jdbc.minPoolSize}"/>
    	<property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
    	<property name="MaxIdleTime" value="${jdbc.MaxIdleTime}"/>
    </bean>

dbconn.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mytest
jdbc.username=root
jdbc.password=123456
jdbc.maxPoolSize=40
jdbc.minPoolSize=1
jdbc.initialPoolSize=1
jdbc.MaxIdleTime=20


PropertyOverrideConfigurer:重写占位符配置器
  <!-- 采用 重写 占位符配置器  
   	发现:  重写 占位符  可以覆盖 属性占位符  中的值
   	两种配置方式
   -->
   <context:property-override location="classpath:db.properties"/>
   
   <!-- <bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
    	<property name="locations">
    		<list>
    			<value>db.properties</value>
    		</list>
    	</property>
    </bean> -->
    
    
    <!--采用 重写 占位符后 就可以不用 指定 属性了 -->
  <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
   </bean>

db.properties

#beanName.property=value 的命名格式 
datasource.driverClass=com.mysql.jdbc.Driver
datasource.jdbcUrl=jdbc:mysql://localhost:3306/mytest
datasource.user=root
datasource.password=123456
datasource.maxPoolSize=40
datasource.minPoolSize=1
datasource.initialPoolSize=1
datasource.MaxIdleTime=20

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值