I'm trying to use values from my application.properties file in my service implementation class/bean. But when the bean is initialized through my config class, the property values are all null.
Config class:
@Configuration
public class AppConfig {
@Bean AppServiceImpl appServiceImpl() {
return new AppServiceImpl();
}
}
Service class:
@Component
public class AppServiceImpl implements AppService {
@Value("${value.one}")
String value_one;
@Value("${value.two}")
String value_two;
@Value("${value.three}")
String value_three;
//values are null here
public AppServiceImpl() {
functionOne(value_one, value_two, value_three);
}
}
application.properties(under src/main/resources):
value.one=1
value.two=2
value.three=3
Doing some debugging i can see that the AppConfig class has found the properties file and if i try to declare @Value("${value.one}") String value_one;
there it shows it has been given the value 1
as expected.
But in my AppServiceImpl class, all the values are null. What am I doing wrong here? How should this be done properly in Springboot? Or just Spring even.
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…