Spring运行可执行jar时的循环占位符引用

时间:2012-07-18 22:08:06

标签: java spring maven maven-shade-plugin

尝试运行可执行jar文件时,我遇到“循环占位符引用”异常。这是详细的例外。


org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'postProcessProperties' defined in class path resource [applicationContext.xml]: Circular placeholder reference 'processor.core.poolsize' in property definitions
     [echo]     at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:287)
     [echo]     at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
     [echo]     at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:663)
     [echo]     at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:638)
     [echo]     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
     [echo]     at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
     [echo]     at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
     [echo]     at com.autodesk.postprocess.engine.PostProcessEngine.start(PostProcessEngine.java:39)
     [echo]     at com.autodesk.postprocess.engine.PostProcessEngine.main(PostProcessEngine.java:29)

这是一个spring应用程序,它使用外部属性文件在启动时读取值。这是春天的定义。到目前为止,这种方法运作良好。


<bean id="propertyConfig"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_NEVER" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <value>classpath:/postprocess.properties</value>
            </list>
        </property>
        <property name="properties">
            <props>
                <prop key="processor.core.poolsize">${processor.core.poolsize}</prop>
                <prop key="processor.max.poolsize">${processor.max.poolsize}</prop>
            </props>
        </property>
    </bean>

    <bean id="postProcessProperties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="properties">
            <props>
                <prop key="processor.core.poolsize">${processor.core.poolsize}</prop>
                <prop key="processor.max.poolsize">${processor.max.poolsize}</prop>
                <prop key="processor.polling.delay">${processor.polling.delay}</prop>
                <prop key="processor.polling.period">${processor.polling.period}</prop>
        </property>
    </bean>

我正在使用shade插件生成jar文件。这是一个片段


<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.7.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.test.postprocess.engine.PostProcessEngine</mainClass>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                            </transformers>
                            <filters>
                                <filter>
                                    <artifact>:</artifact>
                                    <excludes>
                                        <exclude>META-INF/.SF</exclude>
                                        <exclude>META-INF/.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

我不确定是什么导致了这个问题,因为我之前在其他可执行jar文件中使用了类似的模式。

任何指针都会受到赞赏。

由于

5 个答案:

答案 0 :(得分:11)

它可能很晚才回答这个问题,但为了面对类似问题的人的利益而添加它。

我可以通过更改密钥名称来修复它。 e.g。

 <prop key="processor.core.poolsize">${processor.core.poolsize}</prop>
 <prop key="processor.max.poolsize">${processor.max.poolsize}</prop>

更改为

<prop key="processor.core.poolsize">${core.poolsize}</prop>
<prop key="processor.max.poolsize">${max.poolsize}</prop>

property-placeholder键和要过滤的属性值的键不能相同。

答案 1 :(得分:4)

Circular placeholder reference XXXX in property definitions

当您的属性键名称和值变量名称完全相同时,spring-framework会引发上述异常。

确保您的财产看起来不像 key={$key}。保持键名称和值的不同名称将解决此问题。

答案 2 :(得分:1)

I was having the same issue when trying to run a spring integration test using SpringJUnit4ClassRunner. I was unsure of how to get my properties file loaded and after a few mis-steps, I found the @TestPropertySource annotation that allowed me to define the properties file(s) needed.

I had tried @PropertySource before that, and it does not work when you are running a spring integration test like this.

Hopefully this helps someone else.

答案 3 :(得分:0)

可能无法找到资源 - postprocess.properties。你能否删除这一行 -

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

然后,如果找不到资源,则应显示相应的消息。

答案 4 :(得分:0)

在您的 java 代码中,如果您使用在除 application.propertiesapplication.yaml 之外的某些 .propertiesyaml 文件中声明的键,您当您尝试运行 Spring 应用程序而不指定要使用的配置文件时,将收到此异常。

解决办法: 就我而言,我使用的是 Spring Boot 2.4,这是告诉您的应用将使用哪个配置文件的一种方法:

命令行:

java -jar -Dspring.profiles.active=DEV myapp.jar

如果您通过 IntelliJ 等 IDE 运行您的应用:

运行 -> 编辑配置

在您的 Spring Config 中,指定您想要激活的配置文件(以逗号分隔):

enter image description here