Java 无法从Spring Boot中的属性文件加载密钥,null为@Autowired
我希望将所有属性文件加载到列表中,但看起来好像一切都不正常。我下面是:。我使用的是SpringBootV2.2.2.0版本Java 无法从Spring Boot中的属性文件加载密钥,null为@Autowired,java,spring,spring-boot,Java,Spring,Spring Boot,我希望将所有属性文件加载到列表中,但看起来好像一切都不正常。我下面是:。我使用的是SpringBootV2.2.2.0版本 @Configuration @PropertySource(value="classpath:endpoints.properties") public class AppConfig { @Value("#{'${endpoints}'.split(',')}") private List<String> endpoints; pu
@Configuration
@PropertySource(value="classpath:endpoints.properties")
public class AppConfig {
@Value("#{'${endpoints}'.split(',')}")
private List<String> endpoints;
public List<String> getEndPoints() {
return endpoints;
}
//To resolve ${} in @Value
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
}
编辑:
@Slf4j
@组成部分
公共类DecisionInterceptor实现EndpointInterceptor{
@值(“#{${endpoints}.split(',')}”)
私有列表端点;
@凌驾
公共布尔HandlerRequest(MessageContext,对象端点)引发异常{
if(方法端点的端点实例){
MethodEndpoint MethodEndpoint=(MethodEndpoint)端点;
//List endpoints=Arrays.asList(env.getProperty(“edr.soap.endpoints”,String[].class));
返回endpoints.contains(methodEndpoint.getMethod().getName());
}
返回true;
}
@凌驾
公共布尔HandlerResponse(MessageContext MessageContext,对象端点)引发异常{
返回true;
}
@凌驾
公共布尔handleFault(MessageContext,对象端点)引发异常{
返回true;
}
@凌驾
公共void afterCompletion(MessageContext MessageContext,对象端点,异常ex)引发异常{
调试(“DecisionInterceptor-完成后执行…”);
}
}
总结评论中的回复。
看来这是一个类似的问题。并且应该检查
DecisionInterceptor
是如何在配置中构造的
您得到了什么异常?@Environment是null您使用Environment env的类是什么类型的Bean?import org.springframework.core.env.Environment@PAA你看到这个问题了吗?看起来是同一个问题:
@Autowired
private Environment env;
@Override
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
if(endpoint instanceof MethodEndpoint) {
MethodEndpoint methodEndpoint = (MethodEndpoint)endpoint;
List<String> endpoints = Arrays.asList(env.getProperty("endpoints", String[].class));
return endpoints.contains(methodEndpoint.getMethod().getName());
}
return true;
}
@Slf4j
@Component
public class DecisionInterceptor implements EndpointInterceptor {
@Value("#{'${endpoints}'.split(',')}")
private List<String> endpoints;
@Override
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
if(endpoint instanceof MethodEndpoint) {
MethodEndpoint methodEndpoint = (MethodEndpoint)endpoint;
// List<String> endpoints = Arrays.asList(env.getProperty("edr.soap.endpoints", String[].class));
return endpoints.contains(methodEndpoint.getMethod().getName());
}
return true;
}
@Override
public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
return true;
}
@Override
public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception {
return true;
}
@Override
public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) throws Exception {
log.debug("DecisionInterceptor - afterCompletion executed ...");
}
}