配置:
Spring 2.5、Junit 4、Log4j
log4j 文件位置由系统属性指定
${log.location}
在运行时,系统属性使用 -D java 选项设置。一切都很好。
问题/我需要什么:
在单元测试时,未设置系统属性,且未解析文件位置。
应用程序使用Spring,想简单地配置Spring来设置系统属性。
更多信息:
要求仅用于配置。无法将新的 Java 代码或条目引入 IDE。理想情况下,Spring 的属性配置实现之一可以处理这个问题——我只是无法找到正确的组合。
这个想法很接近,但是需要添加Java代码:
Spring SystemPropertyInitializingBean
有什么帮助吗?任何想法表示赞赏。
Configuration:
Spring 2.5, Junit 4, Log4j
The log4j file location is specified from a system property
${log.location}
At runtime, system property set with -D java option. All is well.
Problem / What I Need:
At unit test time, system property not set, and file location not resolved.
App uses Spring, would like to simply configure Spring to set the system property.
More Info:
Requirement is for configuration only. Can't introduce new Java code, or entries into IDE. Ideally, one of Spring's property configuration implementations could handle this--I just haven't been able to find the right combination.
This idea is close, but needs to add Java code:
Spring SystemPropertyInitializingBean
Any help out there? Any ideas are appreciated.
发布评论
评论(4)
评论中有一个关于如何执行此操作的 Spring 3 示例的请求。
There was a request in the comments for a Spring 3 example on how to do this.
您可以通过组合两个 MethodInvokingFactoryBeans
创建一个访问 System.getProperties 的内部 bean 和一个对内部 bean 获取的属性调用 putAll 的外部 bean:(
当然,您可以只使用一个 bean 并定位 System.setProperties( ),但是你会替换现有的属性,这不是一个好主意。
无论如何,这是我的小测试方法:
这是输出:
You can achieve that with the combination of two MethodInvokingFactoryBeans
Create an inner bean that accesses System.getProperties and an outer bean that invokes putAll on the properties acquired by the inner bean:
(You could of course use just one bean and target System.setProperties(), but then you'd be replacing existing properties which is not a good idea.
Anyway, here's my little test method:
And here's the output:
Spring Batch 有一个 SystemPropertyInitializer 类它可以用来更简洁地设置系统属性,例如强制 JBoss 日志记录使用 slf4j(使用 Spring JPA):
请记住添加“depends-on”属性以强制首先设置系统属性。
Spring Batch has a SystemPropertyInitializer class which can be used to set a system property slightly more concisely, e.g. to force JBoss logging to use slf4j (with Spring JPA):
Remember to add the "depends-on" attribute to force the system property to be set first.
对于更简洁的方法,请尝试:
For a more terse approach try: