属性 未在依赖 jar 中填充 (Spring 3 / XML)
Property does not get filled within dependency jar (Spring 3 / XML)
我们有一个 Spring 托管应用程序,它使用另一个 jar 作为依赖项,其中包含一个 Spring 托管服务 class,需要使用从 [=57 注入的一些值=] 文件
具有 Spring 上下文设置的主应用程序
public static void main(String[] args) {
GenericXmlApplicationContext appContext = new GenericXmlApplicationContext("applicationContext.xml");
SomeClass someClass = (SomeClass) appContext.getBean("someClass");
someClass.someMethod();
...
从依赖jar中调用服务的class
public class SomeClass {
private ServiceFromTheOtherJar serviceFromTheOtherJar;
public SomeClass(ServiceFromTheOtherJar serviceFromTheOtherJar) {
this.serviceFromTheOtherJar = serviceFromTheOtherJar;
}
public void someMethod() {
serviceFromTheOtherJar.call();
...
主应用applicationContext.xml
<bean name="serviceFromTheOtherJar" class="com...ServiceFromTheOtherJar"/>
<bean name="someClass" class="com...SomeClass">
<constructor-arg ref="serviceFromTheOtherJar"/>
</bean>
依赖jar中的服务class
public class ServiceFromTheOtherJar {
private String someFieldWeWantToFillFromPropertyFile;
public void setSomeFieldWeWantToFillFromPropertyFile(String someFieldWeWantToFillFromPropertyFile) {
this.someFieldWeWantToFillFromPropertyFile = someFieldWeWantToFillFromPropertyFile;
}
public void call() {
//we would like to use the filled someFieldWeWantToFillFromPropertyFile here
...
当然,我们在相关 jar 中有一个 application.properties 文件,其中包含我们想要注入到 someFieldWeWantToFillFromPropertyFile
中的 属性 值
现在我们可以将依赖jar作为依赖添加到主应用中;当主应用程序正在执行时,它的 Spring 上下文设置正确,并且 ServiceFromTheOtherJar.call() 方法按预期被调用;然而 someFieldWeWantToFillFromPropertyFile 并没有从 属性 文件中得到填充,无论我们到目前为止尝试了什么(例如 @PropertySource({"application.properties"}), Environment.getProperty(...) 等)
限制
我们在两个 jar 中都有 Spring 3 版本,由于部署环境的原因,它必须保持不变;所以Spring 4个解不成问题
正如您在上面看到的,主应用程序当前使用 GenericXmlApplicationContext 并且更改似乎表明对应用程序进行了重大重写。因此,例如似乎无法在 ServiceFromTheOtherJar 上使用 @Service 注释,因为它在执行和上下文设置期间导致了 BeanCreationException
要从 属性 文件中读取值,您必须将以下 bean 添加到 applicationContext.xml。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:application.properties" />
</bean>
假设 application.properties 文件包含这样的定义
myValue=Hello World
服务bean的定义应该像这样扩展
<bean name="serviceFromTheOtherJar" class="com...ServiceFromTheOtherJar">
<property name="someFieldWeWantToFillFromPropertyFile" value="${myValue}" />
</bean>
现在Spring会在类路径中查找application.properties-文件,并根据myValue设置服务bean的属性。
我们有一个 Spring 托管应用程序,它使用另一个 jar 作为依赖项,其中包含一个 Spring 托管服务 class,需要使用从 [=57 注入的一些值=] 文件
具有 Spring 上下文设置的主应用程序
public static void main(String[] args) {
GenericXmlApplicationContext appContext = new GenericXmlApplicationContext("applicationContext.xml");
SomeClass someClass = (SomeClass) appContext.getBean("someClass");
someClass.someMethod();
...
从依赖jar中调用服务的class
public class SomeClass {
private ServiceFromTheOtherJar serviceFromTheOtherJar;
public SomeClass(ServiceFromTheOtherJar serviceFromTheOtherJar) {
this.serviceFromTheOtherJar = serviceFromTheOtherJar;
}
public void someMethod() {
serviceFromTheOtherJar.call();
...
主应用applicationContext.xml
<bean name="serviceFromTheOtherJar" class="com...ServiceFromTheOtherJar"/>
<bean name="someClass" class="com...SomeClass">
<constructor-arg ref="serviceFromTheOtherJar"/>
</bean>
依赖jar中的服务class
public class ServiceFromTheOtherJar {
private String someFieldWeWantToFillFromPropertyFile;
public void setSomeFieldWeWantToFillFromPropertyFile(String someFieldWeWantToFillFromPropertyFile) {
this.someFieldWeWantToFillFromPropertyFile = someFieldWeWantToFillFromPropertyFile;
}
public void call() {
//we would like to use the filled someFieldWeWantToFillFromPropertyFile here
...
当然,我们在相关 jar 中有一个 application.properties 文件,其中包含我们想要注入到 someFieldWeWantToFillFromPropertyFile
中的 属性 值现在我们可以将依赖jar作为依赖添加到主应用中;当主应用程序正在执行时,它的 Spring 上下文设置正确,并且 ServiceFromTheOtherJar.call() 方法按预期被调用;然而 someFieldWeWantToFillFromPropertyFile 并没有从 属性 文件中得到填充,无论我们到目前为止尝试了什么(例如 @PropertySource({"application.properties"}), Environment.getProperty(...) 等)
限制
我们在两个 jar 中都有 Spring 3 版本,由于部署环境的原因,它必须保持不变;所以Spring 4个解不成问题
正如您在上面看到的,主应用程序当前使用 GenericXmlApplicationContext 并且更改似乎表明对应用程序进行了重大重写。因此,例如似乎无法在 ServiceFromTheOtherJar 上使用 @Service 注释,因为它在执行和上下文设置期间导致了 BeanCreationException
要从 属性 文件中读取值,您必须将以下 bean 添加到 applicationContext.xml。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:application.properties" />
</bean>
假设 application.properties 文件包含这样的定义
myValue=Hello World
服务bean的定义应该像这样扩展
<bean name="serviceFromTheOtherJar" class="com...ServiceFromTheOtherJar">
<property name="someFieldWeWantToFillFromPropertyFile" value="${myValue}" />
</bean>
现在Spring会在类路径中查找application.properties-文件,并根据myValue设置服务bean的属性。