2016-09-29 52 views
0

我工作的兩個Java項目 項目A和項目B.我們可以從一個maven依賴關係到另一個有不同的類路徑位置嗎?

在B項目的pom.xml組成的應用程序,我已表明項目A作爲一個依賴。

在項目A的spring配置文件中,我使用propertyPlaceholders從屬性文件加載值。

這是在項目中定義的彈簧jBPMConfig.xml文件:

 <?xml version="1.0" encoding="UTF-8"?> 
      <beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" 
      xmlns:util="http://www.springframework.org/schema/util" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 



      <bean id="dataSource" 
       class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 

       <property name="driverClassName" value="${jBPM.database.driver.class.name}" /> 
       <property name="url" value="${jBPM.database.url}" /> 
       <property name="username" value="${jBPM.database.user.name}" /> 
       <property name="password" value="${jBPM.database.user.password}" /> 
      </bean> 


      <bean name="propertyPlaceholder" 
       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
       <property name="locations"> 
       <list> 
        <value>classpath:/**/global.properties</value> 
       </list> 
       </property> 
      </bean> 

正如你可以看到,我想從在src隨時隨地/主/資源加載global.properties文件。

此配置適用於項目A的單元測試,因此global.properties中的值在src/main/resources下的global.properties所在的任何位置都可以很好地加載。

奇怪的是,當我在項目A被調用時運行項目B時,編譯失敗,抱怨項目A無法初始化,因爲項目A的spring配置文件中使用的變量無法解析。這是因爲通過定義placeholderConfigurer的方式,在類路徑中找不到global.properties文件。

這是失敗的主要原因:

  Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'jBPM.database.driver.class.name' in string value "${jBPM.database.driver.class.name}" 
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173) 
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125) 
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:258) 
    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:206) 
    ... 77 more 

做項目A和B可能可能有不同的類路徑中的位置?

有人能幫我理解發生了什麼嗎?

請注意,如果我提到類似路徑的硬修復路徑:service/global.properties in propertyplaceholderconfigurer,問題就解決了。但我不想要修復位置

+0

您可以爲項目添加整個應用程序上下文xml文件,也可以添加堆棧跟蹤你遇到的錯誤? – Veeram

+0

我已編輯我的文章 –

回答

0

你說A是B的依賴關係。屬性的位置是什麼?它們是否添加到jar中? (它們可能在A的運行時可見,但沒有打包,在這種情況下,B無權訪問這些屬性)

+0

.properties位於src/main/resources/service/< - 這裏是項目A,當打包項目A時,屬性文件被添加到它的jar中。我注意到,如果我提到一個像classpath這樣的硬解決方法路徑的話,問題就解決了:propertyplaceholderconfigurer中的service/global.properties。但我不想要一個修復位置 –

相關問題