Spring 초 입 설정 ~ 13

우선 우리 가 앞에서 소개 한 springMVC 는 이미 매우 강력 한 내용 이지 만, 우 리 는 다른 방안 을 말 하고 싶다.제 내용 은 SpringMVC 에서 얻 은 것 이지 만 기록 하고 싶 습 니 다.
1SpringMVC 설정 의 대체 방안 (이 방안 은 servlet 3.0 이상 버 전)
 1.     DispatcherServlet  
                customizeRegistration()    ,     AbstractAnnotationConfigDispatcherServletInitializer dispachServlet            customizeRegistration()  。                  。

 2.     Servlet filter
       java    initializer   ,                     。        ,              ,            Spring WebApplicationInitializer  。
    
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;

public class TestServlet implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {

        /**  Servlet*/
        Dynamic testServlet=servletContext.addServlet("myServlet",(Class extends Servlet>) MyTest.class);
        testServlet.addMapping("/custom/***");  //  Servlet
    }

}


위의 코드 는 간단 합 니 다. servlet 를 등록 하고 하나의 경로 에 비 추 는 것 입 니 다. 물론 디 스 패 치 Servlet 에 도 비 출 수 있 습 니 다.이와 유사 한 우 리 는 테크네튬 방식 으로 Listener 와 filter 의 방식 으로 등록 할 수 있다.
import javax.servlet.Filter;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.web.WebApplicationInitializer;
public void onStartup(ServletContext servletContext) throws ServletException {
        Dynamic  filter=servletContext.addFilter("myFilter", (Class extends Filter>) MyTest.class);
        
        /**       null     dispatcher                   false                 */
        filter.addMappingForUrlPatterns(null,false, "/custom/**");
        
    }

이 방식 은 Servlet 용기 에 적합 합 니 다. 프로젝트 에 속 하 는 항목 이 이 용기 보다 낮 으 면 웹. xm 에서 설정 해 야 합 니 다.

        
            contextConfigLocation 
            /WEB-INF/spring/root-context.xml 
        
        
        
        
            
                org.springframework.web.context.ContextLoaderLister
            
        
        
        
        
            appServlet
            
                org.springframwork.web.servlet.DispatcherServlet
            
            
            1
            
        
        
        
                appServlet
                /
        

저 희 는 xml 에서 설정 한 후에 설정 파일 에 따라 위아래 질문 정 보 를 읽 습 니 다.

  

  
  

      
  
    
    
  

     
  

  

이것 은 설정 파일 로 불 러 오고 설정 파일 로 불 러 올 수 있 습 니 다. 주석 @ Configuration 주석 클래스 에서 설정 을 불 러 옵 니 다. 설정 파일 웹. xml 을 다시 고 쳐 야 할 수도 있 습 니 다.

        
              contextClass
             org.springframework.web.context.support.AnnotationConfigWebapplicationContext
        
        
        
            contextConfigLocation
            com.textSpring.Spilter    
        
        
        
            
                org.springframework.web.context.ContextLoaderLister
            
        
        
        
            appServlet
            
                org.springframwork.web.servlet.DispatcherServlet
            
            
            
                  contextClass
                 org.springframework.web.context.support.AnnotationConfigWebapplicationContext
            
            
            
            
                  contextConfigLocation
                 com.textSpring.Spilter
            
            
            
            1
            
        

        
                appServlet
                /
                    
        

여기 서 우리 가 설정 한 웹. xml 과 처음에 설정 한 웹. xml 은 바로 여기 서 시작 하 는 자바 류 를 직접 설정 하여 dispatcher Servlet 를 시작 하 는 것 입 니 다. 우 리 는 여기 서 init 를 사용 해 야 합 니 다.param 탭 은 위 에 설 정 된 context - param 의 파일 을 자바 클래스 로 설정 합 니 다.다른 것 은 기본적으로 같다.

좋은 웹페이지 즐겨찾기