Spring

스프링 4 경험하기

오잎 클로버 2022. 4. 30. 09:10
728x90

기존 스프링을 스프링5, 스프링 부트로 편하게 개발하였는 데 일상 글에서 말했다시피 스프링의 히스토리를 조금이나마 알고, 얕게 경험해보고자 스프링 4를 스프링 부트없이 직접 의존성을 주입해주는 등 스프링 부트에서는 자동으로 되어있는 것들을 직접 해보았다.

스프링 버전은 4.2.4 릴리즈를 사용하였으며, jdk는 1.8, aspectJ는 1.6, slf4j는 1.6.6, apache.titles는 3.0.7 버전으로 사용하였다. 


pom.xml는 다음과 같이 구성하였다. (너무 길기때문에 파일 참고)

pom.xml
0.01MB

그리고 default welcome 페이지는 기본 제공 JSP(index.jsp)를 사용하였다.

애플리케이션 config과 dispatcher를 설정하기 위해 web.xml에 다음과 같이 작성하여 설정하였다.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 

dispatcher-servlet.xml는 기본 제공을 사용하였고, applicationContext.xml는 메타타입을 지정해주고, web 및 resource에 있는 파일들을 사용하기 위해 세팅을 하였습니다.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       ">
    <context:component-scan base-package="me.clover5.samplespring"/>
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

위 세팅에 비하인드가 있다. base-package를 설정해야할 때, 이를 xmlns:context만 설정하면 되는 줄 알고 헤맸다.

xsi.schemaLocation에 설정해야한다는 것은 혹시나 싶어서 추가해보니 되었다....

(머리가 나쁘면 몸이 고생한다더니, 진짜였다.)

 

기본 패키지는 위에 나와있듯이 me.clover5.samplespring이다.

컨트롤러를 사용해서 ResponseBody로 출력하도록 하였다.

 

 

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping(value = "hello", method = RequestMethod.GET)
    public String hello() {
        return "Hello, Spring!";
    }
}

다행히도 4.0부터는 @RestController를 지원하였기에 나름 편하게 컨트롤러를 구현할 수 있었다.

하지만, @GetMapping 등 @RequestMapping의 다양한 매핑을 지원해주는 것은 4.3부터였기에 위와 같은 방식으로 구현하였습니다.


내가 알던 그 스프링이 맞는 가라는 생각이 들었다. 스프링 부트가 없던 시절에 스프링으로 개발하신 개발자 분들에게 경의를 표합니다. 스프링의 악명을 다시끔 느끼는 시간이었다. 

많은 기능을 구현한 것도 아닌, 단순 컨트롤러만 하는 것도 뭔가 어색했으며, 스프링이 아닌 다른 무언가를 하는 느낌이 들었다.

그리고 스프링 4.0버전이 2013년에 만들었다는 것에 조금 경의로웠다. (조금 무섭네요;;)


 

 

이상입니다. (스프링은 역시 대단한 오픈 소스 프레임워크입니다.)

'Spring' 카테고리의 다른 글

[Spring] Spring 3.0.0 AutoConfigure 사용  (0) 2023.03.13
[Spring] JPA 공부 #1  (1) 2022.03.25
[Spring] AOP 예제  (0) 2022.03.14
[Spring] DTO vs VO  (0) 2022.02.28
[Spring] Spring Transaction 세부 설정 설명  (0) 2022.02.21