Spring: How to inject a property value into the bean via XML?

柔情痞子 提交于 2021-02-07 09:34:59

问题


I have a bean with injected field value. It works just fine. I use annotation to ask Spring to inject value from properties into the bean.

public class MyBean {
    @Value("${app.settings.value}")
    private String value;
}

How can I do the same via XML? I've tried to inject using value="#{}".

<bean id="myBean" class="com.test.MyBean">
    <property name="value" value="{app.settings.value}"/>
</bean>

But my approach does not work.


回答1:


Try this:

<bean id="myBean" class="com.test.MyBean">
    <property name="value" value="${app.settings.value}"/>
</bean>

The syntax is the same as for java config - prefixed with the $ sign.



来源:https://stackoverflow.com/questions/34882646/spring-how-to-inject-a-property-value-into-the-bean-via-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!