Injection of autowired dependencies failed

回眸只為那壹抹淺笑 提交于 2020-01-06 03:33:12

问题


I'm trying to realize the same as in Spring error when trying to manage several classes that share a common base class?

But I'm still getting this Exception:

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

Here are my classes in hope someone can help me understanding this autowiring stuff...

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

UPDATE

Servlet context does contain this line: <context:component-scan base-package="com.example" /> Test context (I'm using maven) does contain this line: <context:annotation-config />

Replacing <context:annotation-config /> with <context:component-scan base-package="com.example" /> results in this Exception:

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

I'm still getting this exception, here's my new code (only changed classes):

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

The solution:

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

回答1:


It doesn't look like an instance of CategoryService is available for Spring to inject in the dependency into the test. You may be missing the component-scan in your services package - <context:component-scan base-package="..">

Update: Based on your update, and this post - Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' defined in ServletContext resource , it looks like you will have to change your BaseService, to have a setter for dao rather than set using a constructor. CGLIB with Spring AOP may not work well with a non default constructor




回答2:


You should annotate your classes with @Component at least, for them to be eligible for autowired injection.



来源:https://stackoverflow.com/questions/11298610/injection-of-autowired-dependencies-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!