注入自动连接的依赖项失败;无法自动装配字段

时间:2013-09-22 15:35:18

标签: java mysql spring hibernate spring-mvc

我是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;
            }

        }

2 个答案:

答案 0 :(得分: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或{{1将为您创建一个bean实例。因此,上面您自己的bean声明是多余的。摆脱它们。

其次,除非您没有显示它,否则您实际上没有@Configuration bean作为自动装配的候选者。 hibernate配置中的SessionFactory元素与Spring上下文bean无关。如果要连接两者,可以使用

<session-factory>

答案 1 :(得分: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添加一个定义。