Java,Spring - 记录Bean属性

时间:2009-10-21 17:31:42

标签: java spring logging

说我有以下bean定义:

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

有没有办法让Spring记录它已设置的所有属性(不在log4j中为Spring启用详细日志记录)。我正想着...... verbose =“true”

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" verbose="true">

更新

我使用了以下内容,如答案所示:

public class SpringBeanLoggingPostProcessor implements BeanPostProcessor {
    private static final Logger log = Logger.getLogger(SpringBeanLoggingPostProcessor.class);
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        if (bean.getClass() == org.apache.commons.dbcp.BasicDataSource.class) {
            BasicDataSource ds = (BasicDataSource) bean;
            log.info("url="+ds.getUrl());
            log.info("username="+ds.getUsername());
        }

        return bean;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String arg1) throws BeansException {
        return bean;
    }
}

1 个答案:

答案 0 :(得分:2)

不,没有这样的设施。您可以创建一个专用的bean后处理器来记录目标bean属性。