【问题标题】:Initializing a Properties object using Spring使用 Spring 初始化 Properties 对象
【发布时间】:2013-05-23 10:55:35
【问题描述】:

我有一个扩展属性的类,用于存储一些专门的键名:

public class StorageConfiguration extends Properties {
    private final String PROPERTY_NAME_1 = "property.key";

    public String getProperty1() {
        return this.getProperty(PROPERTY_NAME_1);
    }

    public void setProperty1(String property1) {
        this.setProperty(PROPERTY_NAME_1, property1);
    }
}

还有一个使用这些属性的类:

public class Storage {
    StorageConfiguration storageConfiguration;

    @Autowired
    public void setStorageConfiguration(StorageConfiguration storageConfiguration) {
        this.storageConfiguration = storageConfiguration;
    }

    public void init() {
        // Initialize properties in this class using StorageConfiguration.
    }
}

我的 Spring 设置为像这样初始化 Storage 和 StorageConfiguration:

<bean id="storage" class="com.k4rthik.labs.Storage" init-method="init">
    <property name="storageConfiguration" ref="storageConfiguration" />
</bean>
<bean id="storageConfiguration"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="storageConfiguration">
        <props>
            <prop key="property.key">property_value</prop>
        </props>
    </property>
</bean>

我预期会发生的是 Spring 会通过将属性“property.key”设置为“property_value”来初始化 StorageConfiguration 对象。

但是我得到以下异常

org.springframework.beans.factory.BeanCreationException: 错误 创建在类路径资源中定义的名称为“storage”的bean [applicationContext.xml]:无法解析对 bean 的引用 设置 bean 属性时的“storageConfiguration” '授权配置';嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建在类路径中定义的名称为“authorizationConfig”的bean 资源 [applicationContext.xml]:设置属性值时出错; 嵌套异常是 org.springframework.beans.NotWritablePropertyException:无效 bean 类的属性“storageConfiguration” [org.springframework.beans.factory.config.PropertiesFactoryBean]:豆 属性“storageConfiguration”不可写或无效 设置方法。 setter的参数类型是否匹配return getter 的类型?

如您所见,我在 Storage 类中有一个用于 storageConfiguration 的自动装配设置器,所以我看不出这里有什么问题。

【问题讨论】:

  • 您的 storageConfiguration bean 属于 class="org.springframework.beans.factory.config.PropertiesFactoryBean" 类。这是正确的吗?
  • 为什么要在 XML 配置中将storageConfiguration 注入到storage@Autowired 注释setter?这不是多余的吗?
  • 如果你想要一个 setter,你不能使用 final 关键字。 final 表示该值不会在类中被修改。

标签: java spring properties


【解决方案1】:

PropertiesFactoryBean 创建一个 Properties 类型的 bean。

要创建 StorageConfiguration,您可以创建 Copy 构造函数

public class StorageConfiguration
{
    public StorageConfiguration(Properties defaults) {
        super(defaults);
    }
}

那么这应该可以工作:

<bean id="storageConfiguration" class="..StorageConfiguration">
  <constructor-arg>

   <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="property.key">property_value</prop>
        </props>
    </property>
  <bean>
  </constructor-arg>
</bean>

甚至:

<bean id="storageConfiguration" class="..StorageConfiguration">
  <constructor-arg>   
        <props>
            <prop key="property.key">property_value</prop>
        </props>
  </constructor-arg>
</bean>

【讨论】:

  • 太棒了。这行得通。 (尝试了较小的解决方案)。首先从 storageConfiguration 变量中删除了 @Autowired 标签,然后这个答案就像一个魅力。
【解决方案2】:

应该是

<bean id="storage" class="com.k4rthik.labs.Storage" init-method="init">
    <property name="storageConfiguration">
         <props>
            <prop key="property.key">property_value</prop>
        </props>
    </property>
</bean>

配置

<bean id="storageConfiguration"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="storageConfiguration">
        <props>
            <prop key="property.key">property_value</prop>
        </props>
    </property>
</bean>

表示org.springframework.beans.factory.config.PropertiesFactoryBean 类型的StorageConfiguration 有一个名为storageConfiguration 的属性,这与您的代码不同

【讨论】:

  • 那行不通:setStorageConfiguration 需要属性的特定子类。 总是创建超类 afaik。
  • 看起来@greyfairer 是对的。我得到一个 TypeMismatchException。接下来让我试试他的答案。
猜你喜欢
  • 1970-01-01
  • 2013-06-27
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多