Spring 在使用组件扫描时,可以将属性设置为bean吗?

Spring 在使用组件扫描时,可以将属性设置为bean吗?,spring,Spring,当自动检测到bean时,是否可以将属性设置为bean 我有一个需要读取文本文件的bean,我想插入路径,但这个bean是自动检测所有bean的web应用程序的一部分。是的,只需使用@Value注释插入它们,例如: @Service("myService") public class MyService @Value("${myProperty}") String whatever; ... 然后在应用程序上下文中: <context:property-placehold

当自动检测到bean时,是否可以将属性设置为bean


我有一个需要读取文本文件的bean,我想插入路径,但这个bean是自动检测所有bean的web应用程序的一部分。

是的,只需使用
@Value
注释插入它们,例如:

@Service("myService")
public class MyService

    @Value("${myProperty}")
    String whatever;
...
然后在应用程序上下文中:

<context:property-placeholder
  location="classpath:application.properties"
  ignore-unresolvable="true"
/>
final org.springframework.core.io.Resource myFile = new ClassPathResource("MyTextFile.text");