如何在属性文件中创建bean初始化我的属性?

时间:2012-12-17 20:45:36

标签: spring properties constructor applicationcontext

我正在使用Spring 3.1.1.RELEASE。在我的应用程序上下文中,我正在尝试使用基于属性文件属性的构造函数来设置bean。这是我的申请背景......

    <!-- Define test properties  -->
    <util:properties id="applicationProperties" location="classpath:test.properties" />

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                    <list>
                            <value>classpath:test.properties</value>
                    </list>
            </property>
            <property name="ignoreUnresolvablePlaceholders" value="false"/>
    </bean>

    <bean id="myprojectClient" class="org.mainco.subco.myproject.myprojectClient">
    <constructor-arg value="${quickbase.username}" />
    <constructor-arg value="${quickbase.password}" />
</bean>

和我的test.properties文件的顶部......

# Quickbase credentials
quickbase.username=dalvarado-NONEMP@mainco.org
# It is expected that the password given here is encrypted.
quickbase.password=o37AbCdju57/nTWplI0oNg==

以下是我在JUnit测试中声明应用程序上下文文件的方法......

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:test-context.xml" })
public class TrainingAssignmentDaoTest extends AbstractTransactionalJUnit4SpringContextTests {

…

    @Autowired
    @Qualifier(value="applicationProperties")
    private Properties m_testProps; 

但我得到了这个例外,“无法解决占位符'quickbase.username'”。我需要做些什么才能获得我的房产?

    java.lang.IllegalStateException: Failed to load ApplicationContext
        at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
        at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'myprojectClient' defined in class path resource [test-context.xml]: Could not resolve placeholder 'quickbase.username'
        at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java: 209)
        at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:220)
        at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:84)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)  
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:656)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)
        at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:103)
        at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
        at org.springframework.test.context.support.DelegatingSmartContextLoader.loadContext(DelegatingSmartContextLoader.java:228)
        at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)
        at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
        ... 24 more

2 个答案:

答案 0 :(得分:0)

由于其他一切都很好,因此必须是您的属性文件不在类路径中。 我假设你是从单元测试中运行它,尝试使用这个虚拟测试,假设你的上下文xml文件名是testContext.xml

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:testContext.xml")
public class MyTest {

@Autowired
myprojectClient myprojectClient;

@Test
public void test()
{
    //use myProjectClient
}

}

这样您就可以使用从@ContextConfiguration中指定的文件初始化的Spring上下文开始测试。可以使用多个上下文文件。 还有一件事,使用大写字母表示类名。

答案 1 :(得分:0)

尝试将 ignoreUnresolvablePlaceholders 设置为true,然后重试。

<property name="ignoreUnresolvablePlaceholders" value="true"/>

您的应用程序中可能有多个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer(可能在您的应用程序上下文和测试上下文中)。