Как правильно инжектить поля в bean-метод?
Sonar-линтер, считает, что правильнее будет инжектить так:
@Bean
public Example example(DataSource datasource) {
return new Example(datasource)
}
Но только, если поле использует один bean-метод. Мне интересно, почему это правило применимо только в случае, если поле использует один bean-метод. Или, возможно, лучше вообще инжектить через @autowired прямо в классе конфигурации?
Цитата из правила Sonar'а:
When @Autowired is used, dependencies need to be resolved when the class is instantiated, which may cause early initialization of beans or lead the context to look in places it shouldn't to find the bean. To avoid this tricky issue and optimize the way the context loads, dependencies should be requested as late as possible. That means using parameter injection instead of field injection for dependencies that are only used in a single @Bean method