2012-07-02 131 views
1

我试图实现相同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); 
    } 
} 

UPDATE

servlet上下文包含此行:<context:component-scan base-package="com.example" /> 测试环境(我使用maven)不包含此行:

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<context:annotation-config />

在这种异常与<context:component-scan base-package="com.example" />结果更换<context:annotation-config />

我仍然得到这个异常,这是我的新代码(只更改类):

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

回答

1

它看起来并不像CategoryService的一个实例是供春在依赖到测试注入。您可能缺少组件扫描您的服务包 - <context:component-scan base-package="..">

更新:根据您的更新 ,和这个职位 - Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' defined in ServletContext resource,它看起来像你必须改变你的BaseService,有一个二传手道而不是使用构造函数设置。使用Spring AOP的CGLIB可能不适用于非默认构造函数

+0

查看上面更新的部分 – dtrunk

+0

谢谢,我会在家里尝试一个二传手。 – dtrunk

+0

你能举个例子吗?请参阅UPDATE2 – dtrunk

0

您至少应该使用@Component注释您的类,以使它们符合自动布线注入条件。

+0

我正在使用'@ Repository','@ Service'和'@ Controller'来代替。但在我的Test类中,我不能使用“@ Controller”注释,不是吗?这是一个JUnit测试课程。 – dtrunk

相关问题