1

我是Spring MVC和hibernate的新手,我一直处于低迷状态,所以希望你们能提供帮助。每次我运行时,我都会在创建 bean 时遇到异常错误。我似乎无法在互联网上找到正确的答案,我似乎无法找到我在这里缺少的东西。我希望你们能帮助我。我的 Context.xml

           <?xml version="1.0" encoding="UTF-8"?>
        <beans:beans xmlns="http://www.springframework.org/schema/mvc"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:beans="http://www.springframework.org/schema/beans"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

            <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

            <!-- Enables the Spring MVC @Controller programming model -->
            <annotation-driven />

            <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
            <resources mapping="/resources/**" location="/resources/" />

            <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
            <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <beans:property name="prefix" value="/WEB-INF/views/" />
                <beans:property name="suffix" value=".jsp" />
            </beans:bean>


                <context:annotation-config/>
                <context:component-scan base-package="com.test.cedric." />
                <beans:bean id="departmentController" class="com.test.cedric.controller.DepartmentController" />
                 <beans:bean id="departmentDaoImpl" class="com.test.cedric.dao.DepartmentDaoImpl">

            </beans:bean>
                 <beans:bean id="departmentServiceImpl" class="com.test.cedric.service.DepartmentServiceImpl">

            </beans:bean>

        </beans:beans>

        <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

    My hibernate.cfg.xml
        <hibernate-configuration>
            <session-factory>
                <!-- Database connection settings -->
                <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
                <property name="connection.url">jdbc:mysql://localhost:3306/test1</property>
                <property name="connection.username">root</property>
                <property name="connection.password"></property>

                <!-- JDBC connection pool (use the built-in) -->
                <property name="connection.pool_size">1</property>

                <!-- SQL dialect -->
                <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

                <!-- Enable Hibernate's automatic session context management -->
                <property name="current_session_context_class">thread</property>

                <!-- Disable the second-level cache  -->
                <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

                <!-- Echo all executed SQL to stdout -->
                <property name="show_sql">false</property>

                <property name="hbm2ddl.auto">validate</property>

                <mapping resource="com.test.cedric.model.Department"/>


            </session-factory>
        </hibernate-configuration>

我的服务代码

         package com.test.cedric.service;

        import java.util.List;

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

        import com.test.cedric.dao.DepartmentDao;
        import com.test.cedric.dao.DepartmentDaoImpl;
        import com.test.cedric.model.Department;

        @Service

        public class DepartmentServiceImpl implements DepartmentService{

            @Autowired
            DepartmentDao dDao;
            @Transactional 
            @Override
            public void addDept(Department department) {
                dDao.addDept(department);
                // TODO Auto-generated method stub

            }

            @Override
            public void delDept(Department department) {
                dDao.delDept(department);
                // TODO Auto-generated method stub

            }

            @Override
            public void editDept(Department department) {
                dDao.editDept(department);
                // TODO Auto-generated method stub

            }

            @Override
            public List<Department> getAllDept() {
                // TODO Auto-generated method stub
                return dDao.getAll();
            }

            @Override
            public Department getDeptById(int dept_id) {
                // TODO Auto-generated method stub
                return null;
            }


        }

我的 DAO

            package com.test.cedric.dao;

        import java.util.List;

        import org.hibernate.SessionFactory;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Repository;

        import com.test.cedric.model.Department;

        @Repository
        public class DepartmentDaoImpl implements DepartmentDao {

            @Autowired
            SessionFactory sessionFactory;
            @Override
            public void addDept(Department department) {
                sessionFactory.getCurrentSession().saveOrUpdate(department);

            }

            @Override
            public void delDept(Department department) {
                sessionFactory.getCurrentSession().delete(department);

            }

            @Override
            public void editDept(Department department) {
                sessionFactory.getCurrentSession().saveOrUpdate(department);
                // TODO Auto-generated method stub

            }

            @SuppressWarnings("unchecked")
            @Override
            public List<Department> getAll() {
                sessionFactory.getCurrentSession().createQuery("From Department");
                // TODO Auto-generated method stub
                return sessionFactory.getCurrentSession().createQuery("From Department").list();
            }

            @Override
            public Department getDeptById(int dept_id) {
                // TODO Auto-generated method stub
                return null;
            }

        }
4

2 回答 2

1

首先,你有以下

<context:component-scan base-package="com.test.cedric." />
<beans:bean id="departmentController" class="com.test.cedric.controller.DepartmentController" />
<beans:bean id="departmentDaoImpl" class="com.test.cedric.dao.DepartmentDaoImpl">
</beans:bean>
<beans:bean id="departmentServiceImpl" class="com.test.cedric.service.DepartmentServiceImpl">
</beans:bean>

component-scan扫描您声明的包,如果它找到注释, @Component, @Service, @Repository,@Controller或者@Configuration将为您创建一个 bean 实例。因此,上面您自己的 bean 声明是多余的。摆脱他们。

其次,除非你没有展示它,否则你实际上没有一个SessionFactorybean 可以作为自动装配的候选者。您的休眠配置中的<session-factory>元素与 Spring 上下文 bean 无关。如果你想连接两者,你可以使用

<bean
    id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="configLocation">    
        <value>
            classpath:location_of_config_file/hibernate.cfg.xml
        </value>
    </property>
    <property name="hibernateProperties">
        <props>
            ...    
        </props>    
    </property>

</bean>
于 2013-09-22T15:45:28.713 回答
0

从您提供给我们的内容来看,您似乎缺少 sessionFactory 的 bean 配置。您使用的每个 @Autowire 注释都必须对应于一个 Spring bean。因此,在您的 Context.xml 中,您需要添加如下内容:

<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    ... bean configuration properties ...
</beans:bean>

AnnotationSessionFactoryBean 可能不是您使用的实际 bean —— 有很多方法可以做到这一点,您可以四处搜索以确定您想要做什么。但是您肯定需要为名为“sessionFactory”的 bean 添加一个定义。

于 2013-09-22T15:52:01.937 回答