728x90
300x250
[Spring-Framework] 38. Beans와 Context-XML 방식으로 DB 설정 - (Junit5)


이번에는 Spring Legacy Project로 Spring MVC Project를 생성했을 때, Resources 폴더에 등록하여 Junit5에서 태스트했을 때 사용하는 방법에 대해서 소개하려고 한다.


간단하지만, 의외로 처음 셋팅을 시도했을 때, 오류가 많이 생긴다.


글 제목에는 Beans가 적혀져 있지만, 따로 Beans 용어를 소개하진 않겠다.

이 코드를 해독하거나 태스트를 해 보면, Bean의 감을 잠시 맛볼 수는 있다.


* IDE: Eclipse 2020-06

* Framework: Spring Framework 4.2.4

* Spring-core

* Spring-tx

* Spring-jdbc

* Spring-test

* AspectJ

* AspectJWeaver



* 출력 결과


JUnit5와 Spring Framework 4.2.4 Releases의 조합으로 태스트를 하였다.



그림 1. 출력 결과 - 콘솔



그림 2. 태스트 결과





1. 프로젝트 구성도


아래의 그림은 프로젝트 구성도이다.

주로 작업할 코드는 "TestUnit.java", "applicationContext.xml", "db.properties", "pom.xml"이다.



그림 3. 프로젝트 구성도



2. Java Compiler, Build Paths, Project Factes



/src/main/webapp/WEB-INF/lib에 oracle JDBC.jar(ojdbc.jar) 파일 넣어주기


* Java Compiler - compiler compliance level 1.8

* Build Path - JUnit 5, JRE System Library (JavaSE 1.8) 등

   - Add Jar로 ojdbc.jar 파일 등록해주기

* Project Factes - Java : 1.8




3. pom.xml


(중략)


 <properties>
  <java-version>1.8</java-version>
  <org.springframework-version>4.2.4.RELEASE</org.springframework-version>
  <org.aspectj-version>1.6.10</org.aspectj-version>
  <org.slf4j-version>1.6.6</org.slf4j-version>
 </properties>


(중략)

  
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${org.springframework-version}</version>
  </dependency>
  
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${org.springframework-version}</version>
  </dependency>
  
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${org.springframework-version}</version>

  </dependency>


  <!-- 밑줄 친 이유는 mvnrepository에서 찾아보면, scope라는 항목이 추가적으로 되어 있음. 지워야 동작함. -->
   
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${org.springframework-version}</version>
  </dependency>
    



  <!-- AOP 구현할 경우에는 필요함. (AspectJ) -->


  <!-- AspectJ -->
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjrt</artifactId>
   <version>${org.aspectj-version}</version>
  </dependency> 
  
  <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
  <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>${org.aspectj-version}</version>
      <scope>runtime</scope>
  </dependency>
  




4. db.properties(/src/main/resources)


jdbc.driver=org.h2.Driver
jdbc.url=jdbc:h2:tcp://localhost/~/test
jdbc.username=sa
jdbc.password=
MYSQL_DB_URL=jdbc:mysql://localhost:3306/dbanme?useUnicode=true&characterEncoding=utf8
MYSQL_DB_USERNAME=
MYSQL_DB_PASSWORD=
ORACLE_DB_URL=jdbc:oracle:thin:@127.0.0.1:1521:xe
ORACLE_DB_USERNAME=HR
ORACLE_DB_PASSWORD=1234


파일명: db.properties


[첨부(Attachments)]

db.zip




5. applicationContext.xml(/src/main/resources)


<?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:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 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.2.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

 <context:component-scan base-package="com.website.example" />

 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

 <!-- DataSource 설정 -->
 <context:property-placeholder location="classpath:db.properties" />

 <!-- DataSource 셋팅 -->
    <bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
        <property name="URL" value="${ORACLE_DB_URL}" />
        <property name="user" value="${ORACLE_DB_USERNAME}"/>
        <property name="password" value="${ORACLE_DB_PASSWORD}"/>
        <property name="connectionCachingEnabled" value="true"/>
    </bean>
   
 <!-- Spring JDBC 설정 -->
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource" />
 </bean>
 

 <!-- 미구현하였으나 트랜젝션 고려중 -->

 <!-- Transaction 설정 -->
 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"></property>
 </bean>

 

 <!-- 미구현하였으나 AOP 고려중 -->

 <tx:advice id="txAdvice" transaction-manager="txManager">
  <tx:attributes>
   <tx:method name="get*" read-only="true"/>
   <tx:method name="*"/>
  </tx:attributes>
 </tx:advice>

 
 <!-- 미구현하였으나 AOP 고려중 -->
 <aop:config>
  <aop:pointcut id="txPointcut"  expression="execution(* com.website.example.service.DBService..*(..))"/>
  
  <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
 </aop:config>
 
</beans>



파일명: applicationContext.xml


[첨부(Attachments)]

applicationContext.zip


[추가]


     <!-- MySQL - DataSource 셋팅 -->
    <bean id="dataSource2" class="com.mysql.cj.jdbc.MysqlDataSource">
        <property name="URL" value="${ORACLE_DB_URL}" />
        <property name="user" value="${ORACLE_DB_USERNAME}"/>
        <property name="password" value="${ORACLE_DB_PASSWORD}"/>
    </bean>


    <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> -->
 
     <!-- 2. Oracle - DataSource 셋팅 -->
    <bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
        <property name="URL" value="${ORACLE_DB_URL}" />
        <property name="user" value="${ORACLE_DB_USERNAME}"/>
        <property name="password" value="${ORACLE_DB_PASSWORD}"/>
        <property name="connectionCachingEnabled" value="true"/>
    </bean>

    <!-- 3. Apache DBCP -->

   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
         <property name="driverClassName" value="${jdbc.driverClassName}" />
         <property name="url" value="${jdbc.url}" />
         <property name="username" value="${jdbc.username}" />
         <property name="password" value="${jdbc.password}" />
   </bean>


    <!-- 4. HikariCP -->
    <!--
    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
         <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />
         <property name="username" value="username" />
         <property name="password" value="password" />
    </bean>
 
    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
          <constructor-arg ref="hikariConfig"/>
    </bean>
     -->



    <!-- 5. MariaDB -->
    <!--
    <bean id="dataSource4" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="org.mariadb.jdbc.Driver"/>
        <property name="url" value="jdbc:mariadb://localhost:3306/test"></property>
        <property name="username" value="root"/>
        <property name="password" value="test"/>
    </bean>
     -->
    
    <!-- 6. MyBatis -->
    <!--
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource4"/>
        <property name="configLocation" value="classpath:mybatis/config.xml"/>
        <property name="mapperLocations" value="classpath:mybatis/sql/*.xml"></property>
    </bean>
   
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
   
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource4"/>
    </bean>
   
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
     -->





6. TestUnit.java (com/website/example/unit)


package com.website.example.unit;

import static org.junit.jupiter.api.Assertions.*;

import java.sql.Connection;
import java.sql.SQLException;

import javax.inject.Inject;
import javax.sql.DataSource;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;


class TestUnit {
 
        private DataSource dataSource;
        private ApplicationContext ctx;
 
        // 태스트 환경에서 Autowired 버리기(JUnit 5 to Spring Framework 4.24 일 떄)
 

        @Test
        void test() {
  
               // 방법 1. (ApplicationContext로 Bean 불러오기)
               ctx = new GenericXmlApplicationContext("applicationContext.xml");
               this.dataSource = (DataSource) ctx.getBean("dataSource");
  
               if ( this.ctx != null ) {
                     System.out.println("야1");
               }
  
               if ( this.dataSource != null ) {
                     System.out.println("야2");
               }
  
               try(Connection con = dataSource.getConnection()){
                     System.out.println("참");
    
              }catch(Exception e)
              {
                    //fail(e.getMessage());
                    System.out.println("거짓");
    
              }
  
      }

}


파일명: TestUnit.java


[첨부(Attachments)]

TestUnit.zip


applicationContext.zip



* 맺음글(Conclusion)


연속해서 /src/main/webapp/WEB-INF/spring/root-context.xml 등의 작업을 소개하면 이해가 되지 않을 수 있어서 핵심 위주로 간단하지만, 

중요한 키 포인트를 위주로 정리하였다.


환경 셋팅 관련해서 허공에 삽질을 적게 하였으면 하는 바람이다.


[Tomcat 배포, 웹 개발]

1. [Spring Framework] 39. Beans와 Context-XML 방식으로 DB 설정 그리고 Autowired - (Web.xml), 2020-10-10

- https://yyman.tistory.com/1464


반응형
728x90
300x250
[Spring-Framework] 37. Spring-JDBCTemplate - 트랜젝션 (어노테이션, Java - AOP)


이번에 소개할 내용은 지난 36번 글에 이어서 AOP를 추가로 적용한 방법에 대해 소개하려고 한다.

어노테이션 방식의 트랜젝션 구축이 이해가 되지 않으면, 이전 글을 참고하면 도움이 된다.


1. [Spring-Framework] 36. Spring-JDBCTemplate - 트랜젝션 (어노테이션, Java 설정), 2020-10-10

- https://yyman.tistory.com/1461


IDE: Eclipse 2020-06

- Spring Framework 4.2.4 Releases

- Spring-JDBC

- Spring-TX

- Spring-Core

- AspeetJ Weaver

- AspectJ


DB: Oracle Databases 11g (Express Edition)



1. 프로젝트 구성도(비교)


AOP 작업을 추가한 프로젝트와 기존의 프로젝트의 구성을 비교해놓은 것이다.

이 글에서는 이전의 작성된 객체 파일을 그대로 복사 붙여넣기하여 AOP패키지에 적용하였다.


LogAdvisor.java 파일 말고는 추가 코드를 작성하진 않았다.

MainTestAOP.java도 복사, 붙여넣기해서 만든 것이다. 가리키고 있는 명칭만 변경하였다.



 

 



그림 1, 그림 2. 
(Spring 트랜젝션 + Spring JDBC) + AOP

그림 3, 그림 4. 이전 프로젝트
(Spring 트랜젝션 + Spring JDBC)



2. pom.xml 설정하기


((중략) - 추가된 사항)
    
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${org.springframework-version}</version>
  </dependency>

  
    
  <!-- AspectJ -->
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjrt</artifactId>
   <version>${org.aspectj-version}</version>
  </dependency> 
  

  <!-- AspectJWeaver 추가 -->
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>${org.aspectj-version}</version>
  </dependency>
  





3. LogAdvisor.java(com.website.example.aop)


package com.website.example.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Service;


//관점, 서비스
@Aspect
@Service
public class LogAdvisor {


   // 2단계 - 전 단계 시야)
 
  @Before("execution(* com.website.example.aop.service.AccountServiceAOP.*(..))")
  // @Before("execution(public void sum())")
  // 반환값 없어도 무방
  
   public void logBefore() {
           System.out.println("전 단계");


   }

 
}


파일명: LogAdvisor.java


[첨부(Attachments)]

LogAdvisor.zip



4. AccountServiceAOP.java(com.website.example.aop.service) - 예(복사, 붙여넣기)


코드 자체를 복사 붙여넣기 한 것이다.

AOP를 왜 하는지 철학을 이해하라고 하나 예로 보여주는 것이다.



package com.website.example.aop.service;

import java.sql.SQLException;


import com.website.example.vo.AccountVO;


public interface AccountServiceAOP {

 void accountCreate(AccountVO vo) throws SQLException;
 void accountTransfer(String sender, String receiver, int money) throws SQLException;
 
}




5. AccountServiceImplAOP.java(com.website.example.aop.service) - 예(복사, 붙여넣기)


하나 어노테이션 @Repository를 달아주었다. 그거 말고는 동일하다.



package com.website.example.aop.service;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import com.website.example.dao.AccountDAO;
import com.website.example.dao.AccountDAOImpl;
import com.website.example.vo.AccountVO;


@Repository
@Transactional
public class AccountServiceImplAOP implements AccountServiceAOP{

 private AccountDAO accountDAO;
 private DataSource ds = null;
 
 public AccountServiceImplAOP(DataSource ds) {
  this.accountDAO = new AccountDAOImpl(ds);
  this.ds = ds;
 }

 @Override
 @Transactional(propagation=Propagation.NEVER)
 public void accountCreate(AccountVO vo) throws SQLException {
  accountDAO.createAccount(vo); 
  System.out.println("create CurrentTransactionName: " + TransactionSynchronizationManager.getCurrentTransactionName());
 }
 
 @Override
 @Transactional
 public void accountTransfer(String sender, String receiver, int money) throws SQLException {
  
     int balance = accountDAO.getBalance(sender); // 보내는 사람 잔액 체크
     
        if(balance >= money){ // 보내는 돈이 잔액보다 많으면
     
         System.out.println("transfer CurrentTransactionName: " + TransactionSynchronizationManager.getCurrentTransactionName() );
         
   accountDAO.minus(sender, money);
   accountDAO.plus(receiver, money);
   
        } else{

         System.out.println("돈 없음");
         //throw new NoMoneyException();
        }
  
 }


}




6. RootConfigAOP.java(com.website.example.common) - 예(복사, 붙여넣기)



package com.website.example.common;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@Import({DBConfigAOP.class})
@ComponentScan(basePackages = {"com.website.example"})
@EnableAspectJAutoProxy
//@ComponentScan(basePackages = {"com.local.example.beans", "com.local.example.advisor"})
public class RootConfigAOP {


}




7. DBConfigAOP.java(com.website.example.common) - 예(복사, 붙여넣기)


package com.website.example.common;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.website.example.dao.AccountDAO;
import com.website.example.dao.AccountDAOImpl;
import com.website.example.aop.service.AccountServiceAOP;
import com.website.example.aop.service.AccountServiceImplAOP;


@Configuration
@EnableTransactionManagement
public class DBConfigAOP {

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
   
    @Bean
    public DataSource dataSource() {
     
     DataSource dataSource = new MyDataSourceFactory().getOracleDataSource();
     
     /* Apache DBCP
     
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/testDb?useUnicode=true&characterEncoding=utf8");
        dataSource.setUsername("test");
        dataSource.setPassword("test123!@#");
       
        */
        return dataSource;
    }
   
    @Bean
    public AccountDAO accountDAOImpl() {
     
     AccountDAO dao = new AccountDAOImpl(dataSource());
     return dao;
    }
   
    @Bean
    public AccountServiceAOP accountServiceImplAOP() {
     
     AccountServiceAOP service = new AccountServiceImplAOP(dataSource());

     return service;
    }
   
}




8. MainTestAOP.java(com.website.example.unit) - 예(복사, 붙여넣기)



package com.website.example.unit;


import java.sql.SQLException;

import java.sql.Timestamp;

import javax.sql.DataSource;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.website.example.common.MyDataSourceFactory;
import com.website.example.common.RootConfigAOP;

import com.website.example.dao.AccountDAOImpl;
import com.website.example.aop.service.AccountServiceAOP;
import com.website.example.aop.service.AccountServiceImplAOP;

import com.website.example.vo.AccountVO;


class MainTestAOP {

 @Test
 void test() throws SQLException {
  
       @SuppressWarnings("resource")
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(RootConfigAOP.class);

  
        AccountServiceAOP service = (AccountServiceAOP) applicationContext.getBean("accountServiceImplAOP");
  
  AccountVO vo = new AccountVO();
  
  // 1. 계정 생성
  vo.setName("홍길동2");
  vo.setBalance(10000);
  vo.setRegidate(Timestamp.valueOf("2020-01-20 11:05:20"));
  service.accountCreate(vo);
  
  // 2. 계정 생성
  vo.setName("홍길자2");
  vo.setBalance(0);
  vo.setRegidate(Timestamp.valueOf("2020-01-20 23:05:20"));
  service.accountCreate(vo);
  
  // 3. 거래 처리
  service.accountTransfer("홍길동", "홍길자", 500);
  
  
 }


}




* 맺음글(Conclusion)


AOP를 쉽고 간단하게 트랜젝션과 연결해서 사용하는 방법에 대해서 소개하였다.



반응형
728x90
300x250
[Spring-Framework] 36. Spring-JDBCTemplate - 트랜젝션 (어노테이션, Java 설정)


이번 글에서 소개할 내용은 스프링 프레임워크의 트랜젝션을 조금 더 다뤄보려고 한다.


어노테이션을 사용하는 데 있어서 방법이 두 가지가 있다.


- 1. Java Config 파일 구성 방식

- 2. (context.xml) XML 파일 구성 방식


* 참고로 스프링 프레임워크는 셋팅이 시작의 반이라고 보면 될 것 같다.


[작업 환경]

IDE: Eclipse 2020-06

Framework: Spring Framework 4.2.4 RELEASE

spring-jdbc

spring-core

spring-tx

mysql-connector-java (8.0.21)

oracle jdbc (WEB-INF/lib에 넣어줘야 함.)


OpenJDK 15 (프로젝트에 적용한 자바 버전은 1.8로 셋팅하였음.)


딱딱한 글도 중요하지만, 영상으로 이게 무엇인지 소개해주겠다. (작동 원리 시연)



영상 1. Spring Framework 4.2.4 RELEASES - Transactions (시연)





1. 프로젝트 구성


프로젝트 구성에 관한 것이다.



그림 1. 프로젝트 구성도



2. POM.xml 구성하기



 <properties>
  <java-version>1.8</java-version>
  <org.springframework-version>4.2.4.RELEASE</org.springframework-version>
  <org.aspectj-version>1.6.10</org.aspectj-version>
  <org.slf4j-version>1.6.6</org.slf4j-version>
 </properties>


  (중략)

  
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${org.springframework-version}</version>
  </dependency>
  
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
  <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-jdbc</artifactId>
       <version>${org.springframework-version}</version>
  </dependency>
  
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${org.springframework-version}</version>
  </dependency>
    
  (중략)

  <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.21</version>
  </dependency>




3. Build Path, Project Factes, Java compiler 설정하기


* Build Path -> JRE System Library 1.8, JUnit 5

* Java Compiler -> Compiler compliance level: 1.8

* Project Factes -> Java : 1.8


* 오라클 JDBC: WEB-INF/lib폴더에 ojdbc.jar 파일 넣어주기 




4. AccountTbl.sql


-- Transaction 실습 DB (은행 - Account)
-- Oracle 11 - 자동번호 생성 테이블 정의
-- Table 생성 (FOODMENU_TBL)
-- NEW.ID (Table의 id를 가리킴)
CREATE TABLE account_tbl
(
    idx NUMBER PRIMARY KEY,
    name VARCHAR2(30),
    balance NUMBER,
    regidate TIMESTAMP
);

-- Sequence 정의
CREATE SEQUENCE account_sequence
START WITH 1
INCREMENT BY 1;


-- Trigger 생성
-- BEFORE INSERT on '테이블명'
CREATE OR REPLACE TRIGGER account_trigger
BEFORE INSERT
    ON account_tbl
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT account_sequence.nextval INTO :NEW.IDX FROM dual;
END;


파일명: AccountTBL.sql


[첨부(Attachments)]

AccountTbl.zip




4. db.properties (/src/main/resources)


MYSQL_DB_URL=jdbc:mysql://localhost:3306/dbanme?useUnicode=true&characterEncoding=utf8
MYSQL_DB_USERNAME=
MYSQL_DB_PASSWORD=
ORACLE_DB_URL=jdbc:oracle:thin:@127.0.0.1:1521:xe
ORACLE_DB_USERNAME=HR
ORACLE_DB_PASSWORD=1234


파일명: db.properties


[첨부(Attachments)]

db.zip



5. AccountVO.java (com.website.example.vo)


package com.website.example.vo;

import java.sql.Timestamp;;

public class AccountVO {
 
      private int idx;
      private String name;
      private int balance;
      private Timestamp regidate;
 
      public int getIdx() {
          return idx;
      }
 
      public void setIdx(int idx) {
          this.idx = idx;
      }
 
      public String getName() {
          return name;
      }
 
      public void setName(String name) {
          this.name = name;
      }
 
      public int getBalance() {
          return balance;
     }

     public void setBalance(int balance) {
          this.balance = balance;
     }


     public Timestamp getRegidate() {
         return regidate;
     }
 
     public void setRegidate(Timestamp regidate) {
         this.regidate = regidate;
     }
 
}


파일명: AccountVO.java


[첨부(Attachments)]

AccountVO.zip




6. DBConfig.java (com.website.example.common)


package com.website.example.common;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.website.example.dao.AccountDAO;
import com.website.example.dao.AccountDAOImpl;
import com.website.example.service.AccountService;
import com.website.example.service.AccountServiceImpl;

@Configuration
@EnableTransactionManagement

public class DBConfig {

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
   
    @Bean
    public DataSource dataSource() {
     
     DataSource dataSource = new MyDataSourceFactory().getOracleDataSource();
     
     /* Apache DBCP
     
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/testDb?useUnicode=true&characterEncoding=utf8");
        dataSource.setUsername("test");
        dataSource.setPassword("test123!@#");
       
        */
        return dataSource;
    }
   
    @Bean
    public AccountDAO accountDAOImpl() {
     
     AccountDAO dao = new AccountDAOImpl(dataSource());
     return dao;
    }
   
    @Bean
    public AccountService accountServiceImpl() {
     
     AccountService service = new AccountServiceImpl(dataSource());
     return service;
    }
   
}


파일명: DBConfig.java


[첨부(Attachments)]

DBConfig.zip




7. RootConfig.java (com.website.example.common)


package com.website.example.common;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@Import({DBConfig.class})
@ComponentScan(basePackages = {"com.website.example"})
//@ComponentScan(basePackages = {"com.local.example.beans", "com.local.example.advisor"})
public class RootConfig {


}


파일명: RootConfig.java


[첨부(Attachments)]

RootConfig.zip




8. MyDataSourceFactory.java (com.website.example.common)



package com.website.example.common;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.mysql.cj.jdbc.MysqlDataSource;

import oracle.jdbc.pool.OracleDataSource;

public class MyDataSourceFactory {
 
 private InputStream is = null;
    private Properties props = null;
 
 public MyDataSourceFactory()  {
  
        String resource = "db.properties";
        this.is = getClass().getClassLoader().getResourceAsStream(resource);
        this.props = new Properties();
       
 }
 
 public DataSource getMySQLDataSource() {
  
        MysqlDataSource mysqlDS = null;
       
        try {
         
            props.load(is);
            mysqlDS = new MysqlDataSource();
            mysqlDS.setURL(props.getProperty("MYSQL_DB_URL"));
            mysqlDS.setUser(props.getProperty("MYSQL_DB_USERNAME"));
            mysqlDS.setPassword(props.getProperty("MYSQL_DB_PASSWORD"));
           
        } catch (IOException e) {
            e.printStackTrace();
        }
       
        return mysqlDS;
       
    }
    
    public DataSource getOracleDataSource(){
     
        OracleDataSource oracleDS = null;
       
        try {
         
            props.load(is);
            oracleDS = new OracleDataSource();
            oracleDS.setURL(props.getProperty("ORACLE_DB_URL"));
            oracleDS.setUser(props.getProperty("ORACLE_DB_USERNAME"));
            oracleDS.setPassword(props.getProperty("ORACLE_DB_PASSWORD"));
           
           
        } catch (IOException e) {
         
            e.printStackTrace();
           
        } catch (SQLException e) {
         
            e.printStackTrace();
           
        }
       
        return oracleDS;
       
    }

}


파일명: MyDataSourceFactory.java


[첨부(Attachments)]

MyDataSourceFactory.zip




9. AccountDAO.java (com.website.example.dao)


package com.website.example.dao;

import java.sql.SQLException;

import com.website.example.vo.AccountVO;


public interface AccountDAO {
 
       void createAccount(AccountVO vo) throws SQLException;
       int getBalance(String name);
       void minus(String name, int money) throws SQLException;
       void plus(String name, int money) throws SQLException;
 
}


파일명: AccountDAO.java


[첨부(Attachments)]

AccountDAO.zip

MyDataSourceFactory.zip




10. AccountRowMapper.java (com.website.example.dao)



package com.website.example.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.website.example.vo.AccountVO;

@Service
public class AccountRowMapper implements RowMapper<AccountVO>  {


      @Override
      public AccountVO mapRow(ResultSet rs, int rowNum) throws SQLException {
  
             AccountVO vo = new AccountVO();

             vo.setIdx(rs.getInt(1));
             vo.setName(rs.getString(2));
             vo.setBalance(rs.getInt(3));
             vo.setRegidate(rs.getTimestamp(4));
  
             return vo;
      }

}


파일명: AccountRowMapper.java


[첨부(Attachments)]

AccountRowMapper.zip





11. AccountDAOImpl.java (com.website.example.dao)


package com.website.example.dao;

import java.sql.SQLException;
import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import com.website.example.vo.AccountVO;


public class AccountDAOImpl implements AccountDAO {

       // Spring Framework - JDBC
       private JdbcTemplate jdbcTemplate = null;
       private DataSource ds = null;
 
       private final String INSERT = "insert into account_tbl(name, balance, regidate) values(?, ?, ?)";
       private final String SELECT_BALANCE = "select * from account_tbl where name = ?";
       private final String UPDATE_MINUS = "update account_tbl set balance = (select balance from account_tbl where name = ?) - ? " +
          " where name = ?";
       private final String UPDATE_PLUS = "update account_tbl set balance = (select balance from account_tbl where name = ?) + ? " +
          " where name = ?";
 
       public AccountDAOImpl(DataSource ds) {
             this.jdbcTemplate = new JdbcTemplate(ds);
             this.ds = ds;
       }
 
       public void createAccount(AccountVO vo) throws SQLException {
   
             this.jdbcTemplate.update(INSERT, vo.getName(), vo.getBalance(), vo.getRegidate());  
       }
 
       public int getBalance(String name){
     
             Object[] args = {name};
     
             AccountVO vo = this.jdbcTemplate.queryForObject(SELECT_BALANCE, args, new AccountRowMapper());
       
             int result = vo.getBalance();
     
             return result;
       }

    
       public void minus(String name, int money) throws SQLException{
     
              this.jdbcTemplate.update(UPDATE_MINUS, name, money, name);
       
       }
   
       public void plus(String name, int money) throws SQLException{

            /*
            // 예외 발생시키기
            if(true){
                throw new RuntimeException("런타임 에러");
            }
             */
     
            this.jdbcTemplate.update(UPDATE_PLUS, name, money, name);
       }
 
}


파일명: AccountDAOImpl.java


[첨부(Attachments)]

AccountDAOImpl.zip





12. AccountService.java (com.website.example.service)



package com.website.example.service;

import java.sql.SQLException;


import com.website.example.vo.AccountVO;


public interface AccountService {

 void accountCreate(AccountVO vo) throws SQLException;
 void accountTransfer(String sender, String receiver, int money) throws SQLException;
 
}


파일명: AccountService.java


[첨부(Attachments)]

AccountService.zip




13. AccountServiceImpl.java (com.website.example.service)



package com.website.example.service;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.website.example.dao.AccountDAO;
import com.website.example.dao.AccountDAOImpl;
import com.website.example.vo.AccountVO;

@Repository
@Transactional
public class AccountServiceImpl implements AccountService{


       private AccountDAO accountDAO;
       private DataSource ds = null;
 
       public AccountServiceImpl(DataSource ds) {
             this.accountDAO = new AccountDAOImpl(ds);
             this.ds = ds;
       }

      @Override
       public void accountCreate(AccountVO vo) throws SQLException {
             this.accountDAO.createAccount(vo);  
       }
 
      @Override
      public void accountTransfer(String sender, String receiver, int money) throws SQLException {
  
            int balance = accountDAO.getBalance(sender); // 보내는 사람 잔액 체크
      
            if(balance >= money){ // 보내는 돈이 잔액보다 많으면
     
                 accountDAO.minus(sender, money);
                 accountDAO.plus(receiver, money);
              
        } else{

                 System.out.println("돈 없음");
                 //throw new NoMoneyException();
        }
  
      }


}


파일명: AccountServiceImpl.java


[첨부(Attachments)]

AccountServiceImpl.zip





14. MainTest.java (com.website.example.unit)


package com.website.example.unit;


import java.sql.SQLException;

import java.sql.Timestamp;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.website.example.common.RootConfig;
import com.website.example.service.AccountService;
import com.website.example.vo.AccountVO;


class MainTest {

      
      @Test
      void test() throws SQLException {
  
           @SuppressWarnings("resource")
           ApplicationContext applicationContext = new AnnotationConfigApplicationContext(RootConfig.class);
  
           AccountService service = (AccountService) applicationContext.getBean("accountServiceImpl");
  
           AccountVO vo = new AccountVO();
  
  /*
  // 1. 계정 생성
  vo.setName("홍길동");
  vo.setBalance(10000);
  vo.setRegidate(Timestamp.valueOf("2020-01-20 10:05:20"));
  service.accountCreate(vo);
  
  // 2. 계정 생성
  vo.setName("홍길자");
  vo.setBalance(0);
  vo.setRegidate(Timestamp.valueOf("2020-01-20 22:05:20"));
  service.accountCreate(vo);
  */
  
          // 3. 거래 처리
          service.accountTransfer("홍길동", "홍길자", 500);
  
      }

}


파일명: MainTest.java


[첨부(Attachments)]

MainTest.zip




* 맺음글(Conclusion)


트랜젝션을 어노테이션 방식으로 Java Config 환경설정으로 구성하여 사용하는 방법에 대해서 소개하였다.


[Spring-Framework] 37. Spring-JDBCTemplate - 트랜젝션 (어노테이션, Java - AOP)

- https://yyman.tistory.com/1462



* 참고 자료(References)


1. Spring JDBC, https://velog.io/@koseungbin/Spring-JDBC, Accessed by 2020-10-10, Last Modified 2020-07-08.

2. Spring JDBC using Annotation based configuration, https://www.topjavatutorial.com/frameworks/spring/spring-jdbc/spring-jdbc-using-annotation-based-configuration/, Accessed by 2020-10-10, Last Modified 2016-02-14.

반응형
728x90
300x250
[Spring-Framework] 35. Spring-JDBCTemplate - 트랜젝션 (어노테이션 X)


@Transactional이라는 어노테이션 기능 방식이 아닌 직접 구현으로 트랜젝션을 Spring JDBC에서 다루는 방법을 소개하려고 한다.


아마 이전의 Spring Framework의 34번 글을 실습해보았다면, 희안하게 insert 명령은 수행되어서 SQL Developer로 SELECT문으로 출력했을 때는 화면에 보이는데, Java에서 DB 질의로 SELECT문으로 조회한다면 동작이 되는 경우도 있고, 안 된 경우도 있었을 거 같다.


이 문제는 commit()을 했느냐 안 했느냐의 차이에서 발생한다.


1. [Spring-Framework] 34. Spring JDBCTemplate - 사용하기, 2020. 10. 9. 21:01

- https://yyman.tistory.com/1459


이 글의 개선 버전이라고 보면 되겠다.


- IDE: Eclipse 2020-06

- Spring Framework 4.2.4 RELEASES



1. 프로젝트 구성


다음 그림은 프로젝트를 구성하고 있는 구성도이다.



그림 1. 프로젝트 구성도


이전 글에서는 프로젝트 구성도까진 필요할까 고민했는데, 심플하게 적어주는 것이 더 나아보여서 핵심만 적었다.

지금 글에서는 Spring JDBC에서 트랜젝션을 제대로 사용하는 방법에 대해서 소개하려고 한다.



2. Project Factes, Build Paths, Java Compiler 버전 변경해주기


* Build Paths -> JRE Library 1.8로 변경, Junit 5 추가할 것

* Java Compiler -> Compiler complicance Level: 1.8 환경으로 바꿔줄 것

* Project Factes -> Java 버전 1.8로 변경할 것


- 오라클 JDBC는 WEB-INF/lib에 넣어서 해결해주면 된다.

  (Build Paths에 등록도 해줘야 함.)




3. pom.xml 설정하기



 <properties>
  <java-version>1.8</java-version>
  <org.springframework-version>4.2.4.RELEASE</org.springframework-version>
  <org.aspectj-version>1.6.10</org.aspectj-version>
  <org.slf4j-version>1.6.6</org.slf4j-version>
 </properties>
 <dependencies>


(중략)


  <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
  <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-jdbc</artifactId>
       <version>${org.springframework-version}</version>
  </dependency>

(중략)



  <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.21</version>
  </dependency>





4. AccountTbl.sql


-- Transaction 실습 DB (은행 - Account)
-- Oracle 11 - 자동번호 생성 테이블 정의
-- Table 생성 (FOODMENU_TBL)
-- NEW.ID (Table의 id를 가리킴)
CREATE TABLE account_tbl
(
    idx NUMBER PRIMARY KEY,
    name VARCHAR2(30),
    balance NUMBER,
    regidate TIMESTAMP
);

-- Sequence 정의
CREATE SEQUENCE account_sequence
START WITH 1
INCREMENT BY 1;

-- Trigger 생성
-- BEFORE INSERT on '테이블명'
CREATE OR REPLACE TRIGGER account_trigger
BEFORE INSERT
    ON account_tbl
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT account_sequence.nextval INTO :NEW.IDX FROM dual;
END;


파일명: AccountTbl.sql


[첨부(Attachments)]

AccountTbl.zip



5. MyDataSourceFactory.java (com.website.example.common)


package com.website.example.common;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.mysql.cj.jdbc.MysqlDataSource;

import oracle.jdbc.pool.OracleDataSource;

public class MyDataSourceFactory {
 
 private InputStream is = null;
    private Properties props = null;
 
 public MyDataSourceFactory()  {
  
        String resource = "db.properties";
        this.is = getClass().getClassLoader().getResourceAsStream(resource);
        this.props = new Properties();
       
 }
 
 public DataSource getMySQLDataSource() {
  
        MysqlDataSource mysqlDS = null;
       
        try {
         
            props.load(is);
            mysqlDS = new MysqlDataSource();
            mysqlDS.setURL(props.getProperty("MYSQL_DB_URL"));
            mysqlDS.setUser(props.getProperty("MYSQL_DB_USERNAME"));
            mysqlDS.setPassword(props.getProperty("MYSQL_DB_PASSWORD"));
           
        } catch (IOException e) {
            e.printStackTrace();
        }
       
        return mysqlDS;
       
    }
    
    public DataSource getOracleDataSource(){
     
        OracleDataSource oracleDS = null;
       
        try {
         
            props.load(is);
            oracleDS = new OracleDataSource();
            oracleDS.setURL(props.getProperty("ORACLE_DB_URL"));
            oracleDS.setUser(props.getProperty("ORACLE_DB_USERNAME"));
            oracleDS.setPassword(props.getProperty("ORACLE_DB_PASSWORD"));
           
           
        } catch (IOException e) {
         
            e.printStackTrace();
           
        } catch (SQLException e) {
         
            e.printStackTrace();
           
        }
       
        return oracleDS;
       
    }

}


파일명: MyDataSourceFactory.java


[첨부(Attachments)]

MyDataSourceFactory.zip


AccountTbl.zip



6. AccountVO.java (com.website.example.vo)


package com.website.example.vo;

import java.sql.Timestamp;;


public class AccountVO {
 
     private int idx;
     private String name;
     private int balance;
     private Timestamp regidate;
 
     public int getIdx() {
          return idx;
     }
 
     public void setIdx(int idx) {
          this.idx = idx;
     }
 
     public String getName() {
          return name;
     }
 
     public void setName(String name) {
          this.name = name;
     }
 
     public int getBalance() {
          return balance;
     }

 

     public void setBalance(int balance) {
          this.balance = balance;
     }


     public Timestamp getRegidate() {
          return regidate;
     }
 
     public void setRegidate(Timestamp regidate) {
          this.regidate = regidate;
      }
   
}


파일명: AccountVO.java


[첨부(Attachments)]

AccountVO.zip

AccountTbl.zip



7. AccountDAO.java (com.website.example.dao)


package com.website.example.dao;


import java.sql.SQLException;

import com.website.example.vo.AccountVO;


public interface AccountDAO {
 
       void createAccount(AccountVO vo) throws SQLException;
       int getBalance(String name);
       void minus(String name, int money) throws SQLException;
       void plus(String name, int money) throws SQLException;
 
}


파일명: AccountDAO.java


[첨부(Attachments)]

AccountDAO.zip



8. AccountDAOImpl.java (com.website.example.dao)


package com.website.example.dao;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import com.website.example.common.MyDataSourceFactory;
import com.website.example.vo.AccountVO;

public class AccountDAOImpl implements AccountDAO {


       // Spring Framework - JDBC
       private JdbcTemplate jdbcTemplate = null;
       private DataSource ds = null;
 
       private final String INSERT = "insert into account_tbl(name, balance, regidate) values(?, ?, ?)";
       private final String SELECT_BALANCE = "select * from account_tbl where name = ?";
       private final String UPDATE_MINUS = "update account_tbl set balance = (select balance from account_tbl where name = ?) - ? " +
          " where name = ?";
       private final String UPDATE_PLUS = "update account_tbl set balance = (select balance from account_tbl where name = ?) + ? " +
          " where name = ?";
 
 
        public AccountDAOImpl(DataSource ds) {
               this.jdbcTemplate = new JdbcTemplate(ds);
               this.ds = ds;
        }
 
         public void createAccount(AccountVO vo) throws SQLException {
  
                TransactionSynchronizationManager.initSynchronization(); // 동기화
                Connection c = DataSourceUtils.getConnection(ds); //커넥션을 생성

  
                // 데이터 저장소 바인딩(트랜젝션)
                c.setAutoCommit(false); //트랜잭션을 시작
  
                try{
   
                    this.jdbcTemplate.update(INSERT, vo.getName(), vo.getBalance(), vo.getRegidate());
                    c.commit(); // 커밋
       
               }
               catch(Exception e) {
                    c.rollback(); // 예외가 발생하면 롤백
       
               }finally {
      
                    DataSourceUtils.releaseConnection(c, ds); // 스프링 유틸리티를 이용해 DB커넥션을 닫는다.
                    TransactionSynchronizationManager.unbindResource(ds); // 동기화 작업을 종료하고
                    TransactionSynchronizationManager.clearSynchronization();     // 정리한다.
      
               }

  
 }
 
    public int getBalance(String name){
     
     Object[] args = {name};
     
     AccountVO vo = this.jdbcTemplate.queryForObject(SELECT_BALANCE, args, new AccountRowMapper());
       
        int result = vo.getBalance();
     
        return result;
    }
    
    public void minus(String name, int money) throws SQLException{

     // 예외 발생시키기
     if(true){
      throw new SQLException(); // 의도적 예외 발생
        }
     
        this.jdbcTemplate.update(UPDATE_MINUS, name, money, name);
       
    }
    
    public void plus(String name, int money) throws SQLException{
          
        this.jdbcTemplate.update(UPDATE_PLUS, name, money, name);
    }
 
}


파일명: AccountDAOImpl.java


[첨부(Attachments)]

AccountDAOImpl.zip


AccountDAO.zip



9. AccountRowMapper.java (com.website.example.dao)


package com.website.example.dao;


import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.website.example.vo.AccountVO;


public class AccountRowMapper implements RowMapper<AccountVO>  {


       @Override
        public AccountVO mapRow(ResultSet rs, int rowNum) throws SQLException {
  
                 AccountVO vo = new AccountVO();

                 vo.setIdx(rs.getInt(1));
                 vo.setName(rs.getString(2));
                 vo.setBalance(rs.getInt(3));
                 vo.setRegidate(rs.getTimestamp(4));
  
                 return vo;
        }

}


파일명: AccountRowMapper.java


[첨부(Attachments)]

AccountRowMapper.zip




10. AccountService.java (com.website.example.service)


package com.website.example.service;

import java.sql.SQLException;

import com.website.example.vo.AccountVO;


public interface AccountService {

       void accountCreate(AccountVO vo) throws SQLException;
       void accountTransfer(String sender, String receiver, int money) throws SQLException;
 
}


파일명: AccountService.java


[첨부(Attachments)]

AccountService.zip




11. AccountServiceImpl.java (com.website.example.service)


package com.website.example.service;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import com.website.example.dao.AccountDAO;
import com.website.example.dao.AccountDAOImpl;
import com.website.example.vo.AccountVO;



public class AccountServiceImpl implements AccountService{

      private AccountDAO accountDAO;
      private DataSource ds = null;
 
      public AccountServiceImpl(DataSource ds) {
            this.accountDAO = new AccountDAOImpl(ds);
            this.ds = ds;
      }


      @Override
      public void accountCreate(AccountVO vo) throws SQLException {
            this.accountDAO.createAccount(vo);  
      }
 

      @Override
      public void accountTransfer(String sender, String receiver, int money) throws SQLException {
  
               int balance = accountDAO.getBalance(sender); // 보내는 사람 잔액 체크
               Connection conn = null;
     
               if(balance >= money){ // 보내는 돈이 잔액보다 많으면

                    TransactionSynchronizationManager.initSynchronization(); // 트랜잭션 동기화 작업 초기화

                    conn = ds.getConnection();
                    conn.setAutoCommit(false);
         
                    try {
                       accountDAO.minus(sender, money);
                       accountDAO.plus(receiver, money);
          
                       conn.commit(); // 성공
    
                   }catch(SQLException e) {
                        conn.rollback(); // 실패
                   }
                   finally {
                        // 커넥션 종료(Spring)
                       DataSourceUtils.releaseConnection(conn, this.ds);
          
                       // 동기화 작업을 종료하고 저장소를 비운다
                       TransactionSynchronizationManager.clearSynchronization();
                   }
           
             } else{

                   System.out.println("돈 없음");
                  //throw new NoMoneyException();
             }
  
       }


}


파일명: AccountServiceImpl.java


[첨부(Attachments)]

AccountServiceImpl.zip



12. MainTest.java (com.website.example.unit)


package com.website.example.unit;

import java.sql.SQLException;
import java.sql.Timestamp;

import javax.sql.DataSource;

import org.junit.jupiter.api.Test;

import com.website.example.common.MyDataSourceFactory;
import com.website.example.service.AccountService;
import com.website.example.service.AccountServiceImpl;
import com.website.example.vo.AccountVO;


class MainTest {


     @Test
     void test() throws SQLException {

            MyDataSourceFactory sourceFactory = new MyDataSourceFactory();
            DataSource ds = sourceFactory.getOracleDataSource();
  
            AccountService service = new AccountServiceImpl(ds);
            AccountVO vo = new AccountVO();
   
            /*
            // 1. 계정 생성
           vo.setName("홍길동");
           vo.setBalance(10000);
           vo.setRegidate(Timestamp.valueOf("2020-01-20 10:05:20"));
           service.accountCreate(vo);
  
           // 2. 계정 생성
           vo.setName("홍길자");
           vo.setBalance(0);
           vo.setRegidate(Timestamp.valueOf("2020-01-20 22:05:20"));
           service.accountCreate(vo);
            */
  
           // 3. 거래 처리
           service.accountTransfer("홍길동", "홍길자", 500);
  
  
      }

}


파일명: MainTest.java


[첨부(Attachments)]

MainTest.zip

AccountServiceImpl.zip




* 맺음글(Conclusion)


스프링 JDBC에서 트랜젝션 사용하는 방법에 대해서 어노테이션을 적용하지 않는 방법에 대해서 살펴보았다.


1. [Spring-Framework] 36. Spring-JDBCTemplate - 트랜젝션 (어노테이션, Java 설정), 2020-10-10

- https://yyman.tistory.com/1461

(이해를 돕기 위하여 실제 개발환경도구에서 트랜젝션 시연 영상도 포함해서 소개하였다.)


반응형
728x90
300x250

[JSP] 27. Java 스타일의 트랜젝션 구현하기(은행 계좌)


트랜젝션 처리에 대해서 소개하려고 한다.

이 주제는 사실 몇 가지 키워드가 있다.


ACID(원자성, 일관성, 고립성, 지속성)을 만족해야 한다.


기본적인 DML만 조작하다가 갑자기 접근하게 되면, 이 내용은 조금 난해할 수도 있다.


1. 자료처리에 있어서 4가지를 모두 충족해야 한다.


원자성(Atomicity)
트랜젝션은 분해가 불가능한 최소의 단위인 하나의 원자처럼 동작한다는 의미. 

트랜젝션 내의 모든 연산들은 반드시 한꺼번에 완전하게 전체가 정상적으로 수행이 완료되거나 아니면 연산자체를 수행하지 않음.


일관성(Consistency)
트랜잭션 작업이 시작되지 전에 데이터베이스 상태가 일관된 상태였다면 트랜잭션 작업이 종료된 후에도 일관성 있는 데이터 베이스 상태를 유지해아한다.

예를 들어서 게시판에 글을 쓰는데 제목의 글자 제한이 255자라고 하자.

트랜잭션이 일어나면 이러한 조건을 만족해야하는 것이다. 만약 이를 위반하는 트랜잭션이 있다면 롤백 처리해야 한다.

(문제가 있는 작업이라면, 입력자체를 시키면 안 되는 것이다.)


고립성(Isolation)
트랜잭션 작업 수행 중에는 다른 트랜잭션에 영향을 주어서도 안 되고, 다른 트랜잭션들에 의해 간섭을 받아서도 안 된다는 것을 의미. 


다른 트랜잭션의 영향을 받게 되면 영향을 주는 트랜잭션에 의해 자신의 동작이 달라 질 수 있기 때문이다.

트랜젝션 자신은 고립된 상태에서 수행되어야 한다는 것을 의미. 즉 다수의 트랜잭션이 동시에 수행중인 상황에서 하나의 트랜잭션이 완료될 때까지는 현재 실행 중인 트랜잭션의 중간 수행결과를 다른 트랜잭션에서 보거나 참조 할 수 없다.


지속성(Durablility)
일련의 데이터 조작(트렌젝션 조작)을 완료 하고 완료 통지를 사용자가 받는 시점에서 그 조작이 영구적이 되어 그 결과를 잃지 않는 것을 나타낸다. 시스템이 정상일 때 뿐 아니라 데이터베이스나 OS의 이상 종료, 즉 시스템 장애도 견딜 수 있다는 것을 말한다.




2. "은행 계좌"라는 주제


은행 계좌 구현의 문제를 놓고 보면, 돈을 이체시켰는데 시스템 장애로 돈이 빠져나가고 이체가 되지 않아버리면 입금자는 돈을 잃어버리게 된다.

물론 트랜젝션만 가지고 은행 시스템이 구축된 것은 아닐 것이다.


이런 경우를 컴퓨터 프로그래밍으로 표현해보려고 한다.




3. JSP/Servlet으로 구현해도 되고, JUnit 태스트 도구로 접근해도 상관없다.


이 프로젝트를 자습하는 데 있어서, 자유롭게 원하는 형식에서 태스트해도 무방하다.

자바 기반이면, 스프링프레임워크에서도 가능하고, Dynamic Web Project를 생성해도 무방하고 Java Project로 따라해도 괜찮다는 이야기를 하고 소개하겠다.


[태스트 환경]

IDE: Eclipse 2020-06

DB: Oracle 11g XE(Express Edition)
- Maven Projects(Spring 포함)

- JUnit 5



4. 데이터베이스 설계


-- Transaction 실습 DB (은행 - Account)
-- Oracle 11 - 자동번호 생성 테이블 정의
-- Table 생성 (FOODMENU_TBL)
-- NEW.ID (Table의 id를 가리킴)

CREATE TABLE account_tbl
(
    idx NUMBER PRIMARY KEY,
    name VARCHAR2(30),
    balance NUMBER,
    regidate TIMESTAMP
);


-- Sequence 정의
CREATE SEQUENCE account_sequence
START WITH 1
INCREMENT BY 1;


-- Trigger 생성
-- BEFORE INSERT on '테이블명'
CREATE OR REPLACE TRIGGER account_trigger
BEFORE INSERT
    ON account_tbl
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT account_sequence.nextval INTO :NEW.IDX FROM dual;
END;


트리거 작성과 스퀀스 번호 생성에 대해서도 적혀져 있으니깐, 잘 사용해도 무방하다.


파일명: account_tbl.sql


[첨부(Attachments)]

AccountTbl.zip




5. 프로젝트 구성도


아래 그림은 프로젝트 구성도이다.

HomeController.java 등은 생략해도 된다.



그림 1, 그림 2. 프로젝트 구성도



6. AccountVO.java(com.website.example.vo)


package com.website.example.vo;

import java.sql.Timestamp;

public class AccountVO {

 private int idx;
 private String name;
 private int balance;
 private Timestamp regidate;
 
 public int getIdx() {
      return idx;
 }
 
 public void setIdx(int idx) {
     this.idx = idx;
 }
 
 public String getName() {
     return name;
 }
 
 public void setName(String name) {
     this.name = name;
 }
 
 public int getBalance() {
     return balance;
 }

 public void setBalance(int balance) {
     this.balance = balance;
 }

 public Timestamp getRegidate() {
     return regidate;
 }
 
 public void setRegidate(Timestamp regidate) {
     this.regidate = regidate;
 }
 
}


파일명: AccountVO.java


[첨부(Attachments)]

AccountVO.zip




6-1. db.properties (/src/main/resources)


MYSQL_DB_URL=jdbc:mysql://localhost:3306/dbanme?useUnicode=true&characterEncoding=utf8
MYSQL_DB_USERNAME=
MYSQL_DB_PASSWORD=
ORACLE_DB_URL=jdbc:oracle:thin:@127.0.0.1:1521:xe
ORACLE_DB_USERNAME=HR
ORACLE_DB_PASSWORD=1234


파일명: db.properties


[첨부(Attachments)]

db.zip




7. JDBCUtil.java(com.website.example.common)


이 프로젝트에서는 close()만 사용하였다. 예비 태스트 용도로 자바 표준의 SQL 방식도 두고 있다.


package com.website.example.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.sql.DataSource;


public class JDBCUtil {
 
      private static final String driverName = "oracle.jdbc.driver.OracleDriver";
      private static final String jdbcUrl = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
      private static final String userId = "{사용자 계정명}";
      private static final String userPwd = "{비밀번호}";
 
 public static Connection getConnection() {


  try {
   
       Class.forName(driverName); 
        return DriverManager.getConnection(jdbcUrl, userId, userPwd);
   
  } catch (Exception e) {
        e.printStackTrace();
  }
  
  return null;
  
 }


 public static void close(PreparedStatement stmt, Connection conn) {
  
  if (stmt != null) {
   
   try {
    
    if (!stmt.isClosed())
     
       stmt.close();
    
   } catch (Exception e) {
    
        e.printStackTrace();
    
   } finally {
    
        stmt = null;
    
   }
   
  } // end of if
  
  if (conn != null) {
   
   try {
    
    if (!conn.isClosed())
         conn.close();
    
   } catch (Exception e) {
    
    e.printStackTrace();
    
   } finally {
    
    conn = null;
    
   }
   
  } // end of if
  
 }

 public static void close(ResultSet rs, PreparedStatement stmt, Connection conn) {
  
  if (rs != null) {
   
   try {
    
    if (!rs.isClosed())
     rs.close();
    
   } catch (Exception e) {
    
    e.printStackTrace();
    
   } finally {
    
    rs = null;
    
   }
   
  } // end of if
  
  if (stmt != null) {
   
   try {
    
    if (!stmt.isClosed())
     stmt.close();
    
   } catch (Exception e) {
    
    e.printStackTrace();
    
   } finally {
    stmt = null;
   }
   
  } // end of if
  
  if (conn != null) {
   
   try {
    
    if (!conn.isClosed())
     conn.close();
    
   } catch (Exception e) {
    
    e.printStackTrace();
    
   } finally {
    
    conn = null;
    
   }
   
  } // end of if
    
 }

}



파일명: JDBCUtil.java


[첨부(Attachments)]

JDBCUtil.zip

AccountVO.zip




8. MyDataSourceFactory.java(com.website.example.common)


DataSourceFactory이다.



package com.website.example.common;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.mysql.cj.jdbc.MysqlDataSource;

import oracle.jdbc.pool.OracleDataSource;

public class MyDataSourceFactory {
 
 private InputStream is = null;
 
 public MyDataSourceFactory()  {
  
        String resource = "db.properties";
        is = getClass().getClassLoader().getResourceAsStream(resource);
 }
 
 public DataSource getMySQLDataSource() {
  
        Properties props = new Properties();
       
        MysqlDataSource mysqlDS = null;
       
        try {
         
            props.load(is);
            mysqlDS = new MysqlDataSource();
            mysqlDS.setURL(props.getProperty("MYSQL_DB_URL"));
            mysqlDS.setUser(props.getProperty("MYSQL_DB_USERNAME"));
            mysqlDS.setPassword(props.getProperty("MYSQL_DB_PASSWORD"));
           
        } catch (IOException e) {
            e.printStackTrace();
        }
       
        return mysqlDS;
       
    }
    
    public DataSource getOracleDataSource(){
     
        Properties props = new Properties();
        OracleDataSource oracleDS = null;
       
        try {
         
            props.load(is);
            oracleDS = new OracleDataSource();
            oracleDS.setURL(props.getProperty("ORACLE_DB_URL"));
            oracleDS.setUser(props.getProperty("ORACLE_DB_USERNAME"));
            oracleDS.setPassword(props.getProperty("ORACLE_DB_PASSWORD"));
           
        } catch (IOException e) {
         
            e.printStackTrace();
           
        } catch (SQLException e) {
         
            e.printStackTrace();
           
        }
       
        return oracleDS;
       
    }

}


파일명: MyDataSourceFactory.java


[첨부(Attachments)]

MyDataSourceFactory.zip


JDBCUtil.zip

AccountVO.zip



9. AccountDAO.java (com.website.example.dao)


AccountDAO에 대한 명세이다. DAO는 Data Access Object의 약자이다.


package com.website.example.dao;

import java.sql.SQLException;

import com.website.example.vo.AccountVO;

public interface AccountDAO {

 // 계좌 생성
 public void create(AccountVO vo) throws SQLException;
 
 // 잔액 조회
 public int getBalance(String name) throws SQLException;
 
 // 플러스 계좌
 public void plus(String name, int money) throws SQLException;
 
 // 마이너스 계좌
 public void minus(String name, int money) throws SQLException;
 
 // 거래
 public void transfer(String sender, String receiver, int money) throws SQLException;
 
}


파일명: AccountDAO.java


[첨부(Attachments)]

AccountDAO.zip


JDBCUtil.zip

AccountVO.zip



10. AccountDAOImpl.java (com.website.example.dao)


AccountDAO에 대한 명세이다. DAO는 Data Access Object의 약자이다.


package com.website.example.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.website.example.common.JDBCUtil;
import com.website.example.vo.AccountVO;

// Transaction - 자바
public class AccountDAOImpl implements AccountDAO {


 private final String CREATE_ACCOUNT = "insert into account_tbl" +
        "(name, balance, regidate) values(?, ?, ?)";
 
 private final String SELECT_BALANCE = "select * from account_tbl where name = ?";
 
 private final String UPDATE_MINUS = "update account_tbl set balance = (select balance from account_tbl where name = ?) - ? " +
          "where name = ?";
 
 private final String UPDATE_PLUS = "update account_tbl set balance = (select balance from account_tbl where name = ?) + ? " +
            "where name = ?";
 
 private DataSource ds = null;
 
 public AccountDAOImpl(DataSource ds) {
       this.ds = ds;
 }
 
 // 단일 쿼리에서의 트랜젝션 방법
 @Override
 public void create(AccountVO vo) throws SQLException {


       Connection conn = ds.getConnection();
       PreparedStatement pstmt = null;
  
  try {
   conn.setAutoCommit(false); // 트랜젝션 시작
   
   pstmt = conn.prepareStatement(CREATE_ACCOUNT);
   pstmt.setString(1, vo.getName());
   pstmt.setInt(2, vo.getBalance());
   pstmt.setTimestamp(3, vo.getRegidate());
   
   pstmt.executeUpdate();
   
   conn.commit();
   
  }catch(SQLException e) {
   
   conn.rollback();
   e.getMessage();
   
  }finally {
   JDBCUtil.close(pstmt, conn);
  }
  
 }

 @Override
 public int getBalance(String name) throws SQLException {
  
  Connection conn = ds.getConnection();
  PreparedStatement pstmt = null;
  ResultSet rs = null;
  
  int result = 0;
  
  try {
        conn.setAutoCommit(false); // 트랜젝션 시작
        pstmt = conn.prepareStatement(SELECT_BALANCE);
   
   pstmt.setString(1, name);
   
   rs = pstmt.executeQuery();
   
   if ( rs.next() ) {
         result = rs.getInt(3);
   }
   
   conn.commit();
   
  }catch(SQLException e) {
   
   conn.rollback();
   e.getMessage();
   
  }finally {
   JDBCUtil.close(pstmt, conn);
  }
  
  return result;
 }

 @Override
 public void plus(String name, int money) throws SQLException {


      Connection conn = ds.getConnection();
      PreparedStatement pstmt = null;
  
  try {
   
   conn.setAutoCommit(false);
   
   // plus, minus 다 확인 후에 commit처리 해야 함.
   // 그래서 지금 바로 트랜젝션을 구현하면 안 됨
   pstmt = conn.prepareStatement(UPDATE_PLUS);
   pstmt.setString(1, name);
   pstmt.setInt(2, money);
   pstmt.setString(3, name);
   
   pstmt.executeUpdate();
      System.out.println(money);

   conn.commit();
   
  }catch(SQLException e) {
   System.out.println(e.getMessage());
   conn.rollback();
   
  }finally {
   JDBCUtil.close(pstmt, conn);
  }
  
 }

 @Override
 public void minus(String name, int money) throws SQLException {
  

       Connection conn = ds.getConnection();
       PreparedStatement pstmt = null;
  
  try {
   
   conn.setAutoCommit(false);

      // 예외 발생시키기(트랜젝션 의도적 발생)
      if(true){
             throw new SQLException(); // 의도적 예외 발생
       }
        
   // plus, minus 다 확인 후에 commit처리 해야 함.
   // 그래서 지금 바로 트랜젝션을 구현하면 안 됨
   pstmt = conn.prepareStatement(UPDATE_MINUS);
   pstmt.setString(1, name);
   pstmt.setInt(2, money);
   pstmt.setString(3, name);
   
   pstmt.executeUpdate();
   
   conn.commit();
   
  }catch(SQLException e) {
   
        conn.rollback();
        System.out.println(e.getMessage());
    
  }finally {
        JDBCUtil.close(pstmt, conn);
  }
  
  
 }

 @Override
 public void transfer(String sender, String receiver, int money) throws SQLException {

  Connection conn = ds.getConnection();
  PreparedStatement pstmt = null;
  
  try {
   
   conn.setAutoCommit(false);

   /*
      // 예외 발생시키기(트랜젝션 의도적 발생)
      if(true){
       throw new SQLException(); // 의도적 예외 발생
         }
         */
        
   // plus, minus 다 확인 후에 commit처리 해야 함.
   // 그래서 지금 바로 트랜젝션을 구현하면 안 됨
   pstmt = conn.prepareStatement(UPDATE_MINUS);
   
   // 주는 분
   pstmt.setString(1, sender);
   pstmt.setInt(2, money);
   pstmt.setString(3, sender);
   
   pstmt.executeUpdate();
   
   // 받는 분
   pstmt = conn.prepareStatement(UPDATE_PLUS);
   pstmt.setString(1, receiver);
   pstmt.setInt(2, money);
   pstmt.setString(3, receiver);
   
   pstmt.executeUpdate();
   
        conn.commit();
   
  }catch(SQLException e) {
   
        conn.rollback();
        System.out.println(e.getMessage());
   
  }finally {
       JDBCUtil.close(pstmt, conn);
  }
  
 }

}


파일명: AccountDAOImpl.java


[첨부(Attachments)]

AccountDAOImpl.zip



JDBCUtil.zip

AccountVO.zip



11. AccountService.java (com.website.example.service)


AccountService에 대한 명세이다.

서비스에 대한 정의이다. DB를 정의하는 영역은 아니다.

하지만, Spring Framework로 구현할 때, Spring JDBC 등을 적용하여 트랜젝션 동기화 기능을 구현부에 구현하면, Connection을 사용할 수도 있다.

질의문을 정의하는 영역은 아니다.


package com.website.example.service;

import java.sql.SQLException;

import com.website.example.vo.AccountVO;


public interface AccountService {
 
     public void accountCreate(AccountVO vo) throws SQLException;
     public void accountTransfer(String sender, String receiver, int money) throws SQLException;
 
}


파일명: AccountService.java


[첨부(Attachments)]

AccountService.zip



12. AccountServiceImpl.java (com.website.example.service)


AccountServiceImpl.java는 AccountService의 구현부이다.

인터페이스의 역할을 정의해보면, 하나의 통로와 같은 역할을 한다고 볼 수 있다.


package com.website.example.service;

import java.sql.SQLException;

import javax.sql.DataSource;

import com.website.example.dao.AccountDAO;
import com.website.example.dao.AccountDAOImpl;
import com.website.example.vo.AccountVO;

public class AccountServiceImpl implements AccountService {


     private AccountDAO accountDAO;
     private DataSource ds = null;
 
 public AccountServiceImpl(DataSource ds) {
  
        accountDAO = new AccountDAOImpl(ds);
        this.ds = ds;
 }

 @Override
 public void accountCreate(AccountVO vo) throws SQLException {
  
        accountDAO.create(vo);
  
 }
 
    public void accountTransfer(String sender, String receiver, int money) throws SQLException{
       
     int balance = accountDAO.getBalance(sender); // 보내는 사람 잔액 체크
     
        if(balance >= money){ // 보내는 돈이 잔액보다 많으면
 
                   accountDAO.transfer(sender, receiver, money);
         
        } else{
                   System.out.println("돈 없음");
                   //throw new NoMoneyException();
        }
       
    }

 
}


파일명: AccountServiceImpl.java


[첨부(Attachments)]

AccountServiceImpl.zip




13. TestMain.java (com.website.example.unit)


JUnit의 태스트 영역이다.


package com.website.example.unit;

import static org.junit.jupiter.api.Assertions.*;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.junit.jupiter.api.Test;

import com.website.example.common.MyDataSourceFactory;
import com.website.example.service.AccountService;
import com.website.example.service.AccountServiceImpl;
import com.website.example.vo.AccountVO;

class TestMain {


       @Test
       void test() throws SQLException {
   
                 DataSource ds = new MyDataSourceFactory().getOracleDataSource();
                 AccountService service = new AccountServiceImpl(ds);
  
  // 1. 계정 생성 (계정이 없는 경우라면, 주석 풀고 작업해보면 됨.
  /*
  AccountVO vo = new AccountVO();
  vo.setName("홍길동");
  vo.setBalance(10000);
  vo.setRegidate(Timestamp.valueOf("2020-10-01 10:30:00"));
  
  service.accountCreate(vo);

  vo.setName("홍길자");
  vo.setBalance(0);
  vo.setRegidate(Timestamp.valueOf("2020-10-04 23:20:00"));

  service.accountCreate(vo);
  */
  
  // 2. 금전 거래
  service.accountTransfer("홍길동", "홍길자", 500);
  
 }

}


파일명: TestMain.java


[첨부(Attachments)]

TestMain.zip




14. 태스트 해보기 - 결과


코드로 구현하여 태스트를 해봐도 좋을 듯 싶다.



그림 3. 태스트 하기



그림 4. 태스트 하기


트랜젝션을 한번 해보면, 무슨 일이 일어나는지 구경을 조금해보면 좋을 듯 싶다.




15. 맺음글(Conclusion)


setAutoCommit(), conn.commit(), conn.rollback() 사소해보이는 이 코드가 반응하는 것을 관찰해보면, 트랜젝션이 중요하다는 사실을 알 수 있다.



* 참고자료(References)


1. JDBC 트랜젝션 동기화, https://joont.tistory.com/158, Accessed by 2020-10-09, Last Modified 2016-07-11.

-> 추천(50점): 정말 잘 설명해주고 있다.

2. Data Access - Transaction, https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/data-access.html#transaction, Accessed by 2020-10-09, Last Modified 2020-09-15.


3. 

반응형
728x90
300x250
[Spring Framework] 33. AOP(Aspect-Oriented-Programming) - XML 방식


XML 기반으로 작성된 AOP 구현 방법에 대해서 소개하려고 한다.

AOP의 관점에 대한 5가지 방식을 모두 적용해보았다.


[태스트 환경]

* IDE: Eclipse 2020-06

* Spring Framework 4.2.4.RELEASE
* Aspectjweaver 1.6.10
* Spring-aop 4.2.4.RELEASE
* JUnit 5




1. 프로젝트 구성도


프로젝트 구성도이다.



그림 1, 그림 2. 프로젝트 구성도




2. 프로젝트 생성


Spring Legacy Project로 프로젝트를 생성한다.

참고로 Spring MVC Project로 선택하고 생성해야 한다.



3. Build Path, Java Compiler, Project Factes 버전 맞춰주기


* Build Path: JRE 버전을 1.8로 변경해준다.
               - Add Library로 JUnit 5를 등록해준다.

* Java Compiler: Compiler compliance level - 1.8로 변경해준다.

* Project Factes: Java 버전을 1.8로 변경해준다.




4. POM.xml 설정하기


(중략)
 <properties>
  <java-version>1.8</java-version>
  <org.springframework-version>4.2.4.RELEASE</org.springframework-version>
  <org.aspectj-version>1.6.10</org.aspectj-version>
  <org.slf4j-version>1.6.6</org.slf4j-version>
 </properties>

~~~

(중략)
    <!-- AspectJ -->
    <dependency>
     <groupId>org.aspectj</groupId>
     <artifactId>aspectjrt</artifactId>
     <version>${org.aspectj-version}</version>
    </dependency>
   
    <!-- AspectJWeaver -->
    <dependency>
     <groupId>org.aspectj</groupId>
     <artifactId>aspectjweaver</artifactId>
     <version>${org.aspectj-version}</version>
    </dependency>
   
    <!-- Spring AOP 추가(Java) --> 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${org.springframework-version}</version>
    </dependency> 
(중략)






5. applicationContext.xml (src/main/resources/applicationContext.xml)


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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 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.2.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
 
 <!-- 방법1 - JAVA -->
 <!-- JAVA 방식(어노테이션)의 AOP - AspectJ Weaver -->
 <!--  <aop:aspectj-autoproxy></aop:aspectj-autoproxy>  어노테이션 작업시 필수 선언해야 함. -->
 
 <!--  XML 방식의 AOP - AsepectJ Weaver -->
 <bean id="resultAOP" class="com.website.example.test.ResultAOP"></bean>
 <bean id="txAdviceXML" class="com.website.example.aop.LogAdvisorXML"></bean>

 <aop:config>
  <!-- txAdviceXML 하나 영역임 -->
  <aop:aspect ref='txAdviceXML'>

   <!-- 진입영역 -->
   <!-- 1개만 가능함 -->
   <!-- <aop:pointcut id="point1" expression="execution(* com.website.example.test..*())"/> -->
   <aop:pointcut id="point1" expression="execution(* com.website.example.test.ResultAOP..*())"/>           
          
          <!-- before -->
          <aop:before method="beforeAdvice" pointcut-ref="point1"/>
          <!-- after -->
          <aop:after method="afterAdvice" pointcut-ref="point1"/>
          
          <!-- around(메서드 자체를 가로채기) -->
          <aop:around method="aroundAdvice" pointcut-ref="point1"/>
          
          <!-- afterThrowing -->
          <aop:after-throwing method="afterThrowing" pointcut-ref="point1"/>
          
          <!-- afterReturning -->
          <aop:after-returning method="afterReturning" pointcut-ref="point1"/>
          
        </aop:aspect>
       
        <!-- 2번째 선언자 -->
 </aop:config>
 
</beans>


파일명: applicationContext.xml


[첨부(Attachments)]

applicationContext.zip



6. ResultAOP.java - com.website.example.text


핵심 로직에 해당되는 부분이다. 이 부분을 비즈니스 로직이라고 표현하기도 한다.


package com.website.example.test;


public class ResultAOP {


     public void method1() {
           System.out.println("[중간]:");
  
           // afterThrowing 유발 코드
           // int d = 2/0;
  
           System.out.println("결과: 메서드");
     }
 
}


파일명: ResultAOP.java


[첨부(Attachments)]

ResultAOP.zip


applicationContext.zip



7. LogAdvisor.java - com.website.example.aop


인터페이스 정의이다.

package com.website.example.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public interface LogAdvisor {

        public void beforeAdvice();
        public void afterAdvice();
        public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable;
        public void afterThrowing();
        public void afterReturning();
 
}


파일명: LogAdvisor.java


[첨부(Attachments)]

LogAdvisor.zip




8. LogAdvisorXML.java - com.website.example.aop


package com.website.example.aop;

import org.aspectj.lang.ProceedingJoinPoint;


public class LogAdvisorXML implements LogAdvisor{
  
       // around advice
       @Override
       public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable{
  
               System.out.println("2단계A?:");
               System.out.println("aroundMethod 호출 1");
  
               // 원래의 메소드를 호출한다.
               Object obj = pjp.proceed();

               System.out.println("2단계B?:");
               System.out.println("aroundMethod 호출 2");
  
               return obj;
       }


       // before advice
       @Override
       public void beforeAdvice() {
              System.out.println("1단계:");
              System.out.println("beforeMethod 호출");
  
       }


       // after
       @Override
       public void afterAdvice() {
              System.out.println("5단계:");
              System.out.println("afterMethod 호출");
  
       }


       // afterThrowing
      @Override
      public void afterThrowing() {
              System.out.println("4단계:");
              System.out.println("afterThrowing 호출");
      }

      // afterReturning
      @Override
      public void afterReturning() {
              System.out.println("3단계:");
              System.out.println("afterReturning 호출");
  
      }
 
}


파일명: LogAdvisorXML.java


[첨부(Attachments)]

LogAdvisorXML.zip


LogAdvisor.zip




9. TestMain.java - com.website.example.unit


package com.website.example.unit;


import org.junit.jupiter.api.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

import com.website.example.test.ResultAOP;


public class TestMain {

      
       // 사용방법1
       @Test
       public void sample() {

                AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml");
   
                ResultAOP rAOP = (ResultAOP) factory.getBean("resultAOP");

                rAOP.method1();
  
                factory.close();
       }
 

       // 사용방법2
       @Test
       public void sample2() {

               ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
               ResultAOP rAOP = ctx.getBean("resultAOP", ResultAOP.class);
               //rAOP.method1();
  
               ctx.close();
       }
 
}


파일명: TestMain.java


[첨부(Attachments)]

TestMain.zip




10. 동작 결과


구현한 코드의 동작 결과이다.



그림 3. 출력 결과 1



그림 4. 출력 결과 2




11. 맺음글(Conclusion)


직관적으로 AOP를 아주 쉽게 이해할 수 있도록 작성하였다.

반응형
728x90
300x250
[Spring Framework] 32. AOP(Aspect-Oriented-Programming) - Java 방식2(Context.xml)


Spring Framework에서 사용되는 AOP 구현방식을 소개하려고 한다.

AOP를 다루고자 하는 사람들은 조금 방법이 다양하게 있으니, 원하는 방식 하나 택해서 사용하면 될듯 싶다.


[배경 입문 지식]

1. [Spring-Framework] 30. AOP(Aspect-Oriented-Programming) 관점지향 프로그래밍 (Java셋팅) (1), 2020-10-04

- https://yyman.tistory.com/1447

2. [Spring-Framework] 30. AOP(Aspect-Oriented-Programming) 관점지향 프로그래밍 (Java셋팅) (2), 2020-10-04

- https://yyman.tistory.com/1448


셋팅 방법에서 다르다.

동일하게 생각하고 작성하면, 오류를 경험할 수 있다.


AOP를 다루는 건 큰 틀에서는 같은 일인데 불구하고, Service 어노테이션 정의부터 다소 셋팅이 달라져버릴 수가 있어서 신중하게 사용하는 것을 권장한다. 셋팅을 먼저 한 경우라면, 거기에 맞춰서 흐름을 타주는 방식으로 가야 하겠다.


XML, 순수한 Java 방식, Context.xml 및 Java 방식(어노테이션) 크게 3가지 방법으로 AOP를 설정할 수 있다.

이론적으로는 같으나 실질적인 구현에서는 호환이 안 되니 주의하여 사용할 것을 권한다.


3가지 방법 중 하나를 택해서 AOP 대상을 두었으면 그 방법을 따라가는 형태로 진행하는 것을 권장한다.

(구현 관점에서보면, 표준화가 진행이 덜된 느낌이 있다.) 시간이 나면, 소개해주겠다.


[태스트 환경]

* IDE: Eclipse 2020-06 (Spring Tools 3 Addon.....)
* Spring Framework Version: 4.2.4.RELEASE

* JUnit 5(태스트 실행)




1. 프로젝트 생성


-> Spring Legacy Project -> Spring MVC Project로 진행하였음.


프로젝트 생성 및 셋팅 방법에 대해서는 "이전 글들"을 참고하면 된다. (동일하게 진행함.)




2. pom.xml


매우 중요한 부분이다. 노란색 불은 들어오는데 빌드하면 오류가 생긴 경우도 있을 수도 있다.

셋팅을 잘해야 한다.


-> Spring Tool-Suites 4.7로 작업하면 14버전을 사용해도 무방하다. 다만, 이전 버전의 Eclipse 등에서 프로젝트 구성에서 오류가 생길 수도 있다.

지금 pom 셋팅은 공통적인 작업이다.


(중략)

 <properties>
  <java-version>1.8</java-version>
  <org.springframework-version>4.2.4.RELEASE</org.springframework-version>
  <org.aspectj-version>1.6.10</org.aspectj-version>
  <org.slf4j-version>1.6.6</org.slf4j-version>
 </properties>

(중략)
    
  <!-- AspectJ -->
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjrt</artifactId>
   <version>${org.aspectj-version}</version>
  </dependency> 
  
  <!-- AspectJWeaver -->
  <dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.8.8</version>
  </dependency>
  
  <!-- Spring AOP 추가(Java) -->  
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${org.springframework-version}</version>
  </dependency>

(중략)




3. 미리 살펴보는 프로젝트 구성도


이번 작업에서는 JUnit5로 TDD 방식으로 구동한 프로젝트이다.



그림 1. 프로젝트 구성도

작업할 프로젝트 구성이다.




4. 프로젝트 환경설정(Build Path, Java Compiler, Project Factes)


Build Path의 Libraries에 들어가서 버전을 1.8로 설정한다.

JUnit 5도 추가해준다.


참고로 JUnit 5은 자바 1.8버전에서부터 지원한다.



그림 2. 프로젝트 구성도


Build Path를 설정해준다. "Add Library"를 클릭해서 JUnit 5을 추가할 수 있다.




그림 3. 자바 컴파일러 환경설정


JDK를 잘 설치하였음에도 1.8버전 등으로 변경하였을 때 이클립스에서 오류로 간주해버리는 경우가 있다.

오류가 생기는 이유는 Project Factes 옵션을 조작할 때 발생한다.


JDK Compliance의 Compiler compliance level 버전을 맞춰주면 오류가 사라지는 것을 확인할 수 있다.



그림 4. Project Factes


Java의 버전을 1.8으로 변경한다.




5. applicationJavaAOP.xml (src/main/resources/applicationJavaAOP.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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 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.2.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
 
 <aop:aspectj-autoproxy />
 <context:component-scan base-package="com.website.example" />
 <bean id="loggingAspect" class="com.website.example.aop.LogAdvisorJava" />
 
</beans>



AOP 스키마 정의는 servlet-context.xml에서 간단한 조작으로 추가하였다.


그림 5. aop 환경설정해주기 - 네임스페이스 정의


양식 틀은 /src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml에서 복사 붙여넣기하였다.


작성한 코드는 <aop>.....</bean> 이 정도이다.


* 파일명: applicationJavaAOP.xml


[첨부(Attachments)]

applicationJavaAOP.zip




6. ResultAOPJava.java (com.website.example.text)


package com.website.example.test;

import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;


@Service
public class ResultAOPJava {
 
     @Pointcut("execution(* com.website.example.test.*(..))")
      public void method1() {
              System.out.println("결과: 메서드");
      }
 
}


* 파일명: ResultAOPJava.java


[첨부(Attachments)]

ResultAOPJava.zip




7. LogAdvisor.java (com.website.example.aop)


관점에 대한 인터페이스이다.
객체의 다형성을 생각하면 좋을 것으로 보인다.


package com.website.example.aop;

import org.aspectj.lang.annotation.Aspect;


@Aspect
public interface LogAdvisor {

       public void beforeAdvice();
       public void afterAdvice();
       public void aroundAdvice();
 
}


* 파일명: LogAdvisor.java


[첨부(Attachments)]

LogAdvisor.zip

ResultAOPJava.zip




8. LogAdvisorJava.java (com.website.example.aop)


클래스명을 Impl로 해도 되었을 법한데 태스트용이니 생략하도록 하자.


package com.website.example.aop;


import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Service;


// 어노테이션 방식
@Aspect
public class LogAdvisorJava implements LogAdvisor{


         @Before("execution(* com.website.example.test.ResultAOPJava.*(..))")
          public void beforeAdvice() {
                 System.out.println("전 단계");
         }

         @After("execution(* com.website.example.test.ResultAOPJava.*(..))")
         public void afterAdvice() {
                 System.out.println("후 단계");
         }


         public void aroundAdvice() {
   
          }
 
}


* 파일명: LogAdvisorJava.java


[첨부(Attachments)]

LogAdvisorJava.zip


LogAdvisor.zip

ResultAOPJava.zip



9. TestMain.java (com.website.example.test)


JUnit 태스트 도구로 실행 영역을 구현하였다.


package com.website.example.test;

import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class TestMain {


     @Test
     public void test() {


              // 방법 1
             @SuppressWarnings("resource")
             AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationJavaAOP.xml");
  
             ResultAOPJava cal = factory.getBean(ResultAOPJava.class);
             cal.method1();
  
     }
 
}


* 파일명: TestMain.java


[첨부(Attachments)]

TestMain.zip



10. 출력 결과


아래의 그림처럼 AOP가 잘 출력되는 것을 확인할 수 있다.



그림 6. AOP 화면 출력 결과


정상적으로 AOP 환경설정이 이뤄진 것을 확인할 수 있다.



* 참고자료(References)


1. Spring AOP – AspectJ Annotation Config Example - HowToDoInJava, https://howtodoinjava.com/spring-aop/spring-aop-aspectj-example-tutorial-using-annotation-config/, Accessed by 2020-10-09, Last Modified 2015-02.


-> 추천(25점): 구현에 대해서 많은 도움을 주었다.


2. 스프링 부트(Spring Boot) - AOP와 트랜잭션(Transaction) 설정하기 [개발을 시작해봐요!], https://congsong.tistory.com/25, Accessed by 2020-10-09, Last Modified 2020-04-28.

-> 추천(25점): 스프링 부트로 작성되었지만, AOP 개발 방법의 흐름을 살펴볼 수 있었음.

반응형
728x90
300x250

[PC활용] 자바 - JDK 버전 별 - Eclipse IDE 정리


자바 JDK 버전 별로 사용가능한 이클립스가 있다.

협업 프로젝트를 하다보면, 이런 것도 맞춰줘야 하는 부분이 생긴다.


최신 버전만 가지고 진행할 수 없는 일들이 생길 수 있을 거 같아서 정리하게 되었다.



1. 이클립스 버전명 및 요구사항


이클립스 버전명 및 요구사항을 정리하였다.

정리하는 이유는 구 버전 이클립스를 제공해줘버리면, 


하위 버전의 JDK를 설치해야 하는 문제가 생기는데, 꽤나 골치 아프게 된다.




                                      표 1. 이클립스 버전명 및 요구사항


번호

이클립스 버전명

요구사항

비고

1

Eclipse 4.17 (2020-09)

A Java 11 or newer JRE/JDK is required,

(Java 11 이상 JRE / JDK가 필요합니다.)

 

2

Eclipse 4.16 (2020-06)

A Java 8 or newer JRE/JDK is required, 

LTS release are preferred

(Java 8 이상 JRE / JDK가 필요합니다.

LTS 릴리스가 선호됨)

 

3

Eclipse 4.14 (2019-12)

A Java 8 or newer JRE/JDK is required.

(Java 8 이상 JRE / JDK가 필요합니다.)

 

4

Eclipse 4.13 (2019-09)

A Java 8 or newer JRE/JDK is required.
(Java 8 이상 JRE / JDK가 필요합니다.)

 

5

Eclipse 4.12 (2019-06)

A Java 8 or newer JRE/JDK is required

(Java 8 이상 JRE / JDK가 필요합니다.)

 

6

Eclipse 4.11 (2019-03)

A Java 8 or newer JRE/JDK is required

(Java 8 이상 JRE / JDK가 필요합니다.)

 

7

Eclipse 4.10 (2018-12)

A Java 8 or newer JRE/JDK is required

(Java 8 이상 JRE / JDK가 필요합니다.)

 

8

Eclipse 4.9 (2018-09)

A Java 8 or newer JRE/JDK is required

(Java 8 이상 JRE / JDK가 필요합니다.) 

 

9

Eclipse 4.8 (Photon)

 A Java 8 or newer JRE/JDK is required

(Java 8 이상 JRE / JDK가 필요합니다.) 

 

10

Eclipse 4.7 (Oxygen)

A Java 8 or newer JRE/JDK is required

(Java 8 이상 JRE / JDK가 필요합니다.)

 

11

Eclipse 4.6 (Neon)

A Java 8 or newer JRE/JDK is required

(Java 8 이상 JRE / JDK가 필요합니다.) 

 

12

Eclipse 4.5 (Mars)

A Java 7 or newer JRE/JDK is required

(Java 7 이상 JRE / JDK가 필요합니다.)

 

13

Eclipse 4.4 (Luna)

A Java 7 JRE/JDK is required

(Java 7 JRE / JDK가 필요합니다.)

 

14

Eclipse 4.3 (Kepler)

A Java 6 JRE/JDK is recommended

(Java 6 JRE / JDK 권장)

 

15

 

 

 


* 참고 사이트: https://wiki.eclipse.org/Eclipse/Installation





2. JDK 버전


JDK를 배포하는 사이트이다. (오픈소스 형태의 JDK)


* Corretto JDK(OpenJDK): https://aws.amazon.com/ko/corretto/

  (배포 버전: 8, 11) - 64bit 가능함.

* OpenJDK: https://openjdk.java.net/
              
https://jdk.java.net/archive/ (이전 버전: 아카이브)

  (배포 버전: 7~버전부터)

  - 단점: 7, 8버전이 32bit로 되어 있음. 9.04버전부터 64bit 지원함.

반응형

+ Recent posts