Skip to content

Instantly share code, notes, and snippets.

View gksxodnd007's full-sized avatar

hubert gksxodnd007

View GitHub Profile
@gksxodnd007
gksxodnd007 / spring-junit5-test.md
Created August 24, 2018 09:00
spring test with junit5

Junit 5 & Spring Test을 이용한 TDD 환경 세팅

기본 세팅

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = KkApplication.class)
@ActiveProfiles("test")
public abstract class SpringTestSupport {
}
@gksxodnd007
gksxodnd007 / profile-default.png
Last active August 24, 2018 03:06
spring boot profile 설정
profile-default.png
@gksxodnd007
gksxodnd007 / spring-junit4-test.md
Last active April 8, 2021 12:05
spring integration test & unit test using mockito & junit4

Junit 4 & Spring Test을 이용한 TDD 환경 세팅

  • SpringTestSupport 클래스에 설정 후 이 클래스를 상속받아 테스트를 개발함.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { RootContextConfig.class },
        loader = AnnotationConfigWebContextLoader.class)
@WebAppConfiguration
public class SpringTestSupport {
@gksxodnd007
gksxodnd007 / JpaTransaction.md
Created August 13, 2018 06:40
JPA 트랜잭션 롤백

Spring Data JPA에서 트랜잭션 롤백

  • 보통 spring data를 이용하면 영속성 컨텍스트의 생명주기는 트랜잭션의 유지시간과 동일함.
    • exception이 발생하여 롤백 시 트랜잭션이 닫히면서 자연스럽게 영속성 컨텍스트도 닫힌다. 문제 발생할 여지 없음
  • OSIV처럼 영속성 컨텍스트의 생명주기가 트랜잭션보다 길 때 발생 할 수 있는 문제점.
    • 데이터베이스에는 데이터가 반영되지 않았지만 영속성 컨텍스트에는 데이터가 잔류 되어있다.
    • spring frame work에서는 다음과 같은 방법으로 문제를 해결한다.
      • 트랜잭션 롤백 시 entityManager.clear()를 호출하여 영속성 컨텍스트를 초기화한다.

관련 코드

@gksxodnd007
gksxodnd007 / springProperties.md
Created August 13, 2018 01:50
properties 파일 값을 읽어오는 방법

config xx.properties with Spring boot

1. @PropertySource 와 @Value를 이용한 방법

@Configuration
@PropertySource("classpath:sandbox_db.properties")
public class SandBoxDBConfig {

    @Value("${datasource.slave.driver-class-name}")
    private String driverClassName;
@gksxodnd007
gksxodnd007 / querydsl.md
Created August 12, 2018 04:26
querydsl이 무엇이고 어떻게 사용하는지 알아보자.

QueryDsl이란?

  • JPQL의 빌더(Criteria)클래스

QueryDsl 사용전 설정

  • dependency 추가
dependencies {
  compile("com.querydsl:querydsl-core:4.2.1")
  compile("com.querydsl:querydsl-apt:4.2.1")
  compile("com.querydsl:querydsl-jpa:4.2.1")
@gksxodnd007
gksxodnd007 / springboot-logback.md
Created August 12, 2018 04:21
스프링부트 Log back 설정

Spring boot logback 설정

  • classpath(resource디렉토리 밑)에 logback-spring.xml파일이 있으면 Boot가 설정파일을 읽어감.
  • logback-spring.xml파일이 없다면 .yml(.properties)파일의 설정을 보게됨.

설정시 특징

<appender name="privateLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <file>${LOG_PATH}/private.${port:-default}.log</file>
 
@gksxodnd007
gksxodnd007 / springBatchStepConfig.md
Created July 24, 2018 02:30
spring batch step config

Configuring a Step

TaskletStep

Chunk-oriented processing is not the only way to process in a Step. What if a Step must consist as a simple stored procedure call? You could implement the call as an ItemReader and return null after the procedure finishes, but it is a bit unnatural since there would need to be a no-op ItemWriter. Spring Batch provides the TaskletStep for this scenario.

The Tasklet is a simple interface that has one method, execute, which will be a called repeatedly by the TaskletStep until it either returns RepeatStatus.FINISHED or throws an exception to signal a failure. Each call to the Tasklet is wrapped in a transaction. Tasklet implementors might call a stored procedure, a script, or a simple SQL update statement

  • example code
@gksxodnd007
gksxodnd007 / springBatchJobConfig.md
Created July 24, 2018 02:29
spring batch job config

Spring Batch Configuring Job

Restartability

if you want to restart the Job with new JobInstance. you have to setting option like below

<job id="footballJob" restartable="false">
    ...
@gksxodnd007
gksxodnd007 / springBatchTerms.md
Created July 24, 2018 02:28
spring batch terms

Spring Batch Terms

Job

In Spring Batch, a Job is simply a container for Steps. It combines multiple steps that belong logically together in a flow and allows for configuration of properties global to all steps, such as restartability.

The job configuration contains

  • The simple name of the job
  • Definition and ordering of Steps