@Component is a bean type annotation of spring. Only after adding this annotation will the bean be managed by spring. If this annotation is not added, spring will not be able to recognize this class, so an error will be reported. If this annotation is added, Then spring knows that these two classes are two beans in spring and can automatically inject each other. My understanding of spring is not deep enough, what I said may be wrong, it is just my personal opinion
@Autowired means injection. To use injection, your class must be managed by spring, which means your class B必须是是一个spring的bean, class A也必须是一个spring的bean, 你报错就是因为你的class A不是一个spring的bean, spring无法帮你注入class B. 如果要在不是spring bean的class A中使用class B只能手动把class Bset is put in.
Make a SpringContextHolder
@Component is a bean type annotation of spring. Only after adding this annotation will the bean be managed by spring. If this annotation is not added, spring will not be able to recognize this class, so an error will be reported. If this annotation is added, Then spring knows that these two classes are two beans in spring and can automatically inject each other. My understanding of spring is not deep enough, what I said may be wrong, it is just my personal opinion
Get it from applicationContext using service name.
@Autowired means injection. To use injection, your class must be managed by spring, which means your
class B
必须是是一个spring的bean,class A
也必须是一个spring的bean, 你报错就是因为你的class A
不是一个spring的bean, spring无法帮你注入class B
.如果要在不是spring bean的
class A
中使用class B
只能手动把class B
set is put in.