【问题标题】:Init-method from properties从属性初始化方法
【发布时间】:2018-06-29 14:53:46
【问题描述】:

我想声明一个 bean,它的 init-method 不是一个常量值,而是依赖于一个属性:

<bean id="XXX" class="YYY" init-method="${some.property}"/>

我已经配置了一个 PropertyPlaceholderConfigurer bean,但不幸的是 spring 尝试调用名为 ${some.property} 的方法而不是属性值。

我应该如何配置才能通过 some.property 值调用方法指针?

【问题讨论】:

  • 不幸的是,我不确定 Spring 是否支持这一点,但作为替代方案,您可以在 init 方法中读取属性并在代码中进行相应的调度。
  • 这是 2018 年,应该没有人再使用 Spring XML。使用@Configuration 类并以编程方式选择init 方法。

标签: spring


【解决方案1】:

为此,您需要包含以下 bean 并将属性文件的路径放在 location 属性中:

<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean" id="appConfig">
    <property name="location" value="classpath:appConfig.properties"></property>
</bean>

假设您的属性文件包含以下条目:

init_method = test

然后,通过使用你自己的代码sn-p,你应该这样引用上述属性:

<bean id="XXX" class="YYY" init-method="#{appConfig['init_method ']}"/>

appConfig 必须与在第一个 sn-p 上声明的 bean 的 id 匹配

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 2021-08-28
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多