In a real project, I found out that @Component
may be omitted in the following code:
// no @Component !!!!!
public class MovieRecommender {
private final CustomerPreference customerPreference;
@Autowired
public MovieRecommender(CustomerPreference customerPreference) {
this.customerPreference = customerPreference;
}
// ...
}
@Component
public class CustomerPreference {...}
(The example is taken from the official Spring docs https://docs.spring.io/spring-framework/docs/4.3.x/spring-framework-reference/htmlsingle/#beans-autowired-annotation , and the docs show no @Component
at all, which may mean either that it is not needed, or that it is just not shown.)
The project where I work does not use any XML bean declarations, but it uses frameworks other than just Spring, so it is possible that something declares the class as a bean. Or it may be a feature of the version of Spring that we use, and if that feature is not documented, it may be dropped later.
Question:
Must the class that uses @Autowired
be annotated with @Component
(well, be a bean)? Is there any official documentation about that?
UPD Folks, there is no @Configuration
and no XML configs in the project, I know that such declarations make a bean from a class, but the question is not about them. I even wrote "(well, be a bean)" in the question above to cover that. Does @Autowired
work in a class that is not a bean? Or maybe it declares the class that uses it as a bean?