gpt4 book ai didi

java - 使用 spring 将属性直接 Autowiring 到 bean 中?

转载 作者:行者123 更新时间:2023-11-30 04:03:05 24 4
gpt4 key购买 nike

我正在使用Spring3。我在其中一个 bean 中有以下属性

private Properties properties;

这里的属性是util包属性

    <bean id="properties" class="java.util.Properties">
<constructor-arg>
<props>
<prop key="id">${id}</prop>
<prop key="protocol">${protocol}</prop>
<prop key="username">${username}</prop>
<prop key="password">${password}</prop>
</props>
</constructor-arg>
</bean>

<bean id="propertyFactory"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="false" />
<property name="locations">
<list>
<value>classpath:conf/test.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="false"/>
<property name="properties" ref="propertyFactory"/>
</bean>

现在如何直接从 spring 配置注入(inject)属性?

谢谢!

最佳答案

如果您想访问某些属性值,有一个优雅的方法。

在您的 context.xml 文件中:

<context:property-placeholder location="classpath:myfile.properties" />

在你的类(class):

@Value("${key.property}")
private int myNumberProperty;

@Value("${another.key.property}")
private String myStringProperty;

关于java - 使用 spring 将属性直接 Autowiring 到 bean 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21480009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com