本文介绍了Unitils:如何从Spring获取数据库属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Unitils 和 Spring 进行单元测试.我已经使用属性文件为 Spring 配置了数据源.

I am using Unitils with Spring for unit testing. I've configured Spring with datasource using a properties file.

我的问题是如何为 Unitils 使用相同的数据源或相同的属性?

My question is how can I use the same datasource or the same properties for Unitils?

Unitils 需要类路径 unitils.properties 中的文件,其中包含 url、用户、密码和驱动程序等数据库配置参数.

Unitils expects a file in the classpath unitils.properties with database configuration parameters like url, user, password and driver.

我尝试使用 Spring 配置中使用的属性来配置 Unitils,如下所示,但它不起作用.

I've tried to configure Unitils using the properties used in the Spring configuration as below but it is not working.

database.driverClassName=${jdbc.driver.class}

谢谢,阿迪

推荐答案

Ryan 的回答是正确且有帮助的,尽管我使用了不同的方法.

Ryan answer is correct and helpful as well though I've used different approach.

我扩展了类 PropertiesDataSourceFactory 并重写了如下方法:

I extended the class PropertiesDataSourceFactory ro override the methods as follows:

public class UnitilsDataSourceFactory extends PropertiesDataSourceFactory {

    @Override
    public void init(Properties configuration) {
        try {
            String[] configFiles = new String[] { "applicationContext-test.xml" };
            BeanFactory factory = new ClassPathXmlApplicationContext(configFiles);

            SystemPropertiesReader systemPropertiesReader = (SystemPropertiesReader) factory.getBean("systemPropertiesReader");
            Properties loadProperties = systemPropertiesReader.loadProperties();

            super.init(loadProperties);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public DataSource createDataSource() {
        DataSource dataSource = super.createDataSource();
        return dataSource;
    }

}

并且还编写了一个 SystemPropertiesReader 为:

and also wrote a SystemPropertiesReader as:

public class SystemPropertiesReader {

    private Collection<Resource> resources;

    public void setResources(final Collection<Resource> resources) {
        this.resources = resources;
    }

    public void setResource(final Resource resource) {
        resources = Collections.singleton(resource);
    }

    @PostConstruct
    public Properties loadProperties() throws Exception {
        final Properties systemProperties = System.getProperties();
        for (final Resource resource : resources) {
            final InputStream inputStream = resource.getInputStream();
            try {
                systemProperties.load(inputStream);
            } finally {
                //
            }
        }

        return systemProperties;
    }

}

并添加了一个带有属性文件的bean:

and added a bean with the properties file:

<bean id="systemPropertiesReader" class="uk.co.friendslife.eventmanager.domain.dao.SystemPropertiesReader">
            <property name="resource">
                <value>classpath:/META-INF/em/config/eventmanager_${database_name_lower}.properties</value>
            </property>
</bean>

将以下内容添加到 unitils.properties:

add the following to unitils.properties:

org.unitils.database.config.DataSourceFactory.implClassName=x.y.UnitilsDataSourceFactory

这篇关于Unitils:如何从Spring获取数据库属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 23:40