Back-End/Spring

Spring 전자정부프레임워크 설정

개발자 DalBy 2024. 5. 14. 15:03
반응형

해당 포스트는 필자 복붙용으로 만든 글이니 여러 설명을 많이 생략 합니다.

참고 부탁드립니다. 감사합니다.

 

 

1. 각 필요에 따라 필요한 의존성 추가한다. Maven

 

 

 

2. Web.xml 설정하기

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
	id="WebApp_ID" version="2.5"> 
	<display-name>egov.sample</display-name> 
        
 	<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>*.yh</url-pattern> 
		<url-pattern>*.act</url-pattern> 
	</filter-mapping> 
     
	<filter> 
		<filter-name>HTMLTagFilter</filter-name> 
		<filter-class>egovframework.rte.ptl.mvc.filter.HTMLTagFilter</filter-class> 
	</filter> 
	<filter-mapping> 
		<filter-name>HTMLTagFilter</filter-name> 
		<url-pattern>*.yh</url-pattern> 
		<url-pattern>*.act</url-pattern> 
	</filter-mapping> 
        
	<context-param> 
		<param-name>contextConfigLocation</param-name> 
		<param-value>classpath*:egovframework/spring/context-*.xml</param-value> 
	</context-param> 
     
	<listener> 
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
	</listener> 
        
	<servlet> 
		<servlet-name>action</servlet-name> 
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
		<init-param> 
			<param-name>contextConfigLocation</param-name> 
			<param-value>/WEB-INF/config/egovframework/springmvc/dispatcher-servlet.xml</param-value> 
		</init-param> 
		<load-on-startup>1</load-on-startup> 
	</servlet> 
        
	<servlet-mapping> 
		<servlet-name>action</servlet-name> 
		<url-pattern>*.yh</url-pattern> 
		<url-pattern>*.act</url-pattern> 
	</servlet-mapping> 
    
	<welcome-file-list> 
		<welcome-file>index.jsp</welcome-file> 
	</welcome-file-list> 
     
	<login-config> 
		<auth-method>BASIC</auth-method> 
	</login-config> 
     
	<error-page> 
		<exception-type>java.lang.Throwable</exception-type> 
		<location>/common/error.jsp</location> 
	</error-page> 
	<error-page> 
		<error-code>404</error-code> 
		<location>/common/error.jsp</location> 
	</error-page> 
	<error-page> 
		<error-code>500</error-code> 
		<location>/common/error.jsp</location> 
	</error-page> 
     
</web-app>

Web.xml에서는 URL mapping과 filter 설정 그리고 기본 메인 화면을 추가 및 수정한다.

 

 

 

3. Dispatcher-servlet 설정하기

<?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:p="http://www.springframework.org/schema/p" 
        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-4.0.xsd 
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 
    <context:component-scan base-package="com.main.mvc"> 
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> 
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> 
    </context:component-scan> 
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
        <property name="webBindingInitializer"> 
            <bean class="egovframework.example.cmmn.web.EgovBindingInitializer"/> 
        </property> 
        <!-- controller return -> auto Map / List JSON --> 
        <property name="messageConverters"> 
	    <list>     
	        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
		    <property name="supportedMediaTypes"> 
    		        <list> 
    		            <value>text/html;charset=UTF-8</value> 
    		            <value>application/json;charset=UTF-8</value> 
    		        </list> 
		    </property> 
		</bean>             
	    </list> 
	</property>   
    </bean>

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> 
        <property name="interceptors"> 
            <list> 
                <ref bean="localeChangeInterceptor" /> 
                <ref bean="authInterceptor" /> 
            </list> 
        </property> 
    </bean> 
    <bean id="authInterceptor" class="com.util.web.Interceptor"/> 
     
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /> 
    <!-- 쿠키를 이용한 Locale 이용시 <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/> --> 
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
        <property name="paramName" value="language" /> 
    </bean> 
    <!-- Json return  --> 
	<!-- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" > 
		 <property name="content" value="text/html;charset=UTF-8"/> 
	</bean> --> 
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
        <property name="defaultErrorView" value="cmmn/egovError"/> 
        <property name="exceptionMappings"> 
            <props> 
                <prop key="org.springframework.dao.DataAccessException">cmmn/dataAccessFailure</prop> 
                <prop key="org.springframework.transaction.TransactionException">cmmn/transactionFailure</prop> 
                <prop key="egovframework.rte.fdl.cmmn.exception.EgovBizException">cmmn/egovError</prop> 
                <prop key="org.springframework.security.AccessDeniedException">cmmn/egovError</prop> 
            </props> 
        </property> 
    </bean> 
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" p:order="1" 
	    p:viewClass="org.springframework.web.servlet.view.JstlView" 
	    p:prefix="/WEB-INF/jsp/main/" p:suffix=".jsp"/> 
    <!-- For Pagination Tag --> 
    <bean id="imageRenderer" class="egovframework.example.cmmn.web.EgovImgPaginationRenderer"/> 
    <bean id="paginationManager" class="egovframework.rte.ptl.mvc.tags.ui.pagination.DefaultPaginationManager"> 
        <property name="rendererType"> 
            <map> 
                <entry key="image" value-ref="imageRenderer"/> 
            </map> 
        </property> 
    </bean> 
	<!-- /For Pagination Tag --> 
    <mvc:view-controller path="/cmmn/validator.do" view-name="cmmn/validator"/> 
     
</beans>

Interceptor와 Exception mapping처리 및 viewResolver 처리 수정 및 작성한다.

Interceptor의 경우 Class를 mapping 시켜서 사용.(com.util.web.Interceptor)

 

 

4. AOP 설정하기

<?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:aop="http://www.springframework.org/schema/aop" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> 
	<!-- execution(* com.main.mvc..*Service.*(..))  --> 
	<aop:config> 
		<aop:pointcut id="serviceMethod" expression="execution(* com.main.mvc..*.*(..))" /> 
		<aop:aspect ref="exceptionTransfer"> 
			<aop:after-throwing throwing="exception" pointcut-ref="serviceMethod" method="transfer" /> 
		</aop:aspect> 
	</aop:config> 
	<bean id="exceptionTransfer" class="egovframework.rte.fdl.cmmn.aspect.ExceptionTransfer"> 
		<property name="exceptionHandlerService"> 
			<list> 
				<ref bean="defaultExceptionHandleManager" /> 
				<ref bean="otherExceptionHandleManager" /> 
			</list> 
		</property> 
	</bean> 
	<bean id="defaultExceptionHandleManager" class="egovframework.rte.fdl.cmmn.exception.manager.DefaultExceptionHandleManager"> 
		<property name="reqExpMatcher"> 
			<ref bean="antPathMater"/> 
		</property> 
		<property name="patterns"> 
			<list> 
				<value>**.*</value> 
			</list> 
		</property> 
		<property name="handlers"> 
			<list> 
				<ref bean="excepHndlr" /> 
			</list> 
		</property> 
	</bean> 
	<bean id="otherExceptionHandleManager" class="egovframework.rte.fdl.cmmn.exception.manager.DefaultExceptionHandleManager"> 
		<property name="reqExpMatcher"> 
			<ref bean="antPathMater"/> 
		</property> 
		<property name="patterns"> 
			<list> 
				<value>**.*</value> 
			</list> 
		</property> 
		<property name="handlers"> 
			<list> 
				<ref bean="otherHandler" /> 
			</list> 
		</property> 
	</bean> 
	<bean id="excepHndlr" class="com.util.web.ExcepHndlr" /> 
	<bean id="otherHandler" class="com.util.web.OthersExcepHndlr" />	 
</beans>

Exception 핸들러 클래스를 mapping해서 추가 처리 MVC 영역 모든 예외처리 추가.

 

 

5. 데이터 베이스 설정하기(Oracle)

<?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:jdbc="http://www.springframework.org/schema/jdbc" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
        http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd"> 
        
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy" /> 
        <property name="url" value="jdbc:log4jdbc:oracle:thin:@localhost:1521:orcl" /> 
        <property name="username" value="hjoy"/> 
        <property name="password" value="hjoy"/> 
    </bean>
    
</beans>

오라클 + log4j 사용

 

 

6. 트랜잭션 설정하기

<?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:tx="http://www.springframework.org/schema/tx" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
    	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> 
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
		<property name="dataSource" ref="dataSource"/> 
	</bean> 
	<tx:advice id="txAdvice" transaction-manager="txManager"> 
		<tx:attributes> 
			<tx:method name="*" rollback-for="Exception"/> 
		</tx:attributes> 
	</tx:advice> 
	<aop:config> 
		<aop:pointcut id="requiredTx" expression="execution(* com.main.mvc.service..*Service.*(..))"/> 
		<aop:advisor advice-ref="txAdvice" pointcut-ref="requiredTx" /> 
	</aop:config> 
</beans>

예외가 발생 했을 때 (service transaction) transaction처리가 일어날 수 있도록 수정 및 처리

 

7. MyBatis(ORM) 설정하기

 

sqlMap.xml

<?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-3.2.xsd"> 
	<!-- SqlMap setup for iBATIS Database Layer --> 
	<bean id="sqlMapClient" class="egovframework.rte.psl.orm.ibatis.SqlMapClientFactoryBean"> 
		<property name="configLocation" value="classpath:/egovframework/sqlmap/example/sql-map-config.xml"/> 
		<property name="dataSource" ref="dataSource"/> 
	</bean> 
</beans>

sqlMap 설정

 

config.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 
<configuration> 
    <typeAliases> 
		<typeAlias alias="egovMap" type="egovframework.rte.psl.dataaccess.util.EgovMap"/> 
		<!-- <typeAlias alias="Sy1010Dto" type="com.main.mvc.dto.sy.Sy1010Dto"/> 
		<typeAlias alias="Bo1010Dto" type="com.main.mvc.dto.bo.Bo1010Dto"/> --> 
		<!-- dto 패키지 한번에 --> 
		<package name="com.main.mvc.dto" /> 
		<!-- <typeAlias alias="sampleVO" type="egovframework.example.sample.service.SampleVO"/> --> 
    </typeAliases> 
</configuration>

<package name="com.main.mvc.dto" />

패키지 기준으로 dto 및 vo 설정 한번에 한다.

 

8. 추가 컴포넌트 추가하기

<?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" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 
	<!-- DI 설정 2가지 의존성 주입 --> 
	<!-- @Resource --> 
    <context:component-scan base-package="com"> 
       <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
    </context:component-scan> 
     
    <!-- @Autowired  --> 
    <!-- <context:annotation-config/> --> 
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" > 
		<property name="maxUploadSize" value="10485760"/> 
		<property name="defaultEncoding" value="UTF-8" /> 
	</bean> 
	<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
		<property name="basenames"> 
			<list> 
				<value>classpath:/egovframework/message/message-common</value> 
				<value>classpath:/egovframework/rte/fdl/idgnr/messages/idgnr</value> 
				<value>classpath:/egovframework/rte/fdl/property/messages/properties</value> 
			</list> 
		</property> 
		<property name="cacheSeconds"> 
			<value>60</value> 
		</property> 
	</bean> 
	<bean id="leaveaTrace" class="egovframework.rte.fdl.cmmn.trace.LeaveaTrace"> 
		<property name="traceHandlerServices"> 
			<list> 
				<ref bean="traceHandlerService" /> 
			</list> 
		</property> 
	</bean> 
	<bean id="traceHandlerService" class="egovframework.rte.fdl.cmmn.trace.manager.DefaultTraceHandleManager"> 
		<property name="reqExpMatcher"> 
			<ref bean="antPathMater" /> 
		</property> 
		<property name="patterns"> 
			<list> 
				<value>*</value> 
			</list> 
		</property> 
		<property name="handlers"> 
			<list> 
				<ref bean="defaultTraceHandler" /> 
			</list> 
		</property> 
	</bean> 
	<bean id="antPathMater" class="org.springframework.util.AntPathMatcher" /> 
	<bean id="defaultTraceHandler" class="egovframework.rte.fdl.cmmn.trace.handler.DefaultTraceHandler" /> 
</beans>

필요한 설정을 추가한다.

반응형