Circular placeholder reference 'jdbc.driver' in property definitions

Circular placeholder reference 'jdbc.driver' in property definitions

Caused by: 
java.lang.IllegalArgumentException: Circular placeholder reference 'jdbc.driver' in property definitions
 at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:141)
 at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:162)
 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)
 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:606)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
 at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
 at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
 at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
 at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:801)
 at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:446)
 at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:793)
 at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:296)
 at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1347)
 at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:745)
 at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:492)
 at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:282)
 at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
 at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:117)
 at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:99)
 at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
 at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:154)
 at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
 at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:117)
 at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:99)
 at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
 at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
 at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:117)
 at org.eclipse.jetty.server.Server.start(Server.java:358)
 at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:99)
 at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
 at org.eclipse.jetty.server.Server.doStart(Server.java:325)
 at org.eclipse.jetty.maven.plugin.JettyServer.doStart(JettyServer.java:68)
 at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
 at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:564)
 at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:360)
 at org.eclipse.jetty.maven.plugin.JettyRunMojo.execute(JettyRunMojo.java:168)
 at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:133)
 at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
 at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
 at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
 at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108)
 at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:76)
 at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
 at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:116)
 at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:361)
 at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
 at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
 at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)
 at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:606)
 at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
 at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
 at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
 at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
 at org.codehaus.classworlds.Launcher.main(Launcher.java:46)

 

三個文件:

pom.xml:

<properties>
<jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
</properties>

jdbc.properties:

jdbc.driver=${jdbc.driver}

applicationContext.xml:

<property name="driver" value="${jdbc.driver}"/>

 

報錯:

Circular placeholder reference 'jdbc.driver' in property definitions

 

解決方式:

在 pom.xml 里加上下面這段:

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<filtering>false</filtering>
</resource>

<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

 

問題解決!


-----------------------------------------------------------------------

如何在properties文件中獲取maven的 pom.xml 的鍵值?
答案就是:在資源文件(*.properties)中放置pom.xml預先設置的變量, 在執行mvn package時就會自動將變量替換爲真實值
例如:
1. 我們在src\main\resources\application.properties放置如下內容
     version=${project.version}
2. 配置pom.xml讓Maven to copy that file into your output classes and translate the resource during that copy, interpreting the property
<!--一定要加上這個,不然在別的配置文件中是讀不到這裏配置的參數的-->      
<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
3. 執行mvn package
    查看生成的jar文件中的application.properties, version值已經變成了pom.xml中的version

因此我們只需使用java讀取application.properties即可


---------------------------------------------------

一般情況下,我們用到的資源文件(各種xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包時,maven能把這些資源文件打包到相應的jar或者war裏。但是如果pom.xml中沒有<build>...</build>   相應的xxx.properties 中就獲取不到pom.xml配置的變量。如${database.url}。所以pom中配置<build> 是xxx.properties,xxx.xml 中獲取pom中配置的變量的關鍵!!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章