038容器处理器

所谓的容器处理器就是对Spirng容器(一般是ApplicationContext)进行处理

容器处理器是一个实现了BeanFactoryPostProcessor接口的bean
BeanFactoryPostProcessor中有方法:

  • void postProcessBeanFactory(ConfigurableListableBeanFactory var1)
    该方法需要重写,参数var1就是容器,可对其进行处理。

配置容器处理器很简单,只需要一行

<bean class="容器处理器的全限定类名" />

一般容器不需要额外处理,因为Spring容器已经很优秀

Spring内置了一些容器处理器,比较常用的有:

  • PropertyPlaceholderConfigurer: 属性占位符配置器
  • PropertyOverrideConfigurer: 重写占位符配置器
  • CustomAutowireConfigurer: 自定义自动装配的配置器
  • ConstomScopeConfigurer:自定义作用域的配置器

PropertyPlaceholderConfigurer使用示例:
beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>
<!--注册容器处理器PropertyPlaceholderConfigurer
	location:指定配置文件
	-->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="classpath:db.properties"
    />
    <!--也可以简化写成-->
<!--	<context:property-placeholder location="classpath:db.properties"/>	-->
    
    <bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    p:driverClass="${driver}"
    p:jdbcUrl="${url}"
    p:user="${user}"
    p:password="${pass}"
    p:maxPoolSize="${maxSize}"
    p:minPoolSize="${minSize}"/>
</beans>

db.properties

url=jdbc:mysql://localhost:3306/javaweb
driver=com.mysql.jdbc.Driver
user=root
pass=123456
maxSize=30
minSize=2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值