Java 具有Maven属性的Spring属性PlaceHolderConfiguration
我在我的web应用程序中显示一些版本元数据,我使用Maven resources过滤器来更新我的一个属性文件Java 具有Maven属性的Spring属性PlaceHolderConfiguration,java,spring,maven,properties,property-placeholder,Java,Spring,Maven,Properties,Property Placeholder,我在我的web应用程序中显示一些版本元数据,我使用Maven resources过滤器来更新我的一个属性文件 <resources> <resource> <directory>${basedir}/src/main/resources</directory> <filtering>true</filtering> <inc
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>build-info.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>build-info.properties</exclude>
</excludes>
</resource>
</resources>
这一切都很好,当我打包应用程序时,适当的信息会添加到资源中
我的问题是,我试图使用Spring PropertyPlaceHolderConfigure将包含版本信息的bean连接在一起,但这与${}
语法不同。如果没有一个难看的解决方法,我将无法在本地运行应用程序。我得到以下异常:
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'com.mycompany.abc.dm.system.BuildInfo
...
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'project.version' in string value "${project.version}"
是否使用PropertyPlaceHolderConfigure解决了这一问题
下面是来自SpringXML上下文的片段
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:build-info.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="1000" />
</bean>
<bean class="com.mycompany.abc.dm.system.BuildInfo">
<constructor-arg index="0" value="${build.version}"/>
<constructor-arg index="1" value="${build.number}"/>
<constructor-arg index="2" value="${build.commit}"/>
</bean>
类路径:build-info.properties
“解决方案”
我从未使用PropertyPlaceHolderConfigure找到解决此问题的方法。我的解决方案是不使用PropertyPlaceHolderConfigure。我仍然在阅读属性文件,只是没有使用Spring magic。为什么您认为
忽略UnsolvablePlaceholders
是一个丑陋的解决方法?我很困惑,您是否让Maven直接过滤Spring XML文件(我认为这是正确的方法——只需为此修改pom.XML即可),或者您正试图让Spring XML文件读取build-info.properties文件中的解析值?@M4ver1k ignoreunsolvablepholders在这里不起作用。如果这是解决办法,我会很高兴的。我知道的解决方法包括创建另一个具有类路径的属性文件preference@GlenMazza我尝试直接过滤SpringXML文件,但仍然面临同样的问题。这些属性在部署之前不会被替换,因此Spring在启动时仍然失败,因为它无法替换bean属性。就我的目标而言,这只是加载spring应用程序上下文。我不在乎在本地运行时是否解决了这个构建信息,我仍然建议配置Maven直接过滤SpringXML文件。这很容易被看作是Maven的问题--如何配置Maven将值过滤到XML文件中--而不是Spring的问题,如何让Spring将配置文件过滤到XML文件中。为什么你认为ignoreUnsolvablePlaceholder
是一个丑陋的解决方法?我很困惑,您是让Maven直接过滤Spring XML文件(我认为这是正确的方法——只需修改pom.XML即可),还是让Spring XML文件读取build-info.properties文件中的解析值?@M4ver1k ignoreunsolvablepholders在这里不起作用。如果这是解决办法,我会很高兴的。我知道的解决方法包括创建另一个具有类路径的属性文件preference@GlenMazza我尝试直接过滤SpringXML文件,但仍然面临同样的问题。这些属性在部署之前不会被替换,因此Spring在启动时仍然失败,因为它无法替换bean属性。就我的目标而言,这只是加载spring应用程序上下文。我不在乎在本地运行时是否解决了这个构建信息,我仍然建议配置Maven直接过滤SpringXML文件。这最容易被看作是Maven问题——如何配置Maven将值过滤到XML文件中——而不是Spring问题,如何让Spring将配置文件过滤到XML文件中。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:build-info.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="1000" />
</bean>
<bean class="com.mycompany.abc.dm.system.BuildInfo">
<constructor-arg index="0" value="${build.version}"/>
<constructor-arg index="1" value="${build.number}"/>
<constructor-arg index="2" value="${build.commit}"/>
</bean>