Spring的@Autowired不注入依赖
我有一个由各种模块组成的项目。 基本上,我使用过 Spring MVC 和 Spring MVC。 JUnit 4,一切都运行良好。 但是现在,我添加了一些与测试或 MVC 无关的类,并且 @Autowired 注释不会向它们注入对象。 相同的对象被注入到 MVC 和 JUnit 类中,所以我真的很困惑。
这是 Spring Context XML:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.justic.more" />
<mvc:annotation-driven />
<context:annotation-config />
<bean id="monkDAO" class="com.justic.more.data.monkDAO" />
<bean id="BidDAO" class="com.justic.more.data.BidDAO" />
</beans>
我想要注入的类:
@Component
Public Class Tesser {
@Autowired
MonkDAO monkdao;
...
blablabla
...
}
I have a project that consists of various modules.
Basically, I've worked with Spring MVC & JUnit 4, and everything was working good.
But Now, I added few classes which aren't related to testing or MVC, and the @Autowired annotation doesn't inject objects to them.
The same objects are injected to the MVC and JUnit classes, so I realy confused.
This is the Spring Context XML:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.justic.more" />
<mvc:annotation-driven />
<context:annotation-config />
<bean id="monkDAO" class="com.justic.more.data.monkDAO" />
<bean id="BidDAO" class="com.justic.more.data.BidDAO" />
</beans>
The Class that I want to inject to:
@Component
Public Class Tesser {
@Autowired
MonkDAO monkdao;
...
blablabla
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从与 OP 的聊天中可以清楚地看出,他创建了像
Tesser tesser = new Tesser()
这样的对象,而不是将其注入到测试类中。Spring 没有机会自动装配不是它自己创建的 bean 中的依赖关系。
解决方案是将 Tesser 对象自动装配到测试类中,以便 Spring 可以注入依赖项。
From the chat with OP it became clear that he created the object like
Tesser tesser = new Tesser()
instead of injecting it into the test class.Spring has no chance to autowire dependencies in beans that it does not create itself.
The solution is to autowire the
Tesser
object into the test class so Spring can inject the dependencies.添加限定符:
如果您从注释开始,请一直进行下去。
Add qualifiers:
If you start with annotations, go all the way.