I have a dependent class B
of class A
that I want injected into A
.
And so I use the @Autowired
annotation in class A
. But class A
itself I create using new
keyword and I find that the reference to class B
is null
.
So if you are using Autowired
, must Spring manage class A
also?
I mean, is the null
reference because I am using new
and not letting Spring manage the whole thing?
Yes, the both classes need to be in the same context. Manage by Spring.
You can see an example here. http://www.mkyong.com/spring/spring-auto-wiring-beans-with-autowired-annotation/
是的,Spring容器应该管理A类和B类的对象,以便能够将B引用注入A.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.