2016-11-16 1 views
0

최대 절전 모드로 간단한 봄 휴식 프로젝트를 만들려고합니다.봄철 나머지와 함께 최대 절전 모드로 만들기

DAO 및 서비스 계층을 만들었습니다. 또한 dispatcher-servlet에 bean을 구성하고 서비스 및 서비스 DAO 세션 팩토리 및 트랜잭션과 함께 컨트롤러 클래스에서 autowired를 수행했습니다.

하지만 계속 오류가 발생합니다.

나는 또한 오류 사진을 업로드했습니다.

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

    xsi:schemaLocation="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 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 
<!-- <mvc:annotation-driven /> --> 

<!-- <resources mapping="/resources/**" location="/resources/" /> --> 

<!--JDBC ko properties--> 
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close"> 
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <beans:property name="url" value="jdbc:mysql://localhost:3306/student_database" /> 
    <beans:property name="username" value="root" /> 
    <beans:property name="password" value="" /> 
</beans:bean> 

<!-- Hibernate ko SessionFactory Bean definition part --> 
<beans:bean id="hibernate5AnnotatedSessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <beans:property name="dataSource" ref="dataSource" /> 
<!--  <beans:property name="annotatedClasses"> 
     <beans:list> 
      <beans:value>com.mycompany.SpringRestMaven.bean</beans:value> 
     </beans:list> 
    </beans:property> --> 
    <beans:property name="hibernateProperties"> 
     <beans:props> 
      <beans:prop  key="hibernate.dialect">org.hibernate.dialect.MySQLDialect 
      </beans:prop> 
      <beans:prop key="hibernate.show_sql">true</beans:prop> 
     </beans:props> 
    </beans:property> 
</beans:bean> 

<context:component-scan base- package="com.mycompany.SpringRestMaven.controller"/> 
<tx:annotation-driven transaction-manager="transactionManager"/> 

<beans:bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" /> 
</beans:bean> 

controller class: 
package com.mycompany.SpringRestMaven.controller; 
/* I have not included imports*/ 
@RestController 
public class StudentController { 

@Autowired 
ServiceLayer studentService; 

@RequestMapping(value = "/insert", method = RequestMethod.POST, headers = "Accept=application/json") 
public void insert(@RequestBody Student student) { 
    studentService.insert(student); 
} 

@RequestMapping(value = "/update", method = RequestMethod.PUT, headers = "Accept=application/json") 
public void update(@RequestBody Student student) { 
    studentService.update(student); 
} 

@RequestMapping(value = "/delete", method = RequestMethod.DELETE, headers = "Accept=application/json") 
public void delete(@PathVariable("id") int id) { 
    studentService.delete(id); 
} 

@RequestMapping(value = "/getAll", method = RequestMethod.GET, headers = "Accept=application/json") 
public List<Student> getAll() { 

    List<Student> listOfStudent = studentService.getAll(); 
    return listOfStudent; 
} 

@RequestMapping(value = "/searchById/{id}", method = RequestMethod.GET, headers = "Accept=application/json") 
public Student searchById(@PathVariable int id) { 
    return studentService.searchById(id); 
} 
} 

This is my service layer implementation: 

package com.mycompany.SpringRestMaven.Service; 

@Service("studentService") 
public class ServiceImpl implements ServiceLayer { 

@Autowired StudentDAO studentDao; 

@Transactional 
@Override 
public void insert(Student s) { 
    studentDao.insert(s); 
} 

@Transactional 
@Override 
public void update(Student s) { 
    studentDao.update(s); 
} 

@Transactional 
@Override 
public void delete(int id) { 
    studentDao.delete(id); 
} 

@Transactional 
@Override 
public List<Student> getAll() { 
    return studentDao.getAll(); 
} 

@Transactional 
@Override 
public Student searchById(int id) { 
    return studentDao.searchById(id); 
} 

} 

This is my DAO implementation: 

package com.mycompany.SpringRestMaven.DAO; 

@Repository 
public class StudentDAOImpl implements StudentDAO { 

@Autowired 
private SessionFactory sessionFactory; 
private Session session; 
private Transaction trans; 

@Override 
public void insert(Student s) { 
    session = sessionFactory.openSession(); 
    trans = session.beginTransaction(); 
    session.save(s); 
    trans.commit(); 
    session.close(); 
} 

@Override 
public void update(Student s) { 
    session = sessionFactory.openSession(); 
    trans = session.beginTransaction(); 
    session.saveOrUpdate(s); 
    trans.commit(); 
    session.close(); 
} 

@Override 
public void delete(int id) { 
    session = sessionFactory.openSession(); 
    trans = session.beginTransaction(); 
    Student sObject = (Student) session.get(Student.class, id); 
    session.delete(sObject); 
    trans.commit(); 
    session.close(); 
} 

@Override 
public List<Student> getAll() { 
    return session.createQuery("SELECT s FROM Student s").list(); 
} 

@Override 
public Student searchById(int id) { 
    return (Student) session.get(Student.class, id); 
} 


} 

나는이 오류가 무엇입니까 : 구성

screenshot

+0

오류를 볼 수 없습니다! –

+0

오류로 봄이 ServiceLayer 유형의 빈을 찾지 못했다고합니다. – smsnheck

+0

구성 요소 검사 기준을 com.mycompany.SpringRestMaven으로 변경하십시오. 현재 설정으로 컨트롤러 패키지에없는 빈을 찾을 수 없습니다. – smsnheck

답변

0

이 변경

<context:component-scan base- package="com.mycompany.SpringRestMaven.controller"/> 

으로

<context:component-scan base- package="com.mycompany.SpringRestMaven"/> 

그러면 Spring은 모든 클래스를 SpringRestMaven 패키지 아래에서 스캔하고 주석이 달린 클래스에서 bean을 작성합니다.

현재 구성에서 Spring은 .controller 패키지 아래에서 Bean 만 스캔하고 만듭니다. ServiceLayer가이 패키지에 없으므로 Spring은 클래스를 찾을 수 없으며 bean을 생성하지 않고 필드를 autowire 할 수 없습니다.

+0

감사합니다. 선생님 께 감사 드렸습니다. 사실 나는 이전에이 작업을 수행 했었지만 다음과 같은 오류가 발생했습니다. – prithivi

+0

심각한 문제 : 앱을로드하는 중 예외가 발생했습니다. .ConflictingBeanDefinitionException : bean 클래스 [com.mycompany.SpringRestMaven.WebController에 대한 주석 지정 bean 이름 'webController' ]은 같은 이름과 클래스의 기존의 호환되지 않는 bean 정의와 충돌합니다 [com.mycompany.SpringRestMaven.controller.WebController] – prithivi

+0

다른 패키지에있는'WebController'라는 이름의 두 클래스가있는 것 같군요? – smsnheck

관련 문제