스핑 IOC 컨테이너

스핑 IOC 컨테이너

패키지 서블릿; 

수입 org.springframework.context.ApplicationContext;
수입 org.springframework.web.context.support.WebApplicationContextUtils;
수입 service.IStudentService;
수입 javax.servlet.ServletException;
수입 javax.servlet.annotation.WebServlet;
수입 때 java.io.IOException; 

@WebServlet ( "/ QueryStudentByIdServlet" )
 공개  수업 QueryStudentByIdServlet는 확장 인 javax.servlet.http.HttpServlet {
     개인 IStudentService studentService을; 

    공공  무효setStudentService (IStudentService studentService) {
          .studentService = studentService; 
    } 

    @Override 
    공공  무효 의 init ()이 발생 ServletException을 {
         슈퍼 .init를 (); 
        애플리케이션 컨텍스트의 ApplicationContext = WebApplicationContextUtils.getWebApplicationContext ( .getServletContext ()); 
        studentService = (IStudentService) applicationContext.getBean ( "studentServiceId" ); 
    } 

    보호  공극 의 doGet (나오는 javax.servlet.http.HttpServletRequest 요청의 javax.servlet.http.HttpServletResponse 응답)
            발생 javax.servlet.ServletException, IOException가 { 
        문자열 이름 = studentService.queryStudentById (); 
        request.setAttribute ( "이름" , 이름); 
        request.getRequestDispatcher ( "result.jsp" )의 .forward (요청, 응답); 
    } 

    보호  공극 의 doPost는 (나오는 javax.servlet.http.HttpServletRequest 요청은 응답의 javax.servlet.http.HttpServletResponse)
             발생 javax.servlet.ServletException IOException을 { 
        의 doGet (요청, 응답); 
    } 
}

web.xml에

<? XML 버전 = = "UTF-8"인코딩 "1.0" >? 
< 웹 응용 프로그램 의 xmlns = "http://xmlns.jcp.org/xml/ns/javaee" 
         에 xmlns : XSI = "HTTP : //www.w3 .ORG / 2001 / 된 XMLSchema 인스턴스 " 
         XSI :의 schemaLocation ="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd " 
         버전 ="3.1 " > 
    < 컨텍스트 PARAM > 
        < PARAM 이름 > 는 contextConfigLocation </ PARAM 이름 > 
        < PARAM 값 > 클래스 경로 applicationContext.xml, 클래스 경로 applicationContext- *.
    </ 상황 PARAM > 
    < 수신기 > 
        < 청취자 클래스 > org.springframework.web.context.ContextLoaderListener </ 청취자 클래스 > 
    </ 리스너 > 
</ 웹 응용 프로그램 >

applicationContext.xml

<? XML 버젼 = "1.0" "UTF-8"인코딩 = ?> 
< 의 xmlns = "http://www.springframework.org/schema/beans" 
       에 xmlns :이 xsi = "http://www.w3.org/2001 / 된 XMLSchema 인스턴스 " 
       XSI :의 schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP : //www.springframework. 조직 / 스키마 / 컨텍스트 http://www.springframework.org/schema/context/spring-context.xsd " > 
    
</ >

ApplicationContext를-Controller.xml

<? XML 버젼 = "1.0" "UTF-8"인코딩 = ?> 
< 의 xmlns = "http://www.springframework.org/schema/beans" 
       에 xmlns :이 xsi = "http://www.w3.org/2001 / 된 XMLSchema 인스턴스 " 
       는 xsi :의 schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd " > 
    < ID ="studentServlet " 클래스 ="servlet.QueryStudentByIdServlet " > 
        < 속성 이름 ="studentService " REF ="studentServiceId " >   <
    
</ >

ApplicationContext를-Service.xml

<? XML 버젼 = "1.0" "UTF-8"인코딩 = ?> 
< 의 xmlns = "http://www.springframework.org/schema/beans" 
       에 xmlns :이 xsi = "http://www.w3.org/2001 / 된 XMLSchema 인스턴스 " 
       는 xsi :의 schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd " > 
    < ID ="studentServiceId 「클래스 = "service.impl.StudentServiceImpl" > 
        < 속성 = "studentDao" REF = "studentDaoId" > </ 속성 >
    
>

ApplicationContext를-Dao.xml

<? XML 버젼 = "1.0" "UTF-8"인코딩 = ?> 
< 의 xmlns = "http://www.springframework.org/schema/beans" 
       에 xmlns :이 xsi = "http://www.w3.org/2001 / 된 XMLSchema 인스턴스 " 
       는 xsi :의 schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd " > 
    < ID ="studentDaoId " 클래스 ="dao.impl.StudentDaoImpl " > </ > 
</ >

 

추천

출처www.cnblogs.com/kikyoqiang/p/11827261.html