Spring 환경설정

유재훈·2023년 2월 19일
0

Spring_Study

목록 보기
2/9

Spring setting

1. Java-version, spring-version, servlet/jsp-version 업그레이드

  • location -> pom.xml
    <properties>
    		<java-version>11</java-version>
    		<org.springframework-version>5.3.18</org.springframework-version>
    		<org.springframework.security-version>5.1.5.RELEASE</org.springframework.security-version>
    		<org.aspectj-version>1.6.10</org.aspectj-version>
    		<org.slf4j-version>1.6.6</org.slf4j-version>
    </properties>
    <!-- plugin은 pom.xml 밑에 부분에 설정해줘야한다. -->
    <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>2.5.1</version>
             <configuration>
                  <source>${java-version}</source>
                  <target>${java-version}</target>
                  <compilerArgument>-Xlint:all</compilerArgument>
                  <showWarnings>true</showWarnings>
                  <showDeprecation>true</showDeprecation>
             </configuration>
     </plugin>

1.1 servlet버젼 4.0 업그레이드

  • location -> src/main/webapp/WEB-INF/web.xml
<web-app 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 
        id="WebApp_ID" 
        version="4.0">

3. servlet-context 설정

  • mvc관련 bean을 관리하는 context
  • location -> src/main/webapp/WEB-INF/web.xml
<!-- 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>

4. EncodingFilter 등록

  • location -> src/main/webapp/WEB-INF/web.xml
<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>
	<init-param>
		<param-name>forceEncoding</param-name>
		<param-value>true</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>encodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

5.lombok

  • boilerplate code 자동생성
  • location -> pom.xml
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.18.24</version>
	<scope>provided</scope>
</dependency> 

6.mybatis외 db관련 의존

  • location -> pom.xml
<dependency>
	<groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${org.springframework-version}</version>
</dependency>
<dependency>
	<groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.2</version>
</dependency>
<!-- Database Connection Pool -->
 <dependency>
     <groupId>commons-dbcp</groupId>
     <artifactId>commons-dbcp</artifactId>
     <version>1.4</version>
 </dependency>
 <dependency>
     <groupId>com.oracle.database.jdbc</groupId>
     <artifactId>ojdbc8</artifactId>
     <version>21.1.0.0</version>
</dependency>

6.1 Datasource & mybatis 설정

  • location -> src/main/webapp/spring/root-context.xml
<bean class="org.apache.commons.dbcp.BasicDataSource" id="realDataSource" destroy-method="close">
	<property name="driverClassName" value="${datasource.driverClassName}"/>
	<property name="url" value="${datasource.url}"/>
	<property name="username" value="${datasource.username}"/>
	<property name="password" value="${datasource.password}"/>
</bean>
  • location -> src/main/resource/dataresource.properties
#driver.properties
datasource.driverClassName = oracle.jdbc.OracleDriver
datasource.url = jdbc:oracle:thin:@localhost:1521:xe
datasource.username = spring  #id
datasource.password = spring  #password

6.2 @Mapper 인터페이스 등록

  • 해당인터페이스 구현 객체 동적으로 생성해서 bean으로 사용
  • location -> src/main/webapp/spring/root-context.xml
 <mybatis-spring:scan base-package="com.sh.spring.**.dao"/>
  • @Mapper 인터페이스를 namespace로 지정
    -해당 Dao에 @Mapper 선언해줘야한다.
  • location -> src/main/resource/mapper/member-mapper.xml
<!-- MemberDao자리에 해당 Dao class 넣기 -->
<mapper namespace="com.sh.spring.member.model.dao.MemberDao">

7.spring-security 의존

  • location -> pom.xml
<dependency>
     <groupId>org.springframework.security</groupId>
     <artifactId>spring-security-core</artifactId>
     <version>${org.springframework.security-version}</version>        
</dependency>
<dependency>
	<groupId>org.springframework.security</groupId> 
    <artifactId>spring-security-web</artifactId>
    <version>${org.springframework.security-version}</version>        
</dependency>
<dependency>
	<groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>${org.springframework.security-version}</version>        
</dependency>
<dependency>
	<groupId>org.springframework.security</groupId>
    <artifactId>spring-security-taglibs</artifactId>
    <version>${org.springframework.security-version}</version>        
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-test</artifactId>
    <version>${org.springframework.security-version}</version>       
</dependency>

7.1 암호화 처리 빈 BCryptPasswordEncoder 등록

  • location -> src/main/webapp/spring/security-context.xml
<bean class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" id="bcryptPasswordEncoder" />

8.interceptor 등록

  • location -> src/main/webapp/spring/appServlet/servlet-context.xml
<interceptors>
	<interceptor>
		<mapping path="/**/" />
		<beans:bean class="com.sh.spring.common.interceptor.LoggerInterceptor"></beans:bean>
	</interceptor>
    <interceptor>
		<mapping path="/member/memberDetail.do" />
		<mapping path="/member/memberUpdate.do" />
		<beans:bean class="com.sh.spring.common.interceptor.LoginInterceptor" />
	</interceptor>
</interceptors>

9. log4jdbc-remix 실행

  • query와 resultset을 logging
  • location -> pom.xml
<dependency>
	<groupId>org.lazyluke</groupId>
    <artifactId>log4jdbc-remix</artifactId>
    <version>0.2.7</version>
</dependency> 

9.1 log4jdbc-remix의 가짜 dataSource빈 생성

  • location -> src/main/webapp/spring/root-context.xml
<bean id="dataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource">
    <constructor-arg ref="realDataSource"/>
    <property name="logFormatter">
    	<bean class="net.sf.log4jdbc.tools.Log4JdbcCustomFormatter">
         	<property name="loggingType" value="MULTI_LINE"/>
         	<property name="sqlPrefix" value="[SQL]"/>
    	</bean>
    </property>
</bean>

10. aop관련 의존 추가

  • location -> pom.xml
<dependency>
    <groupId>org.aspectj</groupId>
	<artifactId>aspectjweaver</artifactId>
	<version>${org.aspectj-version}</version>
</dependency>

10.1 aop관련 어노테이션 등록설정

  • location -> src/main/webapp/spring/appServlet/servlet-context.xml
<aop:aspectj-autoproxy />

0개의 댓글