请大家帮忙看看ssh配置,为什么dao无法注入呢?

报错信息:
java.lang.NullPointerException (dao为null,我打印测试过这个)
com.bankond.base.service.impl.GenericServiceImpl.save(GenericServiceImpl.java:61)
com.bankond.action.RegisterAction.execute(RegisterAction.java:54)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
。。。。
。。。。

[img]http://dl.iteye.com/upload/attachment/487994/34d41de7-41c7-3c05-972f-faddaf31afa9.jpg[/img]

[code="java"]public interface GenericDao {
public abstract T save(T paramT);

  public abstract T merge(T paramT);

  public abstract T get(PK paramPK);

  public abstract void remove(PK paramPK);

  public abstract void remove(T paramT);

  public abstract void evict(T paramT);

  public abstract List<T> getAll();

 // public abstract List<T> getAll(PagingBean paramPagingBean);

  //public abstract List<T> getAll(QueryFilter paramQueryFilter);

  public abstract List<T> findByHql(String paramString, Object[] paramArrayOfObject);

  //public abstract List<T> findByHql(String paramString, Object[] paramArrayOfObject, PagingBean paramPagingBean);

  public abstract List<T> findByHql(String paramString, Object[] paramArrayOfObject, int paramInt1, int paramInt2);

  public abstract void flush();

}

[/code]

[code="java"]public class GenericDaoImplextends
HibernateDaoSupport implements GenericDao {
protected Log logger = LogFactory.getLog(GenericDaoImpl.class);

protected JdbcTemplate jdbcTemplate;

protected Class persistType;

protected Map<String, String> querys = new HashMap();

public GenericDaoImpl(){}
public GenericDaoImpl(Class persistType) {
    this.persistType = persistType;
}

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

public void setPersistType(Class persistType) {
    this.persistType = persistType;
}



public void evict(T entity) {
    getHibernateTemplate().evict(entity);
}

public List<T> findByHql(final String hql, final Object[] objs) {
    return (List) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session)
                throws HibernateException, SQLException {
            Query query = session.createQuery(hql);
            if (objs != null) {
                for (int i = 0; i < objs.length; ++i) {
                    query.setParameter(i, objs[i]);
                }
            }
            return query.list();
        }
    });
}

public List<T> findByHql(final String hql, final Object[] objs,
        final int firstResult, final int pageSize) {
    return (List) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session)
                throws HibernateException, SQLException {
            Query query = session.createQuery(hql);
            query.setFirstResult(firstResult).setMaxResults(pageSize);
            if (objs != null) {
                for (int i = 0; i < objs.length; ++i) {
                    query.setParameter(i, objs[i]);
                }
            }
            return query.list();
        }
    });

}

public List<T> findByHql(String hql) {
    return findByHql(hql, null);
}

public void flush() {
    getHibernateTemplate().flush();
    }

public T get(PK id) {
    return (T) getHibernateTemplate().get(this.persistType, id);
}

public List<T> getAll() {
    return (List) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session)
                throws HibernateException, SQLException {
            String hql = "from "
                    + GenericDaoImpl.this.persistType.getName();
            Query query = session.createQuery(hql);
            return query.list();
        }
    });
}

public T merge(T entity) {
    getHibernateTemplate().merge(entity);
return entity;
}

public void remove(PK id) {
    getHibernateTemplate().delete(get(id));
    }

public void remove(T entity) {
    getHibernateTemplate().delete(entity);
    }

public T save(T entity) {
    getHibernateTemplate().saveOrUpdate(entity);
    return entity;
}

}[/code]

[code="java"]public abstract interface GenericService {
public abstract T save(T paramT);

  public abstract T merge(T paramT);

  public abstract void evict(T paramT);

  public abstract T get(PK paramPK);

  public abstract List<T> getAll();

  //public abstract List<T> getAll(PagingBean paramPagingBean);

  //public abstract List<T> getAll(QueryFilter paramQueryFilter);

  public abstract void remove(PK paramPK);

  public abstract void remove(T paramT);

  public abstract void flush();

}[/code]

[code="java"]public class GenericServiceImpl
implements GenericService {

protected Log logger = LogFactory.getLog(GenericServiceImpl.class);

public GenericServiceImpl(){}

protected GenericDao<T, Serializable> dao = null;
public GenericServiceImpl(GenericDao dao) {
      this.dao = dao;
}

public void setDao(GenericDao dao) {
    this.dao = dao;
}

public void evict(T entity) {
    this.dao.evict(entity);

}

public void flush() {
    this.dao.flush();

}

public T get(PK id) {
    return this.dao.get(id);
}

public List<T> getAll() {
    return this.dao.getAll();
}

public T merge(T entity) {
    return this.dao.merge(entity);
}

public void remove(PK id) {
    this.dao.remove(id);
}

public void remove(T paramT) {
    // TODO 自动生成方法存根

}

public T save(T entity) {
    System.out.println("传进来的entity:"+entity+"<==>这里的dao是:"+this.dao);
    return this.dao.save(entity);
}

}[/code]

[code="java"]
public interface BaseService extends GenericService{
}
[/code]
[code="java"]public class BaseServiceImpl extends GenericServiceImpl implements BaseService{
public BaseServiceImpl(){}
public BaseServiceImpl(GenericDao dao) {
super(dao);
}
}[/code]

8个回答

问题清楚了,你在RegisterService里set了RegisterDao,但是没有set GenericDao,当然是null了……
在RegisterServiceImpl里的setRegicterDao加上一句:
[code="java"]
public void setRegisterDao(RegisterDao registerDao) {
this.registerDao = registerDao;
setDao(registerDao);
}
[/code]
话说,你项目组织的太复杂、太乱,引用、继承关系搞得乱七八糟的……
建议重构……

[quote][code="java"]
public void setDao(GenericDao dao) {

this.dao = dao;

}

[/code][/quote]
spring应该这样配置:
[code="xml"]



[/code]
……这就是不遵循标准的下场……

这是因为你GenericServiceImpl类中的
[code="java"]protected GenericDao dao = null;[/code]dao跟你配置文件[code="java"]



[/code]中的名字不一样造成的,配置文件的意思就是说,在GenericServiceImpl中有一个GenericDao字段,而实际在GenericServiceImpl类中你只有dao字段,所以spring无法注入。你只要将两个名字改成一样就OK了。希望对你有帮助。 :D

这应该是因为你没有把配置文件托管给容器造成的。web.xml中要先将这些配置文件加载到容器中
[code="java"]
contextConfigLocation
classpath:conf/spring/applicationContext.xml, classpath:conf/spring/daoContext-*.xml, classpath:conf/spring/serviceContext-*.xml, classpath:conf/spring/actionContext-*.xml
[/code]

[quote]
谢谢,按照您说的改了,但还是注入不进来,您看我的配置和代码还有其他的问题吗?我只要打印dao不为null就可以了
[/quote]
问题有很多,比如:dao里persistType没有注入,spring bean配置scop不应当用"prototype",id属性应当遵循变量名命名规则等等……
不过这些都不应该导致dao无法注入才对……
楼主最好把改过的GenericServiceImpl和spring配置文件(包括sessionFacroty)再传上来看看。

提交表单时,你的action有响应吗?action里面的service有值吗?因为整个的过程都是tomcat来处理的(包括调用哪个action,怎么注入实例等等),如果你不从action开始跑起来,那么你的dao永远都是null。或者你自己在service里面写一个方法来加载application.xml文件,这样的话dao肯定不为空。不知道你明白我的意思没? :o

贴一下你修改好的代码我看看 :D

action在spring里是怎么配的?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问