将参数传递给ApplicationContext
我的应用程序有一个 application-context.xml。现在我将 ApplicationContext 实例化为:
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
是否可以通过此实例化传递参数,以便这些参数可用于初始化某些 bean 的某些属性?
PS:不使用属性文件。由于参数是运行时生成的,例如可执行 jar 的位置、系统架构、操作系统名称等,这些参数是可变的。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 applicationContext.xml 中的 >PropertyPlaceholderConfigurer
这允许您使用语法直接在 bean 声明中引用属性
${myProperty}
假设属性文件包含名为myProperty
的属性。如何使用此类属性的示例:
另一种替代方案可以基于
@Value
注释由SpEL
提供支持。You can use the PropertyPlaceholderConfigurer in your
applicationContext.xml
This allows you to reference properties directly in your bean declarations using syntax
${myProperty}
assuming the properties file contains a property namedmyProperty
.A sample how you can use such a property:
Another alternative could be based on the
@Value
annotation powered bySpEL
.这是解决方案,我将其发布,可能对将来的人有帮助:
Bean 类:
ApplicationContext 的初始化:
将此 bean 引用注入到 application-context.xml 的另一个 bean:
谢谢。
Here is the solution, I am posting it, might be helpful to someone in future:
The Bean class:
The initialization of the ApplicationContext:
The injection of this bean reference to another bean of application-context.xml:
Thanks.