我有一个键值属性文件,其中包含错误代码及其错误消息。
我想在应用程序启动时注入此文件,以便我可以在注入的属性上进行查找,而无需读取该文件。
下面只是伪代码,里面有什么吗Spring
可以创建这个设置吗?
@Value(location = "classpath:app.properties")
private Properties props;
而 app.properties 包含:
error1=test
error2=bla
...
如果没有,没有弹簧我怎么能实现这个目标?
您可以首先使用以下方法声明属性文件:<util:properties>
在你的 Spring 配置中:
<util:properties id="messages" location="classpath:app.properties" />
这会注册一个带有名称的beanmessages
,您可以将其自动装配/注入到其他 bean 中。
@Autowired
@Qualifier("messages")
private Properties props;
更多信息:
- Spring 中的 模式 http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-util-properties
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)