是否可以在自动检测到bean时将属性设置为bean?
我有一个需要读取文本文件的bean,我想注入路径,但这个bean是web应用程序的一部分,其中所有bean都会被自动检测。
发布于 2012-06-29 19:01:52
是的,只需使用@Value注解来注入它们,例如:
@Service("myService")
public class MyService
@Value("${myProperty}")
String whatever;
...然后在应用程序上下文中:
<context:property-placeholder
location="classpath:application.properties"
ignore-unresolvable="true"
/>将包含字符串变量的application.properties文件粘贴到类路径中(通常通过src/main/resources)。
或者,您可以确保您的文件在类路径上,并将其作为类路径资源引用
final org.springframework.core.io.Resource myFile = new ClassPathResource("MyTextFile.text");https://stackoverflow.com/questions/11260572
复制相似问题