3
當它們被自動檢測到時,是否可以將屬性設置爲bean?使用組件掃描時,您可以將屬性設置爲bean嗎?
我有一個需要讀取文本文件的bean,我想注入路徑,但是這個bean是一個web應用程序的一部分,所有bean都是自動檢測到的。
當它們被自動檢測到時,是否可以將屬性設置爲bean?使用組件掃描時,您可以將屬性設置爲bean嗎?
我有一個需要讀取文本文件的bean,我想注入路徑,但是這個bean是一個web應用程序的一部分,所有bean都是自動檢測到的。
是隻使用它們注入@Value
annoation,如:
@Service("myService")
public class MyService
@Value("${myProperty}")
String whatever;
...
,然後在應用方面:
<context:property-placeholder
location="classpath:application.properties"
ignore-unresolvable="true"
/>
包含您的字符串棒文件application.properties瓦爾在classpath中(通過SRC正常/主/資源)。
或者您也可以確保您的文件是在類路徑和參考,作爲類路徑資源
final org.springframework.core.io.Resource myFile = new ClassPathResource("MyTextFile.text");