我工作的两个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,问题就解决了。但我不想要修复位置
您可以为项目添加整个应用程序上下文xml文件,也可以添加堆栈跟踪你遇到的错误? – Veeram
我已编辑我的文章 –