Spring

spring-servlet.xml 과 applicationContext.xml 대신 Class 이용

IT공부블로그 2019. 3. 20. 16:07
728x90
반응형

  톰캣과 디스패처서블릿의 init이 실행되면 디폴트값으로 컨테이너가 생성된다.

  

  톰캣이 실행될때 읽는 web.xml에  아래와 같이 설정을 해주면 디폴트 RootApplicationContext가 아닌 AnnotationConfigWebApplicationContext가 생성되며


  <context-param> 

    <param-name>contextClass</param-name>

    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>

  </context-param>

  

   <context-param> 

    <param-name>contextConfigLocation</param-name>

    <param-value>com.douzone.guestbook4.config.RootConfig</param-value>

  </context-param>


RootConfig == applicationContext.xml

com.douzone.guestbook4.config.RootConfig를 참조하여 컨테이너 안의 빈을 채운다



  <servlet>

      <servlet-name>spring</servlet-name>

      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

      

        <init-param>

      <param-name>contextClass</param-name>

      <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>

      </init-param>

      

       <init-param>

      <param-name>contextConfigLocation</param-name>

      <param-value>com.douzone.guestbook4.config.WebConfig</param-value>

      </init-param>

      

       <load-on-startup>1</load-on-startup>

   </servlet>


다음과 같이 디스패처 서블릿에 파라미터로 값을주면 init 실행시 디폴트 컨테이너를 생성하는게 아니라 AnnotationConfigWebApplicationContext을 생성


WebConfig == spring-servlet.xml

com.douzone.guestbook4.config.WebConfig을 참조하여 컨테이너 안의 빈들을 채운다




@Configuration

@EnableWebMvc

@ComponentScan("com.douzone.guestbook4.controller")

public class WebConfig {


@Bean

public ViewResolver viewResolver()

{

InternalResourceViewResolver viewResolver = 

new InternalResourceViewResolver();

viewResolver.setPrefix("/WEB-INF/views");

viewResolver.setSuffix(".jsp");

viewResolver.setExposeContextBeansAsAttributes(true);


return viewResolver;

}

}


@EnableWebMvc == <mvc:annotation-driven/>  mvc에서 어노테이션스캔할때 써주는것


viewResolver, MesageConverter 등 여기서 다 설정해주어야한다



@Configuration

@ComponentScan("com.douzone.emaillist.dao")

public class RootConfig {

 

}


<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- java utf-8이라는걸 알려줌 --><project.reporting.outputEncdoing>UTF-8</project.reporting.outputEncdoing>

 

2개가없으면 젠킨스에서 오류남


728x90
반응형