2

初めて@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をコントローラーに自動配線すると、すべてが機能し始めました。

私はこれについてたくさんのことを読みました、そしてそれがどこかで説明されているなら、それは私にとって明確な方法で説明されていませんでした。ああ。

4

2 に答える 2

5

以下の作品

単体テスト

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 {

}

Dao Impl

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>
于 2012-04-19T04:05:10.253 に答える
0

アプリの起動時にSpringコンテナが起動しているかどうかを確認しましたか?構成に問題がある可能性がありweb.xmlます。

于 2012-04-19T20:26:48.927 に答える