无法解析 XML-config spring 应用中的占位符

Could not resolve placeholder in XML-config spring app

我是 spring 的初学者。我在 SO 上阅读了许多类似的标题问题,要么它们不适合我,要么不是那种类型的配置,我不明白哪里出了问题。

我正在使用 Gradle 在 Intellij Idea 中编写一个简单的 spring 应用程序。一切都运行良好,直到我决定从 .properties 文件中读取一些值。 xml-config 文件和 .properties 文件都在资源中。

我创建了一个简单的 junit 测试。 LoginService 的实现是有效的,因为在 xml 中硬编码电子邮件和密码时,测试已经通过。

堆栈跟踪:

Testing started at 19:08 ...
19:08:27: Executing tasks ':cleanTest :test --tests MyDocumentsLoginTest'...

> Task :cleanTest
> Task :compileJava UP-TO-DATE
> Task :compileGroovy NO-SOURCE
> Task :processResources
> Task :classes
> Task :compileTestJava UP-TO-DATE
> Task :compileTestGroovy NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test FAILED
lip 15, 2019 7:08:29 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6d2d086b: startup date [Mon Jul 15 19:08:29 CEST 2019]; root of context hierarchy
lip 15, 2019 7:08:29 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/spring/MyDocuments-config.xml]
lip 15, 2019 7:08:29 PM org.springframework.beans.factory.config.PropertyPlaceholderConfigurer loadProperties
INFO: Loading properties file from class path resource [META-INF/data/env_dev.properties]

Invalid bean definition with name 'login' defined in class path resource [META-INF/spring/MyDocuments-config.xml]: Could not resolve placeholder 'user.email' in string value "${user.email}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'user.email' in string value "${user.email}"
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'login' defined in class path resource [META-INF/spring/MyDocuments-config.xml]: Could not resolve placeholder 'user.email' in string value "${user.email}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'user.email' in string value "${user.email}"
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:211)
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:223)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at MyDocumentsLoginTest.setup(MyDocumentsLoginTest.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:106)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
    at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:66)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:117)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:155)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:137)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.ManagedExecutorImpl.run(ManagedExecutorImpl.java:46)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'user.email' in string value "${user.email}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:259)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:204)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82)
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:208)
    ... 56 more

MyDocumentsLoginTest > testLogin FAILED
    org.springframework.beans.factory.BeanDefinitionStoreException at MyDocumentsLoginTest.java:19
        Caused by: java.lang.IllegalArgumentException at MyDocumentsLoginTest.java:19
1 test completed, 1 failed
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.

我的xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">


    <bean id="environmentProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:META-INF/data/env_dev.properties" />
    </bean>

    <bean id="login" class="com.doszke.isf.java.spring.service.LoginService">
        <property name="username" value="${user.email}"/>
        <property name="password" value="${user.password}"/>
    </bean>


    <context:component-scan base-package="com.doszke.isf.java" />

    <bean id="engine" class="com.doszke.isf.java.service.SearchEngineService">
        <property name="documentDAO" ref="documentDAO"/>
    </bean>

    <bean id="documentDAO" class="com.doszke.isf.java.spring.service.DocumentRepository">
        <property name="documents">
            <list>
                <ref bean="doc1"/>
                <ref bean="doc2"/>
                <ref bean="doc3"/>
                <ref bean="doc4"/>
            </list>
        </property>
    </bean>

    <bean id="typeDAO" class="com.doszke.isf.java.spring.service.TypeDataRepository">
        <property name="types">
            <map>
                <entry key="webType" value-ref="webType"/>
                <entry key="noteType" value-ref="noteType"/>
                <entry key="pdfType" value-ref="pdfType"/>
            </map>
        </property>
    </bean>

    <!--Document-->
    <bean id="doc1" class="com.doszke.isf.java.model.Document">
        <property name="name" value="Szablon książki" />
        <property name="type" ref="pdfType" />
        <property name="location" value="/Users/doszke/Documents/random/book template.pdf" />
    </bean>

    <bean id="doc2" class="com.doszke.isf.java.model.Document">
        <property name="name" value="Przykładowa umowa" />
        <property name="type" ref="pdfType" />
        <property name="location" value="/Users/doszke/Documents/Contracts/sample contract.pdf" />
    </bean>

    <bean id="doc3" class="com.doszke.isf.java.model.Document">
        <property name="name" value="Moje notatki" />
        <property name="type" ref="noteType" />
        <property name="location" value="/Users/doszke/Documents/random/my note.txt" />
    </bean>

    <bean id="doc4" class="com.doszke.isf.java.model.Document">
        <property name="name" value="Pro Spring Security Book" />
        <property name="type" ref="webType" />
        <property name="location" value="http://www.apress.com/9781430248187" />
    </bean>

    <!--Type-->
    <bean id="webType" class="com.doszke.isf.java.model.Type">
        <property name="name" value="WEB" />
        <property name="desc" value="Łącze sieciowe" />
        <property name="extension" value=".url" />
    </bean>

    <bean id="pdfType" class="com.doszke.isf.java.model.Type">
        <property name="name" value="PDF" />
        <property name="desc" value="Portable Document Format" />
        <property name="extension" value=".pdf" />
    </bean>

    <bean id="noteType" class="com.doszke.isf.java.model.Type">
        <property name="name" value="NOTE" />
        <property name="desc" value="Notatki tekstowe" />
        <property name="extension" value=".txt" />
    </bean>

</beans>

测试class(展示我如何阅读xml):

import com.doszke.isf.java.spring.service.Login;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.Assert.assertNotNull;

public class MyDocumentsLoginTest {


    private static final String EMAIL = "test@mydocuments.com";
    private static final String PASS = "test123";
    private static final String SUCCESS = "Authorized";
    private static final String FAILURE = "Authorization failed";
    private ClassPathXmlApplicationContext context;

    @Before
    public void setup(){
        context = new ClassPathXmlApplicationContext("META-INF/spring/MyDocuments-config.xml");
    }

    @Test
    public void testLogin(){
        Login login = context.getBean(Login.class);
        assertNotNull(login);
        System.out.println(login.isAuthorized(EMAIL, PASS) ? SUCCESS : FAILURE);
    }

}

我的项目结构是这样的:

@更新: env_dev.properties 文件:

user.email=test@mydocuments.com
user.password=test123

applicationContext.xml中,改为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">


    <!-- <bean id="environmentProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:META-INF/data/env_dev.properties" /></bean>-->

    <!-- Add this instead -->
    <context:property-placeholder location="classpath:env_dev.properties" />

    <bean id="login" class="com.doszke.isf.java.spring.service.LoginService">
        <property name="username" value="${user.email}"/>
        <property name="password" value="${user.password}"/>
    </bean>


    <context:component-scan base-package="com.doszke.isf.java" />

    <bean id="engine" class="com.doszke.isf.java.service.SearchEngineService">
        <property name="documentDAO" ref="documentDAO"/>
    </bean>

    <bean id="documentDAO" class="com.doszke.isf.java.spring.service.DocumentRepository">
        <property name="documents">
            <list>
                <ref bean="doc1"/>
                <ref bean="doc2"/>
                <ref bean="doc3"/>
                <ref bean="doc4"/>
            </list>
        </property>
    </bean>

    <bean id="typeDAO" class="com.doszke.isf.java.spring.service.TypeDataRepository">
        <property name="types">
            <map>
                <entry key="webType" value-ref="webType"/>
                <entry key="noteType" value-ref="noteType"/>
                <entry key="pdfType" value-ref="pdfType"/>
            </map>
        </property>
    </bean>

    <!--Document-->
    <bean id="doc1" class="com.doszke.isf.java.model.Document">
        <property name="name" value="Szablon książki" />
        <property name="type" ref="pdfType" />
        <property name="location" value="/Users/doszke/Documents/random/book template.pdf" />
    </bean>

    <bean id="doc2" class="com.doszke.isf.java.model.Document">
        <property name="name" value="Przykładowa umowa" />
        <property name="type" ref="pdfType" />
        <property name="location" value="/Users/doszke/Documents/Contracts/sample contract.pdf" />
    </bean>

    <bean id="doc3" class="com.doszke.isf.java.model.Document">
        <property name="name" value="Moje notatki" />
        <property name="type" ref="noteType" />
        <property name="location" value="/Users/doszke/Documents/random/my note.txt" />
    </bean>

    <bean id="doc4" class="com.doszke.isf.java.model.Document">
        <property name="name" value="Pro Spring Security Book" />
        <property name="type" ref="webType" />
        <property name="location" value="http://www.apress.com/9781430248187" />
    </bean>

    <!--Type-->
    <bean id="webType" class="com.doszke.isf.java.model.Type">
        <property name="name" value="WEB" />
        <property name="desc" value="Łącze sieciowe" />
        <property name="extension" value=".url" />
    </bean>

    <bean id="pdfType" class="com.doszke.isf.java.model.Type">
        <property name="name" value="PDF" />
        <property name="desc" value="Portable Document Format" />
        <property name="extension" value=".pdf" />
    </bean>

    <bean id="noteType" class="com.doszke.isf.java.model.Type">
        <property name="name" value="NOTE" />
        <property name="desc" value="Notatki tekstowe" />
        <property name="extension" value=".txt" />
    </bean>

</beans>

这绝对可以解决问题。请将资源文件夹添加到类路径。