将settings.xml中的属性设置为spring.xml
我在user_directory/.m2
文件夹中有一个settings.xml文件。我在settings.xml
中设置了一个属性。我想要它在spring.xml
中访问它。将settings.xml中的属性设置为spring.xml
Setting.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>
如果我要创建test.properties
文件中的src/main/resources文件夹。
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是
异常在线程 “主” org.springframework.beans.factory.BeanInitializationException:可能 不加载属性;嵌套的例外是 java.io.FileNotFoundException:类路径资源 [来源/主/资源/ test.properties]不能打开,因为它 不存在
这是怎么回事我wrong.How可访问性从settings.xml
到spring.xml
。
您错误配置了您的财产占位符。的src/main /资源是不是在你的classpath,你就应该把这样的:
<context:property-placeholder location="classpath:test.properties"/>
为了您的上下文的配置,你可以:
一个。直接过滤您的春天背景:
<bean class="java.lang.String" id="nameTest">
<constructor-arg value="${testName}"/>
</bean>
b。或者过滤你的test.properties配置文件,然后在你的spring中注入它作为属性占位符。XML:
test.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>
几点我看到:
-
的
property-placeholder
位置属性是指类路径上的一个文件,但是你想使用一个文件的文件系统上,因此它必须是这样的:<context:property-placeholder location="file:///user_directory/.m2/settings.properties"/>
-
您的设置文件是XML。默认情况下,预期实际上是一个Java属性格式的文件。可能有许多方法可以使用自定义XML,但我并不熟悉这一点。所以,你的XML文件将转化为这样的事情:
profile.id = default profile.activation.activateByDefault = true profile.properties.testName = Test ...
当后来在引用您的属性你
spring.xml
您只需使用${profile.id}
的ID值从settings.properties
文件放置。