在@Service类中有几个用@Value注释的字段.这些字段未正确填充并且为空.也许我忽略了一些事情,我已经粘贴了下面的相关代码块.尝试了具有相同结果的替代选项env.getproperty().
package com.project.service.impl;
import org.springframework.beans.factory.annotation.Value
@Service("aService")
@PropertySource(value="classpath:app.properties")
public class ServiceImpl implements Service{
private Environment environment;
@Value("${list.size}")
private Integer list1;
@Value("${list2.size}")
private Integer list2Size;
@Autowired
public ServiceImpl(StringRedistemplate stringTemplate){
this.stringTemplate = stringTemplate;
logger.info("TESTING 123: "+list1);
}
// ...
}
@EnableWebMvc
@ComponentScan(basePackages = {"com.project.service","..."})
@Configuration
public class ServletConfig extends WebMvcConfigurerAdapter {
// ...
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[] {
new ClassPathResource("app.properties")
};
propertyPlaceholderConfigurer.setLocations(resources);
propertyPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
return propertyPlaceholderConfigurer;
}
}
最佳答案
现场注入在构造之后发生,因此出现NullPointer异常.解决方案是使用@Value注释构造函数参数,例如
public ServiceImpl(StringRedistemplate stringTemplate,@Value("${list.size}" Integer list1,..){}
原文地址:https://www.jb51.cc/spring/431752.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。