![](/img/trans.png)
[英]How do I inject a value from Properties file to a field of an existing instance (not managed by Spring) using Spring annotations?
[英]Inject value from properties into interface field
是否有在接口字段上插入屬性的選項? 我嘗試過這樣的事情,但沒有奏效。
public interface ServicePathsConfig {
@Value("${default-connection-timeout}")
int DEFAULT_CONNECT_TIMEOUT = 1000;
}
我嘗試使用@PostConstruct 制作默認設置器,結果相同。 任何想法如何將屬性注入接口字段?
您可以使用此代碼打開配置 java class。 @Configuration @ComponentScan("com.??") @PropertySource("classpath:(??.properties"))
並將 default-connection-tiemout 放到另一個沒有 int 的新屬性文件中。 但是您不能將其注入接口,您需要注入一些 java class。
java class:
@Component
public class blabla implements blabla1 {
@Value("${default-connection.timeout}")
int default-connection;
--------------
interface
public interface blabla1 {
}
-------------
file:
Timeout.properties
default-connection.timeout=1000
------------------
java class:
@Configuration
@ComponentScan("com.(your package name)")
@PropertySource("classpath:connection.properties")
public class TimeoutConfig {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.