Spring的@Autowired不注入依赖

发布于 2024-12-18 12:36:42 字数 1291 浏览 1 评论 0原文

我有一个由各种模块组成的项目。 基本上,我使用过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-12-25 12:36:42

从与 OP 的聊天中可以清楚地看出,他创建了像 Tesser tesser = new Tesser() 这样的对象,而不是将其注入到测试类中。
Spring 没有机会自动装配不是它自己创建的 bean 中的依赖关系。

解决方案是将 Tesser 对象自动装配到测试类中,以便 Spring 可以注入依赖项。

@Autowired
private Tesser tesser;

@Test
public void testSth() {
    assertTrue(tesser.someBoolReturningMethodUtilizingMonkDAO());
}

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.

@Autowired
private Tesser tesser;

@Test
public void testSth() {
    assertTrue(tesser.someBoolReturningMethodUtilizingMonkDAO());
}
请叫√我孤独 2024-12-25 12:36:42

添加限定符:

@Resource(name = "monkDAO")

如果您从注释开始,请一直进行下去。

Add qualifiers:

@Resource(name = "monkDAO")

If you start with annotations, go all the way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文