[英]Spring: @Autowired without @Component
In a real project, I found out that @Component
may be omitted in the following code:在实际项目中,我发现以下代码中可能会省略
@Component
:
// 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.) (该示例取自 Spring 官方文档https://docs.spring.io/spring-framework/docs/4.3.x/spring-framework-reference/htmlsingle/#beans-autowired-annotation ,文档显示没有
@Component
,这可能意味着它不需要,或者只是没有显示。)
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.我工作的项目不使用任何 XML bean 声明,但它使用了 Spring 以外的框架,因此有可能将类声明为 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.
或者它可能是我们使用的 Spring 版本的一个特性,如果该特性没有记录,它可能会在以后被删除。
Question: Must the class that uses @Autowired
be annotated with @Component
(well, be a bean)?问题:使用
@Autowired
的类是否必须用@Component
进行注释(好吧,是一个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. UPD伙计们,项目中没有
@Configuration
和 XML 配置,我知道这样的声明从一个类中生成了一个 bean,但问题不在于它们。 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? @Autowired
在非 bean 的类中工作吗? Or maybe it declares the class that uses it as a bean?或者它可能声明了将它用作 bean 的类?
there are several ways to instantiate a bean in Spring.有几种方法可以在 Spring 中实例化 bean。
One of them indeed is with the @Component
annotations, with that, Spring will scan all the packages defined for component-scan and initialize all annotated classes (either with @Component
or one of the annotations that uses it - Controller, Service, etc.).其中之一确实是使用
@Component
注释,这样,Spring 将扫描为组件扫描定义的所有包并初始化所有带注释的类(使用@Component
或使用它的注释之一 - 控制器、服务等。 )。
Other way to initialise beans is using a configuration class (annotated with @Configuration
) that includes methods annotated with @Bean
.初始化 bean 的其他方法是使用配置类(用
@Configuration
注释),其中包含用@Bean
注释的方法。 each of these methods will create a bean.这些方法中的每一个都将创建一个 bean。
There's also an option to create the beans using xml configurations, but this is becoming less and less common, as the annotation-based approach is more convinient还有一个选项可以使用 xml 配置创建 bean,但这种方法越来越少,因为基于注释的方法更方便
According to https://stackoverflow.com/a/3813725/755804 , with autowireBean()
it is possible to autowire a bean from a class not declared as a bean.据https://stackoverflow.com/a/3813725/755804 ,与
autowireBean()
有可能从没有被声明为一个bean类自动装配的bean。
@Autowired
private AutowireCapableBeanFactory beanFactory;
public void sayHello(){
System.out.println("Hello World");
Bar bar = new Bar();
beanFactory.autowireBean(bar);
bar.sayHello();
}
and和
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
public class Bar {
@Autowired
private Foo foo;
public void sayHello(){
System.out.println("Bar: Hello World! foo="+foo);
}
}
On the other hand, by default the latest Spring does not assume that classes that use @Autowire
are @Component
-s.在另一方面,默认情况下,最新的Spring并不假定类,使用
@Autowire
是@Component
-s。
UPD As to the mentioned real project, the stack trace shows that the constructor is called from createBean()
. UPD至于提到的真实项目,堆栈跟踪显示构造函数是从
createBean()
调用的。 That is, the framework creates beans from classes declared in the framework's configs.也就是说,框架从框架配置中声明的类创建 bean。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.