코딩하는 문과생

[Spring] Spring 프레임워크(10) - 트랜잭션과 로깅 본문

웹 프로그래밍/Spring

[Spring] Spring 프레임워크(10) - 트랜잭션과 로깅

코딩하는 문과생 2020. 3. 12. 14:04

[스프링 트랜잭션]

주로 서비스 메서드를 대상으로 진행

 

- 선언적 트랜잭션

1. 어노테이션을 이용한 선언적 방식

  • root-context.xml에 Transaction Manager<tx:annotation-driven ../> 설정
  • 트랜잭션이 필요한 클래스나 메소드에 @Transactional 어노테이션 추가

2. AOP를 이용한 선언적 방식

  • 트랜잭션 매니저 설정
  • 트랜잭션 어드바이스 설정: <tx:acvice..>
  • AOP설정: <aop:config>

[Logging Tool]

-Logging Tool

commons-logging, log4j, logback등 툴이 있다.

SLF4J과 Logback의 비중이 늘고 있다.

 

 

ex. 트랜잭션 설정

- datasource.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"
	xmlns:context="http://www.springframework.org/schema/context"
	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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">


	<context:property-placeholder location="classpath:config/database.properties"/>
	
	<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
		<property name="driverClass" value="${db.driverClass}" />
		<property name="url" value="${db.url}" />
		<property name="username" value="${db.username}" />
		<property name="password" value="${db.password}" />
	</bean>
	
	<!-- sqlSessionFactory생성 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="configLocation" value="classpath:config/mybatis-config.xml"/>
		<property name="mapperLocations" value="classpath:mappers/*.xml" />
	</bean>
	
	<!-- sqlSessionFactory을 기반으로 sqlSession생성 -->
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg ref="sqlSessionFactory"/>
	</bean>
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 모든 메서드에 대해서 -->
			<tx:method name="*"/>
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
		<!-- 포인트컷 -->
		<aop:pointcut expression="execution(* kr.co.acomp.hello.service.*Service.*(..))" id="transactionPointCut"/>
		<!-- 어드바이즈와 결합 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointCut"/>
	</aop:config>

</beans>

 

ex. 로깅 설정

-pom.xml

...

  	<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context</artifactId>
	    <version>4.3.9.RELEASE</version>
	    <exclusions>
	    	<exclusion>
          		<groupId>commons-logging</groupId>
          		<artifactId>commons-logging</artifactId>
        	</exclusion>
	    </exclusions>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>4.3.9.RELEASE</version>
	    	    <exclusions>
	    	<exclusion>
          		<groupId>commons-logging</groupId>
          		<artifactId>commons-logging</artifactId>
        	</exclusion>
	    </exclusions>
	</dependency>

...

	<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
	<dependency>
	    <groupId>org.slf4j</groupId>
	    <artifactId>slf4j-api</artifactId>
	    <version>1.7.25</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/org.slf4j/jcl-over-slf4j -->
	<dependency>
	    <groupId>org.slf4j</groupId>
	    <artifactId>jcl-over-slf4j</artifactId>
	    <version>1.7.25</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
	<dependency>
	    <groupId>ch.qos.logback</groupId>
	    <artifactId>logback-classic</artifactId>
	    <version>1.2.3</version>
	    <scope>test</scope>
	</dependency>
    
...