Spring Ioc 容器扩展点2 BeanFactoryPostProcessor自定义元数据配置

这个接口的语义与BeanPostProcessor类似,但有一处不同,BeanFactoryPostProcessor操作bean的元数据配置,也就是说,Spring IOC容器允许BeanFactoryPostProcessor来读取元数据配置并在容器实例化任何bean(除了BeanFactoryPostProcessor)之前修改他。

开发者可以配置多个BeanFactoryPostProcessor实例,也可以通过设置order属性来控制执行顺序。

注意:如果想要修改实际的bean实例,需要使用BeanPostProcessor,在BeanFactoryPostProcessor中使用这些Bean的实例虽然在技术上是可行的,但是这个会将bean过早的实例化,违反了标准的容器生命周期,同时也会导致一些问题,例如绕过bean的后置处理。

BeanFactoryPostProcessor会在整个容器内起作用,所以他仅仅与正在使用的容器相关。

但在ApplicationContext中声明时,bean工厂后置处理器会自动被执行,这样就可以对定义在容器中的元数据进行修改,Spring包含了一些预定义的bean工厂后置处理器,例如PropertyOverrideConfigurer和PropertyPlaceholderConfigurer。自定义的BeanFactoryPostProcessor也可以使用,例如注册自定义的属性编辑器。

例子:类名替换PropertyPlaceholderConfigurer

这样做可以使部署应用程序的人能够制定特定于环境的属性,如数据库URL和密码,而无需修改容器的主XML定义文件。

从使用了标准JavaProperties格式的bean定义的分离文件中,开发者可以使用PropertyPlaceholderConfigurer来具体化属性值。这样做可以使部署应用程序的用户能够定制特定于环境的属性,如数据库URL和密码,而无需修改容器的主要XML定义文件,从而降低了文件的复杂性和风险

考虑一下这个基于XML的元数据配置代码片段,这里的DataSource使用了占位符来定义,这个示例展示了从properties文件中配置属性的方法,在运行时,PropertyPlaceholderConfigurer就会用于元数据为数据源替换指定属性,指定属性的值换做为${property-name}形式中的占位符,这里运用了Ant/log4j/JSPEL的风格

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:com/foo/jdbc.properties"/>
    </bean>

    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="dirverClassName" value="${j}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

而真正的值是来自于标准的Java Properties格式的文件:

jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hslqdb:hsql://production:9002
jdbc.username=sa
jdbc.password=root

在上面的例子中,字符串${jdbc.username}在运行时会被sa替换,其它占位符也一样,所有匹配的属性文件都会使用键值替换占位符,PropertyPlaceholderConfigurer在bean定义的属性中会检查占位符,此外,对占位符还可以自定义前缀和后缀。

PropertyPlaceholderConfigurer不仅仅会查看在Properties文件中指定的属性,默认情况下,如果他在指定的属性文件中找不到属性,那么他会检查Java System的属性,开发者可以通过设置systemPropertiesMode属性,使用下面三个整数的其中一个来自定义这种行为:

never(0)从不检查系统属性

fallback(1)如果没有在指定的属性文件中解析到属性,那么就检查系统属性(默认)

override(2)在检查指定的属性文件之前,首先去检查系统属性,允许系统属性覆盖其它任意的属性资源

你可以使用PropertyPlaceholderConfigurer来替换类名,在开发者在运行时需要选择某个特定的实现类时,这个很有用。

例子:修改属性的值

<?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 id="person" class="com.test.entity.Person">
        <property name="name" value="fang"></property>
        <property name="age" value="10"></property>
        <property name="birthday" value="10"></property>
    </bean>

    <bean id="beanFactoryPostTest" class="com.test.config.BeanFactoryPostTest"></bean>
</beans>
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Person {
    private Integer age;
    private String name;
    private String birthday;

}
public class BeanFactoryPostTest implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        String[] beanStr = beanFactory
                .getBeanDefinitionNames();
        for (String beanName : beanStr) {
            if ("person".equals(beanName)) {
                BeanDefinition beanDefinition = beanFactory
                        .getBeanDefinition(beanName);
                MutablePropertyValues m = beanDefinition.getPropertyValues();
                if (m.contains("birthday")) {
                    try {
                        m.addPropertyValue("birthday", "世界和平");
                    } catch (Exception e) {
                        System.out.println("错误");
                    }
                }
            }
        }
    }
}
@GetMapping("/test")
    public Object get() {
        ApplicationContext factory = new ClassPathXmlApplicationContext("classpath:Test.xml");
        return factory.getBean("person", Person.class);
    }

可以看到person的birthday的属性值被修改了,使用BeanPostProcessor里面也能够实现此功能。

例子 PropertyOverrideConfigurer

PropertyOverrideConfigurer 是另一种bean工厂后置处理器,类似于PropertyPlaceholderConfigurer但是与其不同的是,对于所有的bean属性,原始定义可能有默认值也可能没有值,如果一个properties覆盖文件没有特定的bean属性,这就会使用默认的上下文定义。

注意bean定义是不知道是否被覆盖的,所以从XML定义文件中不能马上看到那个配置文件正在被使用,在拥有多个PropertyOverrideConfigurer实例的情况下,为相同的bean的属性定义不同的值,基于覆盖机制,只会最后一个生效。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值