【问题标题】:How do I create a bean initialized my properties in my properties file?如何在我的属性文件中创建一个初始化我的属性的 bean?
【发布时间】:2012-12-05 00:08:28
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: spring properties constructor applicationcontext


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      既然其他一切都很好,那一定是你的属性文件不在类路径中。 我假设您从单元测试中运行它,尝试使用这个虚拟测试,假设您的上下文 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 上下文开始测试。可以使用多个上下文文件。 还有一件事,使用大写字母作为类名。

      【讨论】:

      • 嗨,我同意存在一些类路径问题,但我在 spring 上下文文件中有这个声明() (请参阅我的编辑)并且属性文件的自动装配工作得很好。至于您建议的 JUnit 配置,我完全按照您指定的方式进行操作(另请参阅我的编辑)。
      • 好吧,除了它不在类路径上之外,我没有别的想法 - 我的意思是物理上 - 它的确切位置(在文件系统中),你用什么构建?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      相关资源
      最近更新 更多