我在 user_directory/.m2
文件夹中有一个 settings.xml 文件。我在 settings.xml
中设置了一个属性。我希望它在 spring.xml
中访问它。
设置.xml
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<testName>Test</testName>
</properties>
</profile>
</profiles>
在我写的pom.xml中
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
我是否必须在 src/main/resources 文件夹中创建 test.properties
文件。
name = ${testName}
在 spring.xml
中,我将其用作
<context:property-placeholder location="classpath:src/main/resources/test.properties"/>
<bean class="java.lang.String" id="nameTest">
<constructor-arg value="name"/>
</bean>
当run.Exception是
Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [src/main/resources/test.properties] cannot be opened because it does not exist
出了什么问题。如何访问从 settings.xml
到 spring.xml
的属性。
最佳答案
您错误配置了属性占位符。 src/main/resource 不在你的类路径中,你应该放这样的东西:
<context:property-placeholder location="classpath:test.properties"/>
对于上下文的配置,您可以:
一个。直接过滤你的 spring 上下文:
<bean class="java.lang.String" id="nameTest">
<constructor-arg value="${testName}"/>
</bean>
或者过滤您的 test.properties 配置文件,然后将其作为属性占位符注入(inject)到您的 spring.xml 中:
测试.properties:
spring.testName=${testName}
spring.xml:
<context:property-placeholder location="classpath:test.properties"/>
<bean class="java.lang.String" id="nameTest">
<constructor-arg value="${spring.testName}"/>
</bean>
关于java - 将 settings.xml 中设置的属性访问到 spring.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22109869/