我有一个Spring管理的bean,使用property-placeholder其关联的a来加载属性context.xml:
<context:property-placeholder location="file:config/example.prefs" />
Run Code Online (Sandbox Code Playgroud)
我可以@Value在初始化时使用Spring的注释来访问属性,例如:
@Value("${some.prefs.key}")
String someProperty;
Run Code Online (Sandbox Code Playgroud)
...但我需要以通用方式将这些属性公开给其他(非Spring托管)对象.理想情况下,我可以通过以下方法公开它们:
public String getPropertyValue(String key) {
@Value("${" + key + "}")
String value;
return value;
}
Run Code Online (Sandbox Code Playgroud)
...但显然我不能@Value在该上下文中使用注释.有没有什么方法可以example.prefs在运行时使用键访问Spring加载的属性,例如:
public String getPropertyValue(String key) {
return SomeSpringContextOrEnvironmentObject.getValue(key);
}
Run Code Online (Sandbox Code Playgroud) 我刚从Ruby 1.8升级到1.9,我的大多数文本处理脚本现在都因错误而失败invalid byte sequence in UTF-8.我需要删除无效字符或指定Ruby应该使用ASCII编码(或者C stdio函数编写的任何编码,文件的生成方式) - 我将如何进行这些操作?
最好是后者,因为(尽我所知)磁盘上的文件没有任何问题 - 如果有奇怪的,无效的字符,它们就不会出现在我的编辑器中......
我有一个属性文件,我使用以下property-placeholder元素通过 XML 向 Spring 注册:
<context:property-placeholder location="classpath:foo.properties" />
Run Code Online (Sandbox Code Playgroud)
我可以使用@Value注释访问属性,例如
@Value("${prefs.key}")
private String prefValue;
Run Code Online (Sandbox Code Playgroud)
但我还需要通过 Spring Environment 访问属性,例如
@Autowired
private Environment env;
public String getValue(String key) {
return env.getProperty(key);
}
Run Code Online (Sandbox Code Playgroud)
getValue()这里总是返回null,即使对于属性文件中定义的键也是如此,因为 using<property-placeholder>似乎不会向环境公开属性。有没有办法强制以这种方式加载的属性可以通过环境访问?