所谓的容器处理器就是对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