Noncompliant Code Example
@Configuration
public class FooConfiguration {
@Autowired private DataSource dataSource; // Noncompliant
@Bean public MyService myService() {
return new MyService(this.dataSource);
}
}
It makes total sense to move the Autowired field into the bean method. However, there is one exception I’d like to see added: If myService() is used in one or more other bean method:
@Bean public MyOtherService myOtherService() {
return new MyOtherService(myService()); // don't report "field only used in myService" because then I'd have to declare the Autowired in myOtherService as well.
}