我们可以从一个maven依赖到另一个maven依赖有不同的类路径位置吗?

时间:2016-09-29 20:46:29

标签: java spring maven

我正在处理由两个java项目组成的应用程序 项目A和项目B.

在项目B pom.xml中,我已将项目A指示为依赖项。

在项目A的spring配置文件中,我使用propertyPlaceholders从属性文件加载值。

这是项目A中定义的spring-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 / main / resources下的任何位置加载global.properties文件。

此配置适用于项目A的单元测试,因此global.properties中的值可以在src / main / resources下的global.properties中得到很好的加载。

奇怪的是,当我在调用项目A时运行项目B时,编译失败,同时抱怨由于项目A spring配置文件中使用的变量无法解析,项目A无法初始化。这是因为通过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可能有不同的类路径位置吗?

有人可以帮我理解发生了什么吗?

请注意,如果我在propertyplaceholderconfigurer中提到类似classpath:service / global.properties的硬修复路径,问题就解决了。但我不想要一个修复位置

1 个答案:

答案 0 :(得分:0)

你说A是B的依赖关系。属性的位置是什么,它们是否被添加到jar中? (它们可能在A的运行时可见,但未打包。在这种情况下,B无法访问这些属性)