首页 > 解决方案 > 将属性中的值注入接口字段

问题描述

是否有在接口字段上插入属性的选项?我尝试过这样的事情,但没有奏效。

public interface ServicePathsConfig {
    @Value("${default-connection-timeout}")
    int DEFAULT_CONNECT_TIMEOUT = 1000;
}

我尝试使用@PostConstruct 制作默认设置器,结果相同。任何想法如何将属性注入接口字段?

标签: javaspringinterfacestaticcode-injection

解决方案


您可以使用此代码打开配置 java 类。@Configuration @ComponentScan("com.??") @PropertySource("classpath:(??.properties"))

并将 default-connection-tiemout 放到另一个没有 int 的新属性文件中。但是你不能把它注入到你需要注入一些java类的接口中。


推荐阅读