티스토리 뷰

Spring

Spring(1)_설정파일

clumsy0g 2019. 12. 12. 15:29

web.xml - 서블릿( 필터 포함 ) 을 등록 (정의 및 어떤 요청에 대해 작동시킬 것인지 url mapping )

: 여기서 DispatcherServlet 은 스프링 컨테이너를 생성하며 MVC에서 Front Controller 역할을 수행한다

: 주로 Encoding Filter 와 Dispatcher Servlet을 등록함

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

servlet-context.xml

: 스프링 컨테이너가 매번 클라이언트의 요청을 받을 때 어떻게 처리할건지 "전체적인 구조" 를 설정한 파일

: DispatcherServlet Context: defines this servlet's request-processing infrastructure

  • js, css 파일을 어디서 읽을 것인지
  • view를 읽을때 어떻게 읽을것인지 (prefix, suffix 설정)
  • DispatcherServlet이 요청에 맞는 컨트롤러를 찾고 , 컨트롤러가 보낸 응답을 알맞게 변형해주는 객체들 (HandlerMapping, HandlerAdapter)을 등록한다 (annoation-drriven)
  • @annotation이 명시된 파일들을 어디서 읽을 것인지 base-package 설정 등
<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context https://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:component-scan base-package="com.supermap.demo" />
    <context:component-scan base-package="com.supermap.dao" />


</beans:beans>

root-context.xml

: Root Context: defines shared resources visible to all other web components

: 주로 DB연동과 관련한 bean들을 등록한다 (JDBC : DataSource / MyBatis : SqlSessionFactoryBean)

: 클라이언트의 요청과는 관련이 없는, Back-end 에서만 필요한 설정들을 한다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

    <!-- properties -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:/jdbc.properties" />
        <property name="fileEncoding" value="UTF-8" />
    </bean>

    <!-- JDBC-PostgreSQL -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>


    <!-- MyBatis-Spring -->
    <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>

        <!-- MultipartResolver 설정 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1073741824" />
        <!-- <property name="maxInMemorySize" value="1073741824" /> -->
    </bean>

</beans>

'Spring' 카테고리의 다른 글

[기타 팁] Ajax 요청시 data에 String을 쓰면 안된다?  (0) 2019.12.18
Spring(5)_MVC(2)_Ajax&JSON  (0) 2019.12.12
Spring(4)_Interceptor  (0) 2019.12.12
Spring(3)_DB연결(2)&MVC  (0) 2019.12.12
Spring(2)_DB연결&Security  (0) 2019.12.12
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함