Question

I had being using the JSR 330 @Inject annotation to autowire my Spring beans. I started experimenting by removing the @Inject annotation - yet my application context still gets loaded correctly. Not sure if this is expected and cant find any spring documentation to verify this use case.

// This context is loaded correctly - and beans exist for B, C and Db
final ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);

@Import({ B.class })
@Configuration
public class ApplicationConfig {

@Bean
public Db db() {
   return new Database();
}

@Bean
// I thought this method would need an @Autowire or @Inject annotation to resolve b!?
public C c(final B b){
  return new C(b);
}
}

@Configuration
 public class BConfig {

 @Bean
 public B b() {
    return new B();
 }
}
Was it helpful?

Solution

@Autowired (or @Inject if you prefer) is implicit in @Bean methods (always has been as far as I know).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top