2013-09-22 2 views
1

저는 봄 MVC에서 동성애자이며, 절전 모드 상태였습니다. 여러분이 도와 줄 수 있기를 바랍니다. 매번 나는 빈 생성시 예외 오류가 발생합니다. 나는 인터넷에서 정확한 대답을 찾지 못하는 것 같아. 내가 여기에없는 것을 발견 할 수 없다. 너희들이 나를 도울 수 있기를. 내 context.xml에자동 종속 종속성의 주입이 실패했습니다. 필드를 autowire 할 수 없습니다.

  <?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; 
      } 

     } 
+1

예외 스택 추적을 게시하십시오. –

답변

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 검사를 수행 한 e 패키지를 찾았고 주석이 @Component, @Service, @Repository, @Controller 또는 @Configuration 인 경우 bean 인스턴스가 생성됩니다. 이와 같이, 위의 빈 선언은 중복됩니다. 그들을 제거.

둘째로, 표시하지 않는 한 실제로 autowiring 할 후보인 SessionFactory bean이 없습니다. 요소가 하이버 네이트 구성은 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> 
0

에서 제공 한 것부터 sessionFactory의 빈 구성이 누락 된 것처럼 보입니다. 당신이 사용하는 각각의 @Autowire 어노테이션은 Spring 빈에 대응해야한다. 그래서 context.xml에, 당신은 뭔가를 추가해야 할 것 :

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

AnnotationSessionFactoryBean 사용하는 실제 빈하지 않을 수 있습니다 -이 그것을 할 수있는 방법의 무리, 그리고 당신이 주변에 검색 할 수 있습니다를 당신이하고 싶은 것을 알아 내려고. 하지만 "sessionFactory"라는 bean에 대한 정의를 추가해야합니다.

관련 문제