【问题标题】:Why doesn't my Spring @autowired DAO get autowired?为什么我的 Spring @autowired DAO 没有自动装配?
【发布时间】:2012-04-30 11:40:15
【问题描述】:

我第一次尝试@autowired,但失败了。我已经阅读了很多示例,而且我似乎做的一切都是正确的,但是当我的代码命中我的 getLeagueDAO() 方法时,实例变量被设置为 null。

我有以下代码:

package com.example.app.service;

@Service
public class LeagueService {

    // also tried @Autowired here, and that didn't work
    private LeagueDAO leagueDao; // = new LeagueHibernateDAO();

    public LeagueDAO getLeagueDAO() {
        return this.leagueDao;
    }

    @Autowired
    public void setLeagueDAO( LeagueDAO dao ) {
        this.leagueDao = dao;
    }

    [ ... ]

LeagueHibernateDAO:

package com.example.app.dao.impl.hibernate;

import ...

public class LeagueHibernateDAO implements LeagueDAO {

    public LeagueHibernateDAO() {
        super();
    }

    [ ... ]

我的 *-servlet.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

        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
">

    <context:annotation-config/>

    <context:component-scan base-package="com.example"/>

    <mvc:annotation-driven />

    <bean id="LeagueDAO" class="com.example.app.dao.impl.hibernate.LeagueHibernateDAO" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

我的 Spring 依赖项是:

    ... <org.springframework.version>3.0.6.RELEASE</org.springframework.version> ...


    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

我的单元测试没有运行。我的应用程序没有运行。都是因为 LeagueDao 没有在 LeagueService 中设置。为什么不连线?

我的完整解决方案

为了任何遇到类似问题的人的利益...

1) 很明显,我不知道如何编写自动装配代码的测试。接受的答案提供了如何执行此操作的工作示例。

2) 看来,使用自动装配,它基本上是全有或全无。我已经创建了一个应用程序并想返回并自动装配它。所以我想我会从小处着手,将 LeagueDAO 自动连接到 LeagueService。然而,因为 LeagueServiceTest 没有自动连接 LeagueService,它不会连接到 LeagueDAO 的引用。一旦我正确地自动连接了链中的每个步骤,一切都正常了。

所以当我尝试运行我的应用程序时,即使我的测试现在有效,我的应用程序再次没有自动装配。为了解决问题,我终于意识到我必须完全自动连接另一条链。在这种情况下,我已经将 LeagueDAO 插入到 LeagueService。问题是 LeagueService 是由控制器调用的,控制器只是通过调用构造函数来实例化 LeagueService 类。当我将 LeagueService 自动连接到控制器中时,一切都开始工作了。

我已经阅读了很多关于此的内容,如果在任何地方都对此进行了解释,那么它的解释方式对我来说并不清晰。啊。

【问题讨论】:

  • 我可以看到你LeagueHibernateDAO代码吗?
  • 好了!在服务课之后。
  • LeagueService的包是什么?
  • 添加到代码示例中:它是 com.example.app.service。
  • 如何在单元测试中加载弹簧配置?

标签: spring autowired


【解决方案1】:

您是否在启动应用程序时检查过 Spring 容器是否正在启动?这可能是您的 web.xml 配置的问题。

【讨论】:

  • 我确实想指出,虽然我的 web.xml 是正确的,但我一直感到困惑的是 Tomcat 是如何“正确”安装在 Mac 上的。为了方便起见,我忘记了它进入 /Library 并将其重新安装到 ~/Servers 中。副作用是从 ~/Servers/tomcat/bin 运行的脚本可以工作——但它们实际上是在 /Library 中运行代码。因此 ~/Servers/tomcat/logs 目录中没有任何日志,这让我摸不着头脑。所以你有点解决了一个我没有问过的问题。 =)
【解决方案2】:

以下作品

单元测试

package com.example.app.service;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.*;

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"/springcontext.xml"})
    public class TestLeagueService {

        @Autowired
        LeagueService service;

        @Test
        public void test() {
            assertNotNull(service.getLeagueDAO()); 
        }

    }

DAO

package com.example.app.dao.impl.hibernate;

public interface LeagueDAO {

}

道实现

package com.example.app.dao.impl.hibernate;

public class LeagueHibernateDAO implements LeagueDAO {


    public LeagueHibernateDAO() {
        super();
    }
}

弹簧上下文

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
       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">

     <context:annotation-config/>

    <context:component-scan base-package="com.example"/>


    <bean id="LeagueDAO" class="com.example.app.dao.impl.hibernate.LeagueHibernateDAO" />
    <bean id="LeagueService" class="com.example.app.service.LeagueService" />


</beans>

【讨论】:

  • 我添加了 Spring 单元测试的东西,经过几次跌跌撞撞,这似乎可以工作。但是,它仍然不会自动装配 DAO 引用。我的测试继续以空指针失败。 (我真的需要将我的 DAO 接口与实现放在同一个包中吗?)
  • @Marvo 不,您可以将您的接口放在任何您想要的地方。对于我的简单测试,将其放入与实现相同的包中更容易。
  • 你把 springcontext.xml 文件放在哪里了?我试过几个地方。我以为我让它在 WEB-INF 中工作并指定 @ContextConfiguration(locations={"WEB-INF/springcontext.xml"}) (带或不带前面的斜杠)但我得到“无法打开,因为它没有”不存在。
  • @Marvo Spring 配置文件通常放置在类路径中的某个位置,即用于 Web 应用程序 WEB-INF/classes。参见Common Webapp Configuration,了解如何将 Springframework 集成到 Web 应用程序中。
  • 如果您使用org.springframework.web.context.ContextLoaderListener 加载应用程序上下文,spring 默认尝试从/WEB-INF/applicationContext.xml 加载上下文配置。
猜你喜欢
  • 2018-01-27
  • 1970-01-01
  • 2015-09-18
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-20
  • 1970-01-01
相关资源
最近更新 更多