我正在尝试读取应用程序属性但默认情况下未加载。在存储库中它尝试读取yml文件用户名,但它没有获取api.username的值,但它获取空值。请帮我查找哪里缺少配置。
主要: -
@SpringBootApplication
@EnableScheduling
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
作业中的Spring Batch Processor调用片段: -
@Bean
public PersonItemProcessor processor() {
return new PersonItemProcessor();
}
Spring Batch Processor: -
@Component
public class PersonItemProcessor implements ItemProcessor<PersonDO,PersonDO> {
private static final Logger log = LoggerFactory.getLogger(PersonItemProcessor.class);
@Override
public PersonDO process(final PersonDO person) throws Exception {
getService().printUserName(person.getQueueId());
log.info(person + "");
return person;
}
@Bean
public SService getService(){
return new SService();
}
}
春季服务: -
@Service
public class SService {
public boolean printUserName(String password){
return getSRepository().printUserName(password);
}
@Bean
public SRepository getSRepository(){
return new SRepository();
}
}
Spring存储库: -
@Repository
public class SRepository {
@Value("${api.username}")
private String username;
public boolean printUserName(String password){
System.out.println("username:"+username+":password:"+password);
return (username!=null?true:false);
}
}
application.yml
api:
username: UserName1
spring:
batch:
job:
enabled: false
输出: -
username:null:password:password@123
更新application.yml的位置是src / main / resources