org.springframework.beans.factory.UnsatisfiedDependencyException

时间:2016-04-18 05:12:45

标签: java spring hibernate

当我使用Hibernate Spring Spring MVC开发一个程序时,我遇到了一个错误。

here is the details.
    四月 18, 2016 12:20:27 下午     org.springframework.web.context.support.XmlWebApplicationContext refresh
    警告: Exception encountered during context initialization - cancelling refresh attempt:     org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl' defined in file [C:\Users\Alfred\workspace_1\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Management\WEB-INF\classes\com\alfred\ServiceImpl\UserServiceImpl.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.alfred.DAO.UserDAO]: Cannot find class [org.springframework.orm.hibernate3.AnnotationSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/Management-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.AnnotationSessionFactoryBean; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate3.AnnotationSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/Management-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.AnnotationSessionFactoryBean
    四月 18, 2016 12:20:27 下午 org.springframework.web.servlet.DispatcherServlet initServletBean
    严重: Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl' defined in file [C:\Users\Alfred\workspace_1\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Management\WEB-INF\classes\com\alfred\ServiceImpl\UserServiceImpl.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.alfred.DAO.UserDAO]: Cannot find class [org.springframework.orm.hibernate3.AnnotationSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/Management-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.AnnotationSessionFactoryBean; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate3.AnnotationSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/Management-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.AnnotationSessionFactoryBean
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)

这是我的代码。 UserServiceImpl.java

 package com.alfred.ServiceImpl;

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

    import com.alfred.DAO.UserDAO;
    import com.alfred.Service.UserService;

    @Service
    public class UserServiceImpl implements UserService{

        private UserDAO userDao;

        @Autowired
        public UserServiceImpl(UserDAO userDao) {
            this.userDao = userDao;
        }
    }

这里是UserDAO.java文件

package com.alfred.DAO;
import java.util.ArrayList;
import com.alfred.Beans.User;

/**
 * @author Alfred
 *
 */
public interface UserDAO {

public User findUserById(String id);
public ArrayList<User> findUser(int id_start,int id_end);
public void deleteUser(String id);
public void addUser(User u);
public void updateUser(User u);
}

这是UserDAOImple.java文件

package com.alfred.DAOImpl;
import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Repository;
import com.alfred.Beans.User;
import com.alfred.DAO.UserDAO;

@Repository
public class UserDAOImpl implements UserDAO{

private Session session;
private Transaction tx;

public Session getSession() {
    return session;
}
//设置session
public void setSession(Session session) {
    this.session = session;
}

public Transaction getTx() {
    return tx;
}

public void setTx(Transaction tx) {
    this.tx = tx;
}

public UserDAOImpl(){}

private static SessionFactory getSessionFactory(){  
    Configuration config = new Configuration().configure();
    StandardServiceRegistryBuilder builder = config.getStandardServiceRegistryBuilder().applySetting(null, config.getProperties());
    SessionFactory sessionFactory = config.buildSessionFactory(builder.build());
    return sessionFactory;  
}

public Session openSession(){
    session = getSessionFactory().openSession();
    return session;
}

public Session openSessionWithTransaction(){
    session = getSessionFactory().openSession();
    tx = session.beginTransaction();
    return session;
}

public void closeSession(){
    session.close();
}

public void closeSessionWithTransaction(){
    tx.commit();
    session.close();
}
//以下为业务流程
@Override
public User findUserById(String id) {
    User user = getSession().get(User.class, id);
    return user;
}

@Override
public void deleteUser(String id) {
    // TODO Auto-generated method stub

}

@Override
public void addUser(User u) {
    // TODO Auto-generated method stub

}

@Override
public void updateUser(User u) {
    // TODO Auto-generated method stub

}

//找出所有用户
@SuppressWarnings("unchecked")
@Override
public ArrayList<User> findUser(int id_start,int id_end) {
    String hql = "from usr where u_id > :id_start && u_id < :id_end";
    Query query = getSession().createQuery(hql);
    query.setParameter("id_start", id_start);
    query.setParameter("id_end", id_end);
    return (ArrayList<User>)query.list();
}
}
你能给我一些建议吗?

0 个答案:

没有答案