0 0

baseDaoImpl无法实例化15

    package com.sshFrame.zero.dao; 
     
    import java.io.Serializable; 
    import java.lang.reflect.Field; 
    import java.lang.reflect.ParameterizedType; 
    import java.lang.reflect.Type; 
    import java.util.List; 
     
    import javax.persistence.Id; 
     
    import org.hibernate.Criteria; 
    import org.hibernate.HibernateException; 
    import org.hibernate.Query; 
    import org.hibernate.Session; 
    import org.hibernate.SessionFactory; 
    import org.hibernate.criterion.Example; 
    import org.springframework.beans.factory.annotation.Autowired; 
     
    /** 
     * 基于hibernate的BaseDaoImpl 
     * Spring3对Hibernate4已经没有了HibernateDaoSupport和HibernateTemplate的支持,使用了原生态的API 
     * @author 雪精灵 
     * 
     * @param <T> 
     */ 
    public class BaseDaoImpl<T extends Serializable> implements BaseDao { 
        @Autowired 
        private SessionFactory sessionFactory; 
        //当前泛型类 
        @SuppressWarnings("rawtypes") 
        private Class entityClass; 
        //当前主键名称 
        private String pkname; 
        private final String HQL_LIST_ALL; 
        private final String HQL_COUNT_ALL; 
        @SuppressWarnings("rawtypes") 
        public Class getEntityClass() { 
            return entityClass; 
        } 
        @SuppressWarnings("rawtypes") 
        public void setEntityClass(Class entityClass) { 
            this.entityClass = entityClass; 
        } 
         
        @SuppressWarnings("rawtypes") 
        public BaseDao() { 
            //获取当前泛型类 
            Type type = this.getClass().getGenericSuperclass(); 
            if (type.toString().indexOf("BaseDao") != -1) { 
                ParameterizedType type1 = (ParameterizedType) type; 
                Type[] types = type1.getActualTypeArguments(); 
                setEntityClass((Class) types[0]); 
            }else{ 
                type = ((Class)type).getGenericSuperclass(); 
                ParameterizedType type1 = (ParameterizedType) type; 
                Type[] types = type1.getActualTypeArguments(); 
                setEntityClass((Class) types[0]); 
            } 
            getPkname(); 
            HQL_LIST_ALL="from "+this.entityClass.getSimpleName()+" order by "+pkname+" desc"; 
            HQL_COUNT_ALL="select count(*) from "+this.entityClass.getSimpleName(); 
        } 
        /** 
         * 获取主键名称 
         * @return 
         */ 
        public String getPkname() { 
            Field[] fields = this.entityClass.getDeclaredFields();//反射类字段 
            for (Field field : fields) { 
                field.isAnnotationPresent(Id.class); 
                this.pkname=field.getName(); 
                break; 
            } 
            return pkname; 
        } 
        /** 
         * 保存实例 
         *  
         * @param t 
         * @throws HibernateException 
         */ 
        public void save(T t) throws HibernateException{ 
            Session session=null; 
            try { 
                session=sessionFactory.openSession(); 
                session.beginTransaction(); 
                session.save(t); 
                session.getTransaction().commit(); 
            } catch (HibernateException e) { 
                e.printStackTrace(); 
                throw new HibernateException(e); 
            }finally{ 
                session.close(); 
            } 
        } 
        /** 
         * 修改实例 
         *  
         * @param t 
         * @throws HibernateException 
         */ 
        public void update(T t) throws HibernateException{ 
            Session session=null; 
            try { 
                session=sessionFactory.openSession(); 
                session.beginTransaction(); 
                session.update(t); 
                session.getTransaction().commit(); 
            } catch (HibernateException e) { 
                e.printStackTrace(); 
                throw new HibernateException(e); 
            }finally{ 
                session.close(); 
            } 
        } 
         
        /** 
         * 删除实例 
         *  
         * @param t 
         * @throws HibernateException 
         */ 
        public void delete(T t) throws HibernateException{ 
            Session session=null; 
            try { 
                session=sessionFactory.openSession(); 
                session.beginTransaction(); 
                session.delete(t); 
                session.getTransaction().commit(); 
            } catch (HibernateException e) { 
                e.printStackTrace(); 
                throw new HibernateException(e); 
            }finally{ 
                session.close(); 
            } 
        } 
        /** 
         * 获取实例 
         *  
         * @param id 
         * @throws HibernateException 
         */ 
        @SuppressWarnings("unchecked") 
        public T get(Serializable id) throws Exception{ 
            Session session=null; 
            T t=null; 
            try { 
                session=sessionFactory.openSession(); 
                session.beginTransaction(); 
                t=(T) session.get(getEntityClass(), id); 
                session.getTransaction().commit(); 
            } catch (HibernateException e) { 
                e.printStackTrace(); 
                throw new HibernateException(e); 
            }finally{ 
                session.close(); 
            } 
            return t; 
        } 
        /** 
         * 查询全部 
         *  
         * @throws HibernateException 
         */ 
        @SuppressWarnings("unchecked") 
        public List<T> findAll() throws Exception { 
            List<T> list=null; 
            Session session=null; 
            try { 
                session = sessionFactory.openSession(); 
                session.beginTransaction(); 
                Query query = session.createQuery(HQL_LIST_ALL); 
                list = query.list(); 
                session.getTransaction().commit(); 
            } catch (HibernateException e) { 
                e.printStackTrace(); 
            }finally{ 
                session.close(); 
            } 
            return list; 
        } 
        /** 
         * 查询总数 
         *  
         * @throws HibernateException 
         */ 
        public Integer findAllCount() throws Exception { 
            Session session=null; 
            Integer count=0; 
            try { 
                session = sessionFactory.openSession(); 
                session.beginTransaction(); 
                Query query = session.createQuery(HQL_COUNT_ALL); 
                List<?> list = query.list(); 
                session.getTransaction().commit(); 
                if(list!=null&&!list.isEmpty()){ 
                    count=((Integer) list.get(0)).intValue(); 
                } 
            } catch (HibernateException e) { 
                e.printStackTrace(); 
            }finally{ 
                session.close(); 
            } 
            return count; 
        } 
        /** 
         * QBC查询 
         *  
         * @param criteria 
         * @throws HibernateException 
         */ 
        @SuppressWarnings("unchecked") 
        public List<T> findByCriteria(Criteria criteria) throws Exception { 
            List<T> list=null; 
            Session session=null; 
            try { 
                session = sessionFactory.openSession(); 
                session.beginTransaction(); 
                Criteria criteria1 = session.createCriteria(getEntityClass()); 
                criteria1=criteria; 
                list = criteria1.list(); 
                session.getTransaction().commit(); 
            } catch (HibernateException e) { 
                e.printStackTrace(); 
            }finally{ 
                session.close(); 
            } 
            return list; 
        } 
         
        /** 
         * QBE查询 
         *  
         * @param t 
         * @throws HibernateException 
         */ 
        @SuppressWarnings("unchecked") 
        public List<T> findByExample(T t) throws Exception { 
            List<T> list=null; 
            Session session=null; 
            Example example = Example.create(t); 
            try { 
                session = sessionFactory.openSession(); 
                session.beginTransaction(); 
                Criteria criteria = session.createCriteria(getEntityClass()); 
                criteria.add(example); 
                list = criteria.list(); 
                session.getTransaction().commit(); 
            } catch (HibernateException e) { 
                e.printStackTrace(); 
            }finally{ 
                session.close(); 
            } 
            return list; 
        } 
        /** 
         * HQL查询 
         *  
         * @param hql 
         * @param objects 
         * @throws HibernateException 
         */ 
        @SuppressWarnings("unchecked") 
        public List<Object[]> findByHql(String hql,final Object...objects) throws Exception{ 
            List<Object[]> list=null; 
            Session session=null; 
            try { 
                session=sessionFactory.openSession(); 
                session.beginTransaction(); 
                Query query = session.createQuery(hql); 
                for (int i = 0; i < objects.length; i++) { 
                    query.setParameter(i, objects[i]); 
                } 
                list = query.list(); 
                session.getTransaction().commit(); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            }finally{ 
                session.close(); 
            } 
            return list; 
        } 
        /** 
         * SQL查询 
         *  
         * @param hql 
         * @param objects 
         * @throws HibernateException 
         */ 
        @SuppressWarnings("unchecked") 
        public List<Object[]> findBySql(String sql,final Object...objects){ 
            List<Object[]> list=null; 
            Session session=null; 
            try { 
                session=sessionFactory.openSession(); 
                session.beginTransaction(); 
                Query query = sessionFactory.getCurrentSession().createSQLQuery(sql); 
                for (int i = 0; i < objects.length; i++) { 
                    query.setParameter(i, objects[i]); 
                } 
                list = query.list(); 
                session.getTransaction().commit(); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            }finally{ 
                session.close(); 
            } 
            return list; 
        } 
    } 

问题补充:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 引入项目配置文件 -->
<!-- 自动扫描dao和service包(自动注入) -->
<context:component-scan base-package="com.ins.dao,com.ins.service,com.ins.action" />
</beans>
hibernate.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 配置数据源 -->
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/Ins" />
<property name="username" value="root" />
<property name="password" value="admin" />

<!-- 初始化连接大小 -->
<property name="initialSize" value="0" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />

<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" />
</bean>
<!-- 配置hibernate session工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>

<!-- 自动扫描注解方式配置的hibernate类文件 -->
<property name="packagesToScan">
<list>
<value>com.ins.pojo</value>
</list>
</property>

<!-- 自动扫描hbm方式配置的hibernate文件和.hbm文件 -->
<!-- <property name="mappingDirectoryLocations"> <list> <value>classpath:sy/hbm</value> </list> </property> -->
</bean>

<!-- 配置事务管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 注解方式配置事物 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->

<!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="saveOrUpdate*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="grant*" propagation="REQUIRED" />

<tx:method name="init*" propagation="REQUIRED" />

<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 第一个*代表所有的返回值类型;第二个*代表所有的类;第三个*代表类所有方法;..代表子或者孙子包;最后一个..代表所有的参数 -->
<aop:pointcut id="transactionPointcut" expression="(execution(* com.ins.service..*Impl.*(..)))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>

</beans>
BaseDao.java
package com.ins.dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.springframework.stereotype.Repository;
@Repository
public interface BaseDao<T extends Serializable>
{
/** 
     * 保存实例 
     *  
     * @param t 
     * @throws HibernateException 
     */ 
public void save(T t) throws HibernateException;
/** 
     * 修改实例 
     *  
     * @param t 
     * @throws HibernateException 
     */ 
public void update(T t) throws HibernateException;
/** 
     * 删除实例 
     *  
     * @param t 
     * @throws HibernateException 
     */ 
public void delete(T t) throws HibernateException;
/** 
     * 获取实例 
     *  
     * @param id 
     * @throws HibernateException 
     */ 
public T get(Serializable id) throws Exception;
/** 
     * 查询全部 
     *  
     * @throws HibernateException 
     */ 
public List<T> findAll() throws Exception;
/** 
     * 查询总数 
     *  
     * @throws HibernateException 
     */ 
public Integer findAllCount() throws Exception;
/** 
     * QBC查询 
     *  
     * @param criteria 
     * @throws HibernateException 
     */ 
public List<T> findByCriteria(Criteria criteria) throws Exception;
/** 
     * QBE查询 
     *  
     * @param t 
     * @throws HibernateException 
     */ 
public List<T> findByExample(T t) throws Exception;
/** 
     * HQL查询 
     *  
     * @param hql 
     * @param objects 
     * @throws HibernateException 
     */ 
public List<Object[]> findByHql(String hql,final Object...objects) throws Exception;
/** 
     * HQL查询 
     *  
     * @param hql 
     * @throws HibernateException 
     */ 
public List<T> findByHql(String hql) throws Exception;
/** 
     * SQL查询 
     *  
     * @param hql 
     * @param objects 
     * @throws HibernateException 
     */ 
public List<Object[]> findBySql(String sql,final Object...objects);
}
BaseDaoImpl.java
package com.ins.dao.impl;
import java.io.Serializable; 
import java.lang.reflect.Field; 
import java.lang.reflect.ParameterizedType; 
import java.lang.reflect.Type; 
import java.util.List; 
import javax.persistence.Id; 
import org.hibernate.Criteria; 
import org.hibernate.HibernateException; 
import org.hibernate.Query; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.criterion.Example; 
import org.springframework.beans.factory.annotation.Autowired; 
import com.ins.dao.BaseDao;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional
/** 
* 基于hibernate的BaseDaoImpl 
* Spring3对Hibernate4已经没有了HibernateDaoSupport和HibernateTemplate的支持,使用了原生态的API 

* @param <T> 
*/ 
public class BaseDaoImpl<T extends Serializable> implements BaseDao<T>

    private SessionFactory sessionFactory; 
    public SessionFactory getSessionFactory()
{
return sessionFactory;
}
    @Autowired 
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
//当前泛型类 
    @SuppressWarnings("rawtypes") 
    private Class entityClass; 
    //当前主键名称 
    private String pkname; 
    private final String HQL_LIST_ALL; 
    private final String HQL_COUNT_ALL; 
    @SuppressWarnings("rawtypes") 
    public Class getEntityClass() { 
        return entityClass; 
    } 
    @SuppressWarnings("rawtypes") 
    public void setEntityClass(Class entityClass) { 
        this.entityClass = entityClass; 
    } 
    @SuppressWarnings("rawtypes") 
    public BaseDaoImpl() { 
    Type type = this.getClass().getGenericSuperclass(); 
        if (type.toString().indexOf("BaseDaoImpl") != -1) { 
            ParameterizedType type1 = (ParameterizedType) type; 
            Type[] types = type1.getActualTypeArguments(); 
            setEntityClass((Class) types[0]); 
        }else{ 
            type = ((Class)type).getGenericSuperclass(); 
            ParameterizedType type1 = (ParameterizedType) type; 
            Type[] types = type1.getActualTypeArguments(); 
            setEntityClass((Class) types[0]); 
        } 
        getPkname(); 
        HQL_LIST_ALL="from "+this.entityClass.getSimpleName()+" order by "+pkname+" desc"; 
        HQL_COUNT_ALL="select count(*) from "+this.entityClass.getSimpleName(); 
    } 
    /** 
     * 获取主键名称 
     * @return 
     */ 
    public String getPkname() { 
        Field[] fields = this.entityClass.getDeclaredFields();//反射类字段 
        for (Field field : fields) { 
            field.isAnnotationPresent(Id.class); 
            this.pkname=field.getName(); 
            break; 
        } 
        return pkname; 
    } 
    /** 
     * 保存实例 
     *  
     * @param t 
     * @throws HibernateException 
     */ 
    public void save(T t) throws HibernateException{ 
        Session session=null; 
        try { 
            session=sessionFactory.openSession(); 
            session.beginTransaction(); 
            session.save(t); 
            session.getTransaction().commit(); 
        } catch (HibernateException e) { 
            e.printStackTrace(); 
            throw new HibernateException(e); 
        }finally{ 
            session.close(); 
        } 
    } 
    /** 
     * 修改实例 
     *  
     * @param t 
     * @throws HibernateException 
     */ 
    public void update(T t) throws HibernateException{ 
        Session session=null; 
        try { 
            session=sessionFactory.openSession(); 
            session.beginTransaction(); 
            session.update(t); 
            session.getTransaction().commit(); 
        } catch (HibernateException e) { 
            e.printStackTrace(); 
            throw new HibernateException(e); 
        }finally{ 
            session.close(); 
        } 
    } 
    /** 
     * 删除实例 
     *  
     * @param t 
     * @throws HibernateException 
     */ 
    public void delete(T t) throws HibernateException{ 
        Session session=null; 
        try { 
            session=sessionFactory.openSession(); 
            session.beginTransaction(); 
            session.delete(t); 
            session.getTransaction().commit(); 
        } catch (HibernateException e) { 
            e.printStackTrace(); 
            throw new HibernateException(e); 
        }finally{ 
            session.close(); 
        } 
    } 
    /** 
     * 获取实例 
     *  
     * @param id 
     * @throws HibernateException 
     */ 
    @SuppressWarnings("unchecked") 
    public T get(Serializable id) throws Exception{ 
        Session session=null; 
        T t=null; 
        try { 
            session=sessionFactory.openSession(); 
            session.beginTransaction(); 
            t=(T) session.get(getEntityClass(), id); 
            session.getTransaction().commit(); 
        } catch (HibernateException e) { 
            e.printStackTrace(); 
            throw new HibernateException(e); 
        }finally{ 
            session.close(); 
        } 
        return t; 
    } 
    /** 
     * 查询全部 
     *  
     * @throws HibernateException 
     */ 
    @SuppressWarnings("unchecked") 
    public List<T> findAll() throws Exception { 
        List<T> list=null; 
        Session session=null; 
        try { 
            session = sessionFactory.openSession(); 
            session.beginTransaction(); 
            Query query = session.createQuery(HQL_LIST_ALL); 
            list = query.list(); 
            session.getTransaction().commit(); 
        } catch (HibernateException e) { 
            e.printStackTrace(); 
        }finally{ 
            session.close(); 
        } 
        return list; 
    } 
    /** 
     * 查询总数 
     *  
     * @throws HibernateException 
     */ 
    public Integer findAllCount() throws Exception { 
        Session session=null; 
        Integer count=0; 
        try { 
            session = sessionFactory.openSession(); 
            session.beginTransaction(); 
            Query query = session.createQuery(HQL_COUNT_ALL); 
            List<?> list = query.list(); 
            session.getTransaction().commit(); 
            if(list!=null&&!list.isEmpty()){ 
                count=((Integer) list.get(0)).intValue(); 
            } 
        } catch (HibernateException e) { 
            e.printStackTrace(); 
        }finally{ 
            session.close(); 
        } 
        return count; 
    } 
    /** 
     * QBC查询 
     *  
     * @param criteria 
     * @throws HibernateException 
     */ 
    @SuppressWarnings("unchecked") 
    public List<T> findByCriteria(Criteria criteria) throws Exception { 
        List<T> list=null; 
        Session session=null; 
        try { 
            session = sessionFactory.openSession(); 
            session.beginTransaction(); 
            Criteria criteria1 = session.createCriteria(getEntityClass()); 
            criteria1=criteria; 
            list = criteria1.list(); 
            session.getTransaction().commit(); 
        } catch (HibernateException e) { 
            e.printStackTrace(); 
        }finally{ 
            session.close(); 
        } 
        return list; 
    } 
    /** 
     * QBE查询 
     *  
     * @param t 
     * @throws HibernateException 
     */ 
    @SuppressWarnings("unchecked") 
    public List<T> findByExample(T t) throws Exception { 
        List<T> list=null; 
        Session session=null; 
        Example example = Example.create(t); 
        try { 
            session = sessionFactory.openSession(); 
            session.beginTransaction(); 
            Criteria criteria = session.createCriteria(getEntityClass()); 
            criteria.add(example); 
            list = criteria.list(); 
            session.getTransaction().commit(); 
        } catch (HibernateException e) { 
            e.printStackTrace(); 
        }finally{ 
            session.close(); 
        } 
        return list; 
    } 
    /** 
     * HQL查询 
     *  
     * @param hql 
     * @param objects 
     * @throws HibernateException 
     */ 
    @SuppressWarnings("unchecked") 
    public List<Object[]> findByHql(String hql,final Object...objects) throws Exception{ 
        List<Object[]> list=null; 
        Session session=null; 
        try { 
            session=sessionFactory.openSession(); 
            session.beginTransaction(); 
            Query query = session.createQuery(hql); 
            for (int i = 0; i < objects.length; i++) { 
                query.setParameter(i, objects[i]); 
            } 
            list = query.list(); 
            session.getTransaction().commit(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        }finally{ 
            session.close(); 
        } 
        return list; 
    } 
    /** 
     * HQL查询 
     *  
     * @param hql 
     * @throws HibernateException 
     */ 
    @SuppressWarnings("unchecked") 
    public List<T> findByHql(String hql) throws Exception{ 
        List<T> list=null; 
        Session session=null; 
        try { 
            session=sessionFactory.openSession(); 
            session.beginTransaction(); 
            Query query = session.createQuery(hql); 
            list = query.list(); 
            session.getTransaction().commit(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        }finally{ 
            session.close(); 
        } 
        return list; 
    } 
    /** 
     * SQL查询 
     *  
     * @param hql 
     * @param objects 
     * @throws HibernateException 
     */ 
    @SuppressWarnings("unchecked") 
    public List<Object[]> findBySql(String sql,final Object...objects){ 
        List<Object[]> list=null; 
        Session session=null; 
        try { 
            session=sessionFactory.openSession(); 
            session.beginTransaction(); 
            Query query = sessionFactory.getCurrentSession().createSQLQuery(sql); 
            for (int i = 0; i < objects.length; i++) { 
                query.setParameter(i, objects[i]); 
            } 
            list = query.list(); 
            session.getTransaction().commit(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        }finally{ 
            session.close(); 
        } 
        return list; 
    } 

BaseService.java
package com.ins.service;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
@Resource
public interface BaseService<T extends Serializable>
{
/** 
     * 保存实例 
     *  
     * @param t 
     * @throws HibernateException 
     */ 
public void save(T t) throws Exception;
/** 
     * 修改实例 
     *  
     * @param t 
     * @throws HibernateException 
     */ 
public void update(T t) throws Exception;
/** 
     * 删除实例 
     *  
     * @param t 
     * @throws HibernateException 
     */ 
public void delete(T t) throws Exception;
/** 
     * 获取实例 
     *  
     * @param id 
     * @throws HibernateException 
     */ 
public T get(Serializable id) throws Exception;
/** 
     * 查询全部 
     *  
     * @throws HibernateException 
     */ 
public List<T> findAll() throws Exception;
/** 
     * 查询总数 
     *  
     * @throws HibernateException 
     */ 
public Integer findAllCount() throws Exception;
/** 
     * QBC查询 
     *  
     * @param criteria 
     * @throws HibernateException 
     */ 
public List<T> findByCriteria(Criteria criteria) throws Exception;
/** 
     * QBE查询 
     *  
     * @param t 
     * @throws HibernateException 
     */ 
public List<T> findByExample(T t) throws Exception;
/** 
     * HQL查询 
     *  
     * @param hql 
     * @param objects 
     * @throws HibernateException 
     */ 
public List<Object[]> findByHql(String hql,final Object...objects) throws Exception;
/** 
     * HQL查询 
     *  
     * @param hql 
     * @throws HibernateException 
     */ 
public List<T> findByHql(String hql) throws Exception;
/** 
     * SQL查询 
     *  
     * @param hql 
     * @param objects 
     * @throws HibernateException 
     */ 
public List<Object[]> findBySql(String sql,final Object...objects);
}
BaseServiceImpl.java
package com.ins.service.impl;
import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ins.dao.impl.BaseDaoImpl;
import com.ins.service.BaseService;
@Service("baseServiceImpl")
public class BaseServiceImpl<T extends Serializable> implements BaseService<T>
{
@Resource
private BaseDaoImpl<T> baseDao;
public BaseDaoImpl<T> getBaseDao()
{
return baseDao;
}
@Autowired
public void setBaseDao(BaseDaoImpl<T> baseDao)
{
this.baseDao = baseDao;
}
@Override
public void save(T t) throws Exception
{
baseDao.save(t);
}
@Override
public void update(T t) throws Exception
{
baseDao.update(t);
}
@Override
public void delete(T t) throws Exception
{
baseDao.delete(t);
}
@Override
public T get(Serializable id) throws Exception
{
return baseDao.get(id);
}
@Override
public List<T> findAll() throws Exception
{
return baseDao.findAll();
}
@Override
public Integer findAllCount() throws Exception
{
return baseDao.findAllCount();
}
@Override
public List<T> findByCriteria(Criteria criteria) throws Exception
{
// TODO Auto-generated method stub
return baseDao.findByCriteria(criteria);
}
@Override
public List<T> findByExample(T t) throws Exception
{
// TODO Auto-generated method stub
return baseDao.findByExample(t);
}
@Override
public List<Object[]> findByHql(String hql, Object... objects)
throws Exception
{
return baseDao.findByHql(hql, objects);
}
@Override
public List<T> findByHql(String hql) throws Exception
{
return baseDao.findByHql(hql);
}
@Override
public List<Object[]> findBySql(String sql, Object... objects)
{
return baseDao.findBySql(sql, objects);
}
}
BaseAction.java
package com.ins.action;
import java.io.IOException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Action(value="baseAction")
public class BaseAction extends ActionSupport
{
/**
*
*/
private static final long serialVersionUID = 1L;

public void writeJson(Object object)
{
try
{
String json = JSON.toJSONStringWithDateFormat(object,"yyyy-MM-dd HH:mm:ss");
ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
ServletActionContext.getResponse().getWriter().write(json);
ServletActionContext.getResponse().getWriter().flush();
ServletActionContext.getResponse().getWriter().close();
}
catch(IOException e)
{
e.printStackTrace();
}

}
}
MenuDao.java
package com.ins.dao;

import java.io.Serializable;
import java.util.List;
import com.ins.beans.Menu;
import org.springframework.stereotype.Repository;
@Repository
public interface MenuDao<T extends Serializable> extends BaseDao<T>
{
public List<Menu> getTree(String hql) throws Exception;
}
MenuDaoImpl.java
package com.ins.dao;

import java.io.Serializable;
import java.util.List;
import com.ins.beans.Menu;
import org.springframework.stereotype.Repository;
@Repository
public interface MenuDao<T extends Serializable> extends BaseDao<T>
{
public List<Menu> getTree(String hql) throws Exception;
}
MenuService.java
package com.ins.dao;

import java.io.Serializable;
import java.util.List;
import com.ins.beans.Menu;
import org.springframework.stereotype.Repository;
@Repository
public interface MenuDao<T extends Serializable> extends BaseDao<T>
{
public List<Menu> getTree(String hql) throws Exception;
}
MenuServiceImpl.java
package com.ins.dao;

import java.io.Serializable;
import java.util.List;
import com.ins.beans.Menu;
import org.springframework.stereotype.Repository;
@Repository
public interface MenuDao<T extends Serializable> extends BaseDao<T>
{
public List<Menu> getTree(String hql) throws Exception;
}

问题补充:MenuDaoImpl.java
package com.ins.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.ins.beans.Menu;
import com.ins.dao.MenuDao;
import com.ins.pojo.TMenu;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional
public class MenuDaoImpl<T extends Serializable> extends BaseDaoImpl<T> implements MenuDao<T>
{
private BaseDaoImpl<T> baseDao;
public BaseDaoImpl<T> getBaseDao()
{
return baseDao;
}
@Autowired
public void setBaseDao(BaseDaoImpl<T> baseDao)
{
this.baseDao = baseDao;
}
@SuppressWarnings("unchecked")
@Override
public List<Menu> getTree(String hql) throws Exception
{
List<Menu> menu = new ArrayList<Menu>();
List<TMenu> tmenu = (List<TMenu>) baseDao.findByHql("from TMenu t where t.tmenu is null");
if(tmenu !=null && tmenu.size()>0)
{
for(TMenu t:tmenu)
{
Menu m = new Menu();
BeanUtils.copyProperties(t, m);
Set<TMenu> set = t.getTMenus();
if(set !=null && !set.isEmpty())
{
m.setState("closed");
}
else
{
m.setState("open");
}
menu.add(m);
}
}
return menu;
}
}
MenuService.java
package com.ins.service;
import java.util.List;
import javax.annotation.Resource;
import com.ins.beans.Menu;
import com.ins.pojo.TMenu;
@Resource
public interface MenuService extends BaseService<TMenu>
{
public List<Menu> getTree(String hql) throws Exception;
}
MenuServiceImpl.java
package com.ins.service.impl;
import java.util.List;
import org.springframework.stereotype.Service;
import com.ins.beans.Menu;
import com.ins.dao.impl.MenuDaoImpl;
import com.ins.pojo.TMenu;
import com.ins.service.MenuService;
@Service("menuServiceImpl")
public class MenuServiceImpl extends BaseServiceImpl<TMenu> implements MenuService
{
private MenuDaoImpl<TMenu> menuDao;
public MenuDaoImpl<TMenu> getMenuDao()
{
return menuDao;
}
public void setMenuDao(MenuDaoImpl<TMenu> menuDao)
{
this.menuDao = menuDao;
}
@Override
public List<Menu> getTree(String hql) throws Exception
{
return menuDao.getTree(hql);
}
}

问题补充:六月 04, 2014 4:11:58 下午 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\Program Files\Java\jdk1.7.0_55\bin;D:\Program Files\apache-tomcat-7.0.53\bin
六月 04, 2014 4:11:59 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8080"]
六月 04, 2014 4:11:59 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-bio-8009"]
六月 04, 2014 4:11:59 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 2915 ms
六月 04, 2014 4:11:59 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Catalina
六月 04, 2014 4:11:59 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.53
六月 04, 2014 4:11:59 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.53\webapps\docs
六月 04, 2014 4:12:01 下午 org.apache.catalina.util.SessionIdGenerator createSecureRandom
信息: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [235] milliseconds.
六月 04, 2014 4:12:01 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.53\webapps\examples
六月 04, 2014 4:12:03 下午 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
六月 04, 2014 4:12:03 下午 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
六月 04, 2014 4:12:04 下午 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@1426497')
六月 04, 2014 4:12:04 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.53\webapps\host-manager
六月 04, 2014 4:12:04 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.53\webapps\Ins
六月 04, 2014 4:12:42 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://www.springframework.org/tags/form is already defined
六月 04, 2014 4:12:42 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://www.springframework.org/tags is already defined
六月 04, 2014 4:12:42 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/core_rt is already defined
六月 04, 2014 4:12:42 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/core is already defined
六月 04, 2014 4:12:42 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jsp/jstl/core is already defined
六月 04, 2014 4:12:42 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/fmt_rt is already defined
六月 04, 2014 4:12:42 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/fmt is already defined
六月 04, 2014 4:12:42 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jsp/jstl/fmt is already defined
六月 04, 2014 4:12:42 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jsp/jstl/functions is already defined
六月 04, 2014 4:12:42 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/permittedTaglibs is already defined
六月 04, 2014 4:12:42 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/scriptfree is already defined
六月 04, 2014 4:12:42 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/sql_rt is already defined
六月 04, 2014 4:12:42 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/sql is already defined
六月 04, 2014 4:12:43 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jsp/jstl/sql is already defined
六月 04, 2014 4:12:43 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/xml_rt is already defined
六月 04, 2014 4:12:43 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/xml is already defined
六月 04, 2014 4:12:43 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jsp/jstl/xml is already defined
六月 04, 2014 4:12:43 下午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
六月 04, 2014 4:12:44 下午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
[ERROR][2014-06-04 16:12:50,185][org.springframework.web.context.ContextLoader]Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseDaoImpl' defined in file [D:\Program Files\apache-tomcat-7.0.53\webapps\Ins\WEB-INF\classes\com\ins\dao\impl\BaseDaoImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ins.dao.impl.BaseDaoImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1229)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1875)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ins.dao.impl.BaseDaoImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:76)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
... 26 more
Caused by: java.lang.NullPointerException
at com.ins.dao.impl.BaseDaoImpl.<init>(BaseDaoImpl.java:63)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 28 more
六月 04, 2014 4:12:50 下午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseDaoImpl' defined in file [D:\Program Files\apache-tomcat-7.0.53\webapps\Ins\WEB-INF\classes\com\ins\dao\impl\BaseDaoImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ins.dao.impl.BaseDaoImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1229)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1875)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ins.dao.impl.BaseDaoImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:76)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
... 26 more
Caused by: java.lang.NullPointerException
at com.ins.dao.impl.BaseDaoImpl.<init>(BaseDaoImpl.java:63)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 28 more
六月 04, 2014 4:12:51 下午 com.sun.faces.config.ConfigureListener contextInitialized
信息: 初始化上下文 '/Ins' 的 Mojarra 2.0.3 (FCS b03)
六月 04, 2014 4:12:55 下午 com.sun.faces.spi.InjectionProviderFactory createInstance
信息: JSF1048:有 PostConstruct/PreDestroy 注释。标有这些注释的 ManagedBeans 方法将表示注释已处理。
六月 04, 2014 4:12:59 下午 org.apache.catalina.core.StandardContext startInternal
严重: Error listenerStart
六月 04, 2014 4:12:59 下午 org.apache.catalina.core.StandardContext startInternal
严重: Context [/Ins] startup failed due to previous errors
六月 04, 2014 4:12:59 下午 org.apache.catalina.core.ApplicationContext log
信息: Closing Spring root WebApplicationContext
六月 04, 2014 4:12:59 下午 org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
严重: The web application [/Ins] created a ThreadLocal with key of type [com.sun.faces.util.Util$1] (value [com.sun.faces.util.Util$1@db53f4]) and a value of type [java.util.HashMap] (value [{com.sun.faces.patternCache={ = }}]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
六月 04, 2014 4:12:59 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.53\webapps\manager
六月 04, 2014 4:13:00 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.53\webapps\ROOT
六月 04, 2014 4:13:00 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
六月 04, 2014 4:13:00 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8009"]
六月 04, 2014 4:13:00 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 60798 ms

2个答案 按时间排序 按投票排序

0 0

把你的spring配置文件贴一下,你说的不能实例化,是报空指针错误吗?估计你的dao没有被spring注入吧

2014年6月04日 13:10
0 0

什么是不能实例化

你先看下 你的sessionFactory 是否被spring管理

2014年6月03日 18:11

相关推荐

    Spring定时器的实例.doc

    9. **对象实例化**: `Performance`、`Afficheinfo`和`PerformanceList`等对象的创建和初始化,表明类中可能涉及到多个实体类的实例,用于存储和处理业务数据。 10. **时间工具类**: `TimeKit1`可能是一个自定义...

    ORMLite jar、文档、源码、以及博客实例

    1. **数据库初始化**:首先,需要创建一个DatabaseHelper实例,初始化数据库连接和配置。 2. **实体类定义**:定义User类,使用`@DatabaseTable`注解标识,并使用`@DatabaseField`注解定义字段及其对应数据库列。 3....

    android持久化 ormlite-android-4.31.jar

    - **数据库操作**:通过数据库帮助器实例化DAO,然后调用DAO的方法进行数据的增删查改。 3. **ORMLite的特性与优势**: - **简单易用**:ORMLite通过注解和简单的API,降低了数据库操作的复杂性。 - **灵活的...

    spring的jdbctemplate的crud的基类dao

    接着,`BaseDaoImpl` 类实现了 `BaseDaoInf` 接口,并注入了名为 `jdbcTemplate` 的`JdbcTemplate`实例。这是通过`@Resource`注解实现的,它告诉Spring容器将名称为`jdbcTemplate`的bean注入到这个字段中。 在`...

    Spring JdbcTemplate

    它提供了一种模板化的方式来执行SQL语句,使得开发人员可以避免编写大量的重复代码,专注于业务逻辑,而不是底层的数据库交互细节。 **1. Spring JdbcTemplate接口:** Spring JdbcTemplate接口提供了丰富的API,...

    Hibernate框架笔记

    5. **添加BaseDao与BaseDaoImpl**:设计通用的数据访问接口(BaseDao)及其实现类(BaseDaoImpl),封装常用的数据库操作方法。 6. **添加单元测试**:编写单元测试代码来验证数据库连接是否正确,以及基本的CRUD...

    ORMLite With Android Guide

    - **无参构造函数**:确保每个实体类都有一个无参构造函数,这是ORMLite进行实例化时所必需的。 ##### 2.3 配置DAO **DAO(Data Access Object)**是ORMLite提供的用于执行数据库操作的核心组件。配置DAO主要包括...

    ssh整合总结资料(struts,spring,hibernate)

    - 实现时需要关注`BaseDaoImpl`类如何与Spring框架结合,例如通过注解或XML配置的方式将`BaseDaoImpl`与Spring容器关联起来。 - 在Spring的配置文件中,需要为`SessionFactory`和`HibernateTemplate`等组件配置相应...

    ormlite操作android数据库

    - 初始化数据库:在应用启动时,通过`DatabaseHelper`创建或打开数据库,然后通过`getDao()`获取DAO实例。 - 数据操作:使用DAO实例进行数据的插入、查询、更新和删除。 3. **标签“sqlcipher”** SQLCipher是...

    精简封装Basedao.pdf

    在Java的DAO(Data Access Object)模式中,这样的接口通常用于抽象数据访问层,使其与业务逻辑和持久化存储分离,提高代码的可复用性和可维护性。 `BaseDao`接口中定义了以下几个关键方法: 1. `getConnection()`...

    数据库框架ormlite和单元测试框架junit的使用

    4. **测试运行器(Test Runner)**:JUnit提供多种运行器,如`@RunWith(JUnit4.class)`,它允许使用JUnit 4的特性,如参数化测试。 5. **断言(Assertions)**:JUnit提供了丰富的断言方法,如`assertTrue()`, `...

Global site tag (gtag.js) - Google Analytics