【问题标题】:Injection of autowired dependencies failed注入自动装配的依赖项失败
【发布时间】:2012-07-03 03:48:39
【问题描述】:

我正在尝试实现与Spring error when trying to manage several classes that share a common base class?中的相同

但我仍然收到此异常:

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 );
    }
}

更新

Servlet 上下文确实包含这一行:&lt;context:component-scan base-package="com.example" /&gt; 测试上下文(我使用的是 maven)确实包含这一行:&lt;context:annotation-config /&gt;

&lt;context:annotation-config /&gt; 替换为&lt;context:component-scan base-package="com.example" /&gt; 会导致此异常:

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

更新2

我仍然遇到此异常,这是我的新代码(仅更改了类):

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 );
    }
}

【问题讨论】:

    标签: spring spring-transactions spring-annotations


    【解决方案1】:

    看起来没有 CategoryService 的实例可供 Spring 将依赖项注入到测试中。您的服务包中可能缺少组件扫描 - &lt;context:component-scan base-package=".."&gt;

    更新: 根据您的更新和这篇文章 - Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' defined in ServletContext resource ,看起来您将不得不更改您的 BaseService,为 dao 设置一个设置器,而不是使用构造函数设置。带有 Spring AOP 的 CGLIB 可能不适用于非默认构造函数

    【讨论】:

    • 谢谢,我在家的时候用二传手试试。
    • 你能举个例子吗?见 UPDATE2
    【解决方案2】:

    您应该至少使用 @Component 注释您的类,以便它们有资格进行自动注入。

    【讨论】:

    • 我使用的是@Repository@Service@Controller。但是在我的测试类中,我不能使用@Controller 注释,不是吗?这是一个 JUnit 测试类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 2018-02-21
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多