抽象Spring bean未设置属性

时间:2018-09-15 16:02:45

标签: java xml spring

在抽象bean内设置属性值时遇到麻烦,我一辈子都无法知道如何/为什么。 IDE会解析所有属性,bean等,并且可以从日志中看到所有bean在运行时都已实例化。

抽象类:

public abstract class ParentBean<Key extends 
foo { <-- IDE resolves to xml bean definition
...

private boolean mySqlEnabled;

public void setMySqlEnabled(boolean mySqlEnabled) { <-- IDE resolves to xml property value
    this.mySqlEnabled = mySqlEnabled;
}

xml上下文:

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

<bean name="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="1"/>
    <property name="localOverride" value="true"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:common.data.loader.properties</value> <-- IDE resolves to correct file
        </list>
    </property>
</bean>

<bean id="parentBean" class="path.to.abstract.ParentBean"
      abstract="true">
    <property name="mySqlEnabled" value="${data.loader.mysql.enabled}"/> <-- IDE resolves to correct property value
</bean>

上述父bean正在另一个模块的子类中使用。 子类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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean name="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="1"/>
    <property name="localOverride" value="true"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:common.data.loader.properties</value>
        </list>
    </property>
</bean>

<import resource="classpath:META-INF/spring/common.platform.stream.provider.xml"/> <-- IDE resolves to above parent xml context

<bean id="childBean"
class="path.to.ChildBean"
      parent="parentBean"/>

尽管付出了所有努力和各种注释,mySqlEnabled属性仅解析为默认布尔值false,即使data.loader.mysql.enabled为true。我想念什么很明显的东西?

我假设我在两个xml中都不需要“ configurer” bean(可能只在父级xml中),但是仅在父级上下文中它不起作用,所以我尝试将它放在两个xml中。 >

0 个答案:

没有答案