我正在努力实现与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上下文确实包含以下行:<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 );
}
}
答案 0 :(得分:1)
看起来,类似服务的实例不适用于Spring将依赖项注入到测试中。您可能缺少服务包中的组件扫描 - <context:component-scan base-package="..">
更新的 根据您的更新和此帖子Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' defined in ServletContext resource,您似乎必须更改BaseService,以便为dao设置setter而不是使用构造函数进行设置。使用Spring AOP的CGLIB可能无法与非默认构造函数
一起使用答案 1 :(得分:0)
您应该至少使用@Component为您的课程添加注释,以便他们有资格进行自动注射。