在 Spring 中为属性注入动态值
我对 Spring 非常陌生,在通过各种来源(书籍和互联网)浏览 DI 主题时,我看到了定义 bean 配置的以下模式:
例如,我们有一个类“SampleApp”
public class SampleApp
{
private int intValue;
private float floatValue;
public SampleApp(int value)
{
intValue = value;
}
public void setIntValue(int value)
{
intValue = value;
}
public void setFloatValue(float floatValue)
{
this.floatValue = floatValue;
}
}
相应的 bean 配置如下
<bean class="somepackage.SampleApp" id="samplebeanapp">
<constructor-arg value="15" />
<property value="0.5" name="floatValue"></property>
</bean>
:已将此处的值硬编码为 15 & 0.5。 这是我的问题:
- 是否可以将这些值作为运行时参数传递,并以范围作为原型?
- 如果是的话,我们该怎么做呢?请用一些例子来详细说明。
I am very new to Spring and while going through the DI topic through various sources (Book & Internet) I have seen the following pattern of defining bean configuration:
For example we have a class "SampleApp"
public class SampleApp
{
private int intValue;
private float floatValue;
public SampleApp(int value)
{
intValue = value;
}
public void setIntValue(int value)
{
intValue = value;
}
public void setFloatValue(float floatValue)
{
this.floatValue = floatValue;
}
}
Corresponding bean configuration is as follows:
<bean class="somepackage.SampleApp" id="samplebeanapp">
<constructor-arg value="15" />
<property value="0.5" name="floatValue"></property>
</bean>
We have been hard-coding the value here to 15 & 0.5.
Here are my questions :
- Is it possible to pass those values as run time parameter with scope as prototype?
- If yes, how can we do it? and please elaborate with some example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Spring 配置文件在启动时处理,并且 Spring 单例 bean 也在此时实例化。在正常情况下,它们在应用程序生命周期内不会再次被处理。
您可以使用此类类型的属性注入来传递配置值,同时可以使用对象引用的注入来确定应用程序的结构。
为了避免对 XML 文件中的值进行硬编码,您可以使用 < 将它们提取到属性文件中代码>PropertyPlaceholderConfigurer。
原则是尽可能多地针对接口进行编程,这样您就不会将自己束缚于任何特定的实现。但是,对于您正在考虑的情况,您只需在运行时传递这些值。
示例:BeanA需要DaoBean的服务,但DaoBean不会被BeanA实例化。它将通过依赖注入传递给 BeanA,并且 BeanA 只会与 DaoInterface 通信。
此时如果 BeanA 想要保存 15 和 0.5,将使用实际值(或更常见的变量)调用方法。
在这种情况下,我不使用依赖注入来传递值。依赖注入是一个很棒的工具,但并不意味着它必须在任何地方使用。
Spring configuration files are processed at startup, and Spring singleton beans are instantiated too in that moment. They are not processed again during the application lifecycle, in normal conditions.
You can use such type of property injection to pass configuration values, while you can use injection of object references to determine the structure of your application.
To avoid hardcoding the values inside the XML files, you can extract them in property files using a
PropertyPlaceholderConfigurer
.The principle is to program to interfaces as much as possible, so you don't tie yourself to any specific implementation. However, for the case you're thinking, you'll just pass the values at runtime.
Example: BeanA needs the services of DaoBean, but the DaoBean won't be instantiated by BeanA. It will be passed to BeanA through dependency injection and BeanA will only talk to a DaoInterface.
At this point if BeanA want to save 15 and 0.5, will call the methods with the actual values (or more commonly variables).
I don't use dependency injection to pass the values in this case. Dependency injection is a great tool, but doesn't meant that it has to be used everywhere.