새소식

반응형
Java/Spring

Spring - Dynamic Web Project 설정

  • -
반응형

1. Dynamic Web Project 생성

* Project 생성시 꼭 web.xml 체크

2. WebContent > WEB-INF > lib .jar 추가

* 필수요소 :

spring-aop-4.3.12.RELEASE.jar

spring-context-4.3.12.RELEASE.jar

spring-core-4.3.12.RELEASE.jar

spring-expression-4.3.12.RELEASE.jar

spring-web-4.3.12.RELEASE.jar

spring-webmvc-4.3.12.RELEASE.jar

spring-beans-4.3.12.RELEASE.jar

commons-logging-1.2.jar

commons-dbcp-1.4.jar

commons-pool-1.6.jar

mybatis-3.4.6.jar

mybaits-spring-1.3.2.jar

mysql-connector-java3.1.14.jar

jstl-1.2.jar

3. web.xml 수정

- 최초 web 루트 경로로 접근했을때 보여질 화면을 설정할수 있다.

- webProject/ 경로로 접근 시 index.jsp 파일이 보여진다. 보통 로그인화면을 많이 사용함

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

* ContextLoaderListener 생성

: 시작과 종료를 담당하는 이벤트 핸들러이다.

: 이벤트를 발생하면 감지하여 web.xml 파일을 확인하고 application context 를 생성한다

application context - root WebApplicationContext, servlet WebApplicationContext

여기서 root WebApplicationContext 가 ContextLoaderListener에 의해 생성된다.

: 루트 컨텍스트 안에는 repository, service 등등 공통적으로 사용할수 있는 스프링 빈들이 등록되어있다.

자식 컨텍스트는 루트 컨텍스트를 참조할수 있지만 루트컨텍스트는 자식컨텍스트를 참조할수 없다.

자식 컨텍스트 > 루트 컨텍스트 참조가능 | 루트컨테스트 > 자식컨텍스트 참조 불가능

* application-context 생성

 

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/application-context.xml</param-value>
</context-param>

이러한 contextConfigLocation에 설정된 설정파일은 ​스프링과 관련된 설정파일이고 jsp(view)와 관련이 없는 객체를

설정해주는 파일입니다. 즉, view 설정과 관련이 없고 비즈니스 로직과 관련이 있는 파일들을 설정하면 된다.

예) DB 관련 프레임워크인 myBatis 등

 

* DispatcherServlet 생성

DispatcherServlet은 view(JSP)와 관련된 빈을 설정하는 파일이다.  

Controller, ViewResolver, HandlerMapping bean을 관리한다.

<servlet>
    <servlet-name>Dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/dispatcher-servlet.xml</param-value>
    </init-param>
</servlet>

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

 

: DispatcherServlet 에 의해 자식 컨텍스트인 Servlet WebApplicationContext 를 생성한다.

이러한 자식 컨텍스트가 루트컨텍스트를 참조한다.

자식 컨텍스트인 DispatcherServlet은 Controller, ViewResolver, HandlerMapping bean을 관리한다.

- contextConfigLocation 은 Defalut로 되어있는 파일경로를 설정할수 있다.

 

 

* Full source

<?xml version="1.0" encoding="UTF-8"?>

 

<display-name>WebTest</display-name>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<context-param>

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

<param-value>/WEB-INF/config/application-context.xml</param-value>

</context-param>

<servlet>

<servlet-name>Dispatcher</servlet-name>

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

<init-param>

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

<param-value>/WEB-INF/config/dispatcher-servlet.xml</param-value>

</init-param>

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

</servlet>

<servlet-mapping>

<servlet-name>Dispatcher</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

4. WebContent/WEB-INF/config 에 설정파일 생성

- dispatcher-servlet.xml

 

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

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-4.3.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<mvc:annotation-driven/>

<context:component-scan base-package="com.secure.one.*"/>

<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">

<property name="order" value="1"></property>

</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/views/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

</beans>

- xmlns 의미

* xml 파일에서는 사용자가 직접 태그 요소, xml 요소의 이름을 직접 정의할수 있다.

ex 1)

<body>

<arm>70</arm>

<leg>110</leg>

</body>

ex 2)

<body>

<h1>html에서의 제목</h1>

<p>html에서의 단락</p>

</body>

위와 같이 ex 1은 사용자가 직접 body라는 요소의 이름으로 태그를 만들었으며, ex 2 는 html 태그를 사용했다.

이러한 이유로 xml 파일은 이러한 같은 이름의 요소들이 다른의미지만 충돌이 발생할 수있다.

그래서 xmlns 접두사를 사용하여 같은 이름의 요소라도 다른의미로 사용할수 있게 된다.

<요소이름 xmlns:prefix="URI">

URI 는 네임스페이스 식별자로 사용됩니다.

ex )

xmlns:mvc="http://www.springframework.org/schema/mvc"

<mvc:annotation-driven/>

* @EnableWebMvc 어노테이션과 같다. Spring Mvc를 구성할때 필요한 Bean 설정들을 자동으로 해준다.

* <context:component-scan base-package="com.secure.one.*"/>

: 해당 base-package 하위에 있는 class 들을 스캔하면서 bean으로 등록한다.

@controller, @service, @component, @configuration 등등? 컴포넌트 어노테이션들을 확인하여 bean 등록

* <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">

: HandlerMapping 은 dispatcherServlet 이 클라이언트의 모든 요청을 받아서 HandlerMapping에게 넘겨주면 HandlerMapping은 클라이언트의 요청에 맞는 controller를 찾아 요청 정보를 넘겨준다.

* <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

: default view resolver = InternalResourceViewResolver

: HandlerMapping에 의해 알맞는 컨트롤러로 요청정보가 가고, 해당 컨트롤러는 요청정보에 맞는 데이터를 다시 dispatcherservlet에게 넘겨주면 viewResolver에게 요청이 간다.

viewResolver 의 역할 : 알맞는 view를 찾아 요청 반환

* 참조

https://m.blog.naver.com/PostView.nhn?blogId=anytimedebug&logNo=221292305200&proxyReferer=https:%2F%2Fwww.google.com%2F

[Spring] Dynamic Web Project를 이용한 Spring 4 MVC Tutorial

Maven을 사용하지 않고 Dynamic Web Project를 생성하여 Spring MVC를 구현해보겠습니다. Maven을...

m.blog.naver.com

- application-context.xml

 

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

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-4.3.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<value>/WEB-INF/config/database.properties</value>

</property>

</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="${jdbc.driver}" />

<property name="url" value="${jdbc.url}" />

<property name="username" value="${jdbc.username}" />

<property name="password" value="${jdbc.password}" />

</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource"></property>

<property name="mapperLocations" value="classpath:com/secure/one/mapper/**/*.xml"></property>

</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

<constructor-arg index="0" ref="sqlSessionFactory" />

</bean>

</beans>

* <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

: propertyPlaceholderConfigurer 는 외부 설정파일을 해당 스프링설정파일에서 사용하기 위해 사용

* DataSource 설정

: DB와 관련된 Connection 정보를 가지고 있다.

: DB와의 연결을 위해 사용

: 이때 Commons-dbcp 를 사용한다.(DBCP)

: DataSource 종류

1. BasicDataSource (사용)

2. PoolingDataSource

3. SingleConnectionDataSource

4. DriverManagerDataSource

* DataBase Connection pool

: 매번 DB와의 연결을 위해 Connection 객체를 생성하기 어렵고 불편하기 때문에 미리 Connetcion 객체를 만들어서 pool에 저장한 후 필요할때마다 사용하고 반환한다.

* SqlSessionFactoryBean

: SqlSessionFactory 구성요소를 생성하기 위해서는 Mabatis 구성 설정파일이 필요하지만 SqlSessionFactoryBean을 사용하면 설정파일 없이 SqlSessionFactory를 생성할수 있다.

* SqlSessionFactory

: SqlSession을 생성하기 위한 구성요소

반응형

'Java > Spring' 카테고리의 다른 글

JWT(Json Web Token)  (0) 2021.08.25
@SelectKey, <selectKey>  (0) 2021.08.25
JVM, JRE, JDK 차이  (0) 2021.08.25
C언어와 Java 언어의 차이점  (0) 2021.08.25
DAO와 DTO를 분리하는 이유  (0) 2021.08.25
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.