0

我正在使用 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 文件的顶部……</p>

# 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
4

2 回答 2

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
}

}

通过这种方式,您可以使用 Spring 上下文开始测试,该上下文从@ContextConfiguration. 可以使用多个上下文文件。还有一件事,使用大写字母作为类名。

于 2012-12-17T21:13:22.977 回答
0

尝试将ignoreUnresolvablePlaceholders设置为 true 并重试。

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

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

于 2012-12-18T09:45:59.077 回答