1

共通の基本クラスを共有する複数のクラスを管理しようとすると、Springエラーと同じことを実現しようとしていますか?

しかし、私はまだこの例外を受け取っています:

Error creating bean with name 'com.example.model.CategoryTest': Injection of
autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire
field: private com.example.model.CategoryService
com.example.model.CategoryTest.service; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean
of type [com.example.model.CategoryService] found for dependency: expected at
least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

これが私のクラスです。誰かがこの自動配線のことを理解するのを手伝ってくれることを願っています...

public abstract class BaseDAO<E>
{
    public abstract void delete( int id );
    public abstract void save( E entity );
    public abstract List<E> list();
}

public abstract class BaseService<E, D extends BaseDAO<E>>
{
    private final D dao;

    protected BaseService( D dao )
    {
        this.dao = dao;
    }

    @Transactional
    public void delete( int id )
    {
        dao.delete( id );
    }

    @Transactional
    public void save( E entity )
    {
        dao.save( entity );
    }

    @Transactional
    public List<E> list()
    {
        return dao.list();
    }
}

@Repository
public class CategoryDAO extends BaseDAO<Category>
{
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void delete( int id )
    {
        Category category = ( Category ) sessionFactory.getCurrentSession().load( Category.class, id );

        if ( category != null )
        {
            sessionFactory.getCurrentSession().delete( category );
        }
    }

    @Override
    public void save( Category category )
    {
        sessionFactory.getCurrentSession().save( category );
    }

    @Override
    public List<Category> list()
    {
        return sessionFactory.getCurrentSession().createQuery( "from Category" ).list();
    }
}

@Service
public class CategoryService extends BaseService<Category, CategoryDAO>
{
    @Autowired
    public CategoryService( CategoryDAO dao )
    {
        super( dao );
    }
}

アップデート

サーブレットコンテキストには次の行が含まれています:<context:component-scan base-package="com.example" /> テストコンテキスト(私はmavenを使用しています)には次の行が含まれています:<context:annotation-config />

結果を置き換える<context:annotation-config /><context:component-scan base-package="com.example" />、次の例外が発生します。

org.springframework.beans.factory.BeanCreationException: Could not autowire field:
private com.example.model.CategoryService
com.example.controller.ExampleController.categoryService; 
nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'categoryService' defined in file
[/home/danny/example/target/classes/com/example/model/CategoryService.class]:
Initialization of bean failed; nested exception is
org.springframework.aop.framework.AopConfigException: Could not generate CGLIB
subclass of class [class com.example.model.CategoryService]: Common causes of
this problem include using a final class or a non-visible class; nested exception
is java.lang.IllegalArgumentException: Superclass has no null constructors but no
arguments were given

UPDATE2

私はまだこの例外を受け取っています、これが私の新しいコードです(変更されたクラスのみ):

public abstract class BaseService<E, D extends BaseDAO<E>>
{
    private D dao;

    /*protected BaseService( D dao )
    {
        this.dao = dao;
    }*/
    protected BaseService(){}

    protected void setDAO( D dao )
    {
        this.dao = dao;
    }

    @Transactional
    public void delete( int id )
    {
        dao.delete( id );
    }

    @Transactional
    public void save( E entity )
    {
        dao.save( entity );
    }

    @Transactional
    public List<E> list()
    {
        return dao.list();
    }
}

@Service
public class CategoryService extends BaseService<Category, CategoryDAO>
{
    @Autowired
    public CategoryService( CategoryDAO dao )
    {
        setDAO( dao );
    }
}

UPDATE3

ソリューション:

public abstract class BaseService<E, D extends BaseDAO<E>>
{
    protected D dao;

    public BaseService()
    {
    }

    protected D getDao()
    {
        return dao;
    }

    @Autowired
    protected void setDAO( D dao )
    {
        this.dao = dao;
    }

    // ...
}

@Service
public class CategoryService extends BaseService<Category, CategoryDAO>
{
    public CategoryService()
    {
        setDAO( dao );
    }
}
4

2 に答える 2

1

Spring が依存関係をテストに挿入できるように、CategoryService のインスタンスを使用できるようには見えません。サービス パッケージにコンポーネント スキャンが含まれていない可能性があります -<context:component-scan base-package="..">

更新: あなたの更新とこの投稿 - ServletContext リソースで定義された名前 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' を持つ Bean を作成中にエラーが発生しました。BaseService を次のように変更する必要があるようです。コンストラクターを使用して設定するのではなく、dao のセッターを使用します。Spring AOP を使用した CGLIB は、デフォルト以外のコンストラクターではうまく機能しない可能性があります

于 2012-07-02T18:00:22.510 に答える
0

自動配線インジェクションの対象となるには、少なくとも@Componentでクラスに注釈を付ける必要があります。

于 2012-07-03T02:05:58.577 に答える