Spring自动连线异常
这里是错误 Deployment Error for module: EBlood: Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'categoryService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.iu.eblood.daoImp.CategoryDaoImp com.iu.eblood.serviceImp.CategoryService.categoryDaoImp; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.iu.eblood.daoImp.CategoryDaoImp] 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), @org.springframework.beans.factory.annotation.Qualifier(value=categorydaoimp)}. Please see server.log for more details. 模块:EBlood的部署错误:部署期间发生错误: 加载应用程序时出现异常:java.lang.IllegalStateException:ContainerBase.addChild:start:org.apache.catalina.LifecycleException:org.springframework.beans.factory.BeanCreationException: 创建名为“categoryService”的bean时出错:自动连线依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException: 无法自动连线字段:public com.iu.eblood.daoImp.CategoryDaoImp com.iu.eblood.serviceImp.CategoryService.CategoryDaoImp;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException: 找不到依赖项类型为[com.iu.eblood.daoImp.CategoryDaoImp]的匹配bean:应至少有1个bean符合此依赖项的autowire候选项的条件。 依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true),@org.springframework.beans.factory.annotation.Qualifier(value=categorydaoimp)}。 有关详细信息,请参阅server.log。 关键是: 找不到依赖项类型为[com.iu.eblood.daoImp.CategoryDaoImp]的匹配bean:应至少有1个bean符合此依赖项的autowire候选项的条件 这意味着您需要在XML或@Component中至少定义一个bean,关键是:Spring自动连线异常,spring,autowired,Spring,Autowired,这里是错误 Deployment Error for module: EBlood: Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.
package com.iu.eblood.dao;
import java.io.Serializable;
import java.util.Collection;
public interface GenericDaoInterface<T,PK extends Serializable> {
/** Persist the newInstance object into database */
PK create(T newInstance);
/** Retrieve an object that was previously persisted to the database using
* the indicated id as primary key
*/
T read(PK id);
/** Save changes made to a persistent object. */
void update(T transientObject);
/** Remove an object from persistent storage in the database */
void delete(T persistentObject);
/*Get all Data giving specific**/
Collection<T> loadAll();
}
package com.iu.eblood.dao;
import java.io.Serializable;
import java.util.Collection;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
public abstract class GenericDao<T,PK extends Serializable>
implements GenericDaoInterface<T, Serializable>
{
@Autowired
protected HibernateTemplate hibernateTemplate;
public GenericDao(Class<T> persistentClass)
{
setPersistentClass(persistentClass);
}
private Class<T> persistentClass;
public Class<T> getPersistentClass() {
return persistentClass;
}
protected void setPersistentClass(Class<T> persistentClass) {
this.persistentClass = persistentClass;
}
public Serializable create(T newInstance) {
return hibernateTemplate.save(newInstance);
}
public T read(Serializable id) {
return (T) hibernateTemplate.get(persistentClass,id);
}
public void update(T transientObject) {
hibernateTemplate.update(transientObject);
}
public void delete(T persistentObject) {
/*
* Update DeletedDate for Entity*/
update(persistentObject);
}
public Collection<T> loadAll() {
return hibernateTemplate.loadAll(persistentClass);
}
@SuppressWarnings("unchecked")
public T loadByField(String fieldName, Object fieldValue) {
return (T) hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(persistentClass).
add(Restrictions.eq(fieldName, fieldValue)).uniqueResult();
}
@SuppressWarnings("unchecked")
public Collection<T> loadAllByField(String fieldName, Object fieldValue) {
return hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(persistentClass).
add(Restrictions.eq(fieldName, fieldValue)).list();
}
}
package com.iu.eblood.daoImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.iu.eblood.dao.GenericDao;
import com.iu.eblood.model.Category;
@Repository("categorydaoimp")
public class CategoryDaoImp extends GenericDao<Category, Long> {
public CategoryDaoImp() {
super(Category.class);
}
}
找不到依赖项类型为[com.iu.eblood.daoImp.CategoryDaoImp]的匹配bean:应至少有1个bean符合此依赖项的autowire候选项的条件
这意味着您需要在XML或@Component包com.iu.eblood.dao中至少定义一个bean;
package com.iu.eblood.dao;
import java.io.Serializable;
import java.util.Collection;
public interface GenericDaoInterface<T,PK extends Serializable> {
/** Persist the newInstance object into database */
PK create(T newInstance);
/** Retrieve an object that was previously persisted to the database using
* the indicated id as primary key
*/
T read(PK id);
/** Save changes made to a persistent object. */
void update(T transientObject);
/** Remove an object from persistent storage in the database */
void delete(T persistentObject);
/*Get all Data giving specific**/
Collection<T> loadAll();
}
package com.iu.eblood.dao;
import java.io.Serializable;
import java.util.Collection;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
public abstract class GenericDao<T,PK extends Serializable>
implements GenericDaoInterface<T, Serializable>
{
@Autowired
protected HibernateTemplate hibernateTemplate;
public GenericDao(Class<T> persistentClass)
{
setPersistentClass(persistentClass);
}
private Class<T> persistentClass;
public Class<T> getPersistentClass() {
return persistentClass;
}
protected void setPersistentClass(Class<T> persistentClass) {
this.persistentClass = persistentClass;
}
public Serializable create(T newInstance) {
return hibernateTemplate.save(newInstance);
}
public T read(Serializable id) {
return (T) hibernateTemplate.get(persistentClass,id);
}
public void update(T transientObject) {
hibernateTemplate.update(transientObject);
}
public void delete(T persistentObject) {
/*
* Update DeletedDate for Entity*/
update(persistentObject);
}
public Collection<T> loadAll() {
return hibernateTemplate.loadAll(persistentClass);
}
@SuppressWarnings("unchecked")
public T loadByField(String fieldName, Object fieldValue) {
return (T) hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(persistentClass).
add(Restrictions.eq(fieldName, fieldValue)).uniqueResult();
}
@SuppressWarnings("unchecked")
public Collection<T> loadAllByField(String fieldName, Object fieldValue) {
return hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(persistentClass).
add(Restrictions.eq(fieldName, fieldValue)).list();
}
}
package com.iu.eblood.daoImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.iu.eblood.dao.GenericDao;
import com.iu.eblood.model.Category;
@Repository("categorydaoimp")
public class CategoryDaoImp extends GenericDao<Category, Long> {
public CategoryDaoImp() {
super(Category.class);
}
}
导入java.io.Serializable;
导入java.util.Collection;
公共接口GenericDaoInterface{
/**将newInstance对象持久化到数据库中*/
PK-create(T-newInstance);
/**检索以前使用保存到数据库的对象
*指定的id作为主键
*/
T读取(pkid);
/**保存对持久对象所做的更改*/
无效更新(T transientObject);
/**从数据库中的持久存储中删除对象*/
void delete(T persistentObject);
/*获取所有数据,给出具体的**/
集合loadAll();
}
包com.iu.eblood.dao;
导入java.io.Serializable;
导入java.util.Collection;
导入org.hibernate.criteria.Restrictions;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.orm.hibernate3.HibernateTemplate;
公共抽象类GenericDao
实现GenericDao接口
{
@自动连线
受保护的HibernateTemplate HibernateTemplate;
公共泛型DAO(类persistentClass)
{
setPersistentClass(persistentClass);
}
私有类persistentClass;
公共类getPersistentClass(){
返回persistentClass;
}
受保护的void setPersistentClass(类persistentClass){
this.persistentClass=persistentClass;
}
公共可序列化创建(T newInstance){
返回hibernateTemplate.save(newInstance);
}
公共T读(可序列化id){
return(T)hibernateTemplate.get(persistentClass,id);
}
公共无效更新(T transientObject){
hibernateTemplate.update(transientObject);
}
公共无效删除(T persistentObject){
/*
*更新实体的DeletedDate*/
更新(persistentObject);
}
公共集合loadAll(){
返回hibernateTemplate.loadAll(persistentClass);
}
@抑制警告(“未选中”)
公共T loadByField(字符串字段名、对象字段值){
返回(T)hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(persistentClass)。
添加(Restrictions.eq(fieldName,fieldValue)).uniqueResult();
}
@抑制警告(“未选中”)
公共集合loadAllByField(字符串字段名、对象字段值){
返回hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(persistentClass)。
添加(Restrictions.eq(fieldName,fieldValue)).list();
}
}
包com.iu.eblood.daoImp;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.orm.hibernate3.HibernateTemplate;
导入org.springframework.stereotype.Repository;
导入com.iu.eblood.dao.GenericDao;
导入com.iu.eblood.model.Category;
@存储库(“categorydaoimp”)
公共类CategoryDaoImp扩展了GenericDao{
公共类别daoimp(){
超级(类别、类别);
}
}
Appcontext内容
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="DB2DataSource" />
<property name="packagesToScan" value="com.iu.eblood.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
org.hibernate.dialogue.mysql5innodbdialogue
真的
真的
更新
包com.iu.eblood.dao;
导入java.io.Serializable;
导入java.util.Collection;
公共接口GenericDaoInterface{
/**将newInstance对象持久化到数据库中*/
PK-create(T-newInstance);
/**检索以前使用保存到数据库的对象
*指定的id作为主键
*/
T读取(pkid);
/**保存对持久对象所做的更改*/
无效更新(T transientObject);
/**从数据库中的持久存储中删除对象*/
void delete(T persistentObject);
/*获取所有数据,给出具体的**/
集合loadAll();
}
包com.iu.eblood.dao;
导入java.io.Serializable;
导入java.util.Collection;
导入org.hibernate.criteria.Restrictions;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.orm.hibernate3.HibernateTemplate;
公共抽象类GenericDao
实现GenericDao接口
{
@自动连线
受保护的HibernateTemplate HibernateTemplate;
公共泛型DAO(类persistentClass)
{
setPersistentClass(persistentClass);
}
私有类persistentClass;
公共类getPersistentClass(){
返回persistentClass;
}
受保护的void setPersistentClass(类persistentClass){
this.persistentClass=persistentClass;
}
公共可序列化创建(T newInstance){
返回hibernateTemplate.save(newInstance);
}
@Repository
@Repository("categorydaoimp")