스프링/스프링부트 어노테이션
어노테이션 설명/예제
@NoArgsConstructor

<설명>

파라미터가 없는 default 생성자를 생성해주는 어노테이션

 

<예제>

package com.bootbatch.main;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class UserVO {
private int count;


public UserVO(int count) {
this.count = count;
}
}

@Configuration

<설명>

스프링의 @Configuration 어노테이션은 이 클래스가 구성 클래스임을 스프링에 알린다.

스프링은 @Configuration이 달린 클래스를 보면 일단 그 안에서 빈 인스턴스 정의부, 즉 @Bean을 붙인(빈 인스턴스를 생성해 반환하는) 자바 메서드를 찾는다.

 

아래 <예제1>에서 dataSource method를 보자 @Bean처리된 것을 확인할 수 있다.

<예제2>를 보면 private final로 dataSource 변수에 DI(Dependency Injection)가 된 것을 확인할 수 있다.

 

@ComponentScan

<설명>

@ComponentScan 어노테이션은 @Component 어노테이션 및 streotype(@Service, @Repository, @Controller) 어노테이션이 부여된 Class들을 자동으로 Scan하여 Bean으로 등록해주는 역할을 하는 어노테이션이다.

@PropertySource

<설명>

Property 파일을 Environment로 로딩할 수 있다.

@EnableBatchProcessing 

<설명>

@EnableBatchProcessing 어노테이션은 다른 스프링의 Enable**과 비슷하게 동작하며 배치 Job을 구성하는 기본 설정을 제공한다.

<예제1>

@Configuration
@ComponentScan("com.bootbatch")
@PropertySource("classpath:/database.properties")
@EnableBatchProcessing
public class BatchConfiguration {

    @Autowired
    private Environment env;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(env.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.username"));
        dataSource.setPassword(env.getProperty("jdbc.password"));
        return dataSource;
    }

    @Bean
    public DataSourceInitializer databasePopulator() {
        ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.addScript(new ClassPathResource("org/springframework/batch/core/schema-oracle10g.sql"));
        populator.addScript(new ClassPathResource("total_count.sql"));
        populator.setContinueOnError(true);
        populator.setIgnoreFailedDrops(true);

        DataSourceInitializer initializer = new DataSourceInitializer();
        initializer.setDatabasePopulator(populator);
        initializer.setDataSource(dataSource());
        return initializer;
    }

}

<예제2>

package com.bootbatch.job;

import javax.sql.DataSource;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.bootbatch.main.BatchVO;

import lombok.RequiredArgsConstructor;


@Configuration
@RequiredArgsConstructor
public class BatchJob {
private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactoy;
    
    @Autowired
    private DataSource dataSource;
    
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;


    @Bean
    public Job jdbcCursorItemReaderJob() {
     return jobBuilderFactory.get("jdbcCursorItemReaderJob")
     .start(jdbcCursorItemReaderStep())
     .build();
    }

    @Bean
    public Step jdbcCursorItemReaderStep() {
     return stepBuilderFactoy.get("jdbcCursorItemReaderStep")
     .<BatchVO, BatchVO>chunk(10)
     //reader에서 받아서
     .reader(jdbcCursorItemReader())
     //writer에 넣는다.
     .writer(jdbcCursorItemWriter())
     .build();
    }


    @Bean
    JdbcBatchItemWriter jdbcCursorItemWriter(){
     JdbcBatchItemWriter itemWriter = new JdbcBatchItemWriter();
     itemWriter.setDataSource(dataSource);
     itemWriter.setSql("insert into insert_count values(:count)");
     itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider());
     return itemWriter;
    }
    
    @Bean
    public JdbcCursorItemReader jdbcCursorItemReader(){
     System.out.println("===>jdbcCursorItemReader 접속!!");
     return new JdbcCursorItemReaderBuilder()
     .fetchSize(10)
     .dataSource(dataSource)
     .rowMapper(new BeanPropertyRowMapper<>(BatchVO.class))
     .sql("SELECT COUNT FROM TOTAL_COUNT")
     .name("jdbcCursorItemWriter")
     .build();
 
    }
}

@Entity

@Entity

- JPA를 사용해서 테이블과 매핑할 클래스는 @Entity 어노테이션을 필수로 붙여야 합니다.
- @Entity가 붙은 클래스는 JPA가 관리하고, 엔티티라 부릅니다.
- @Entity 적용 시 주의사항
- 기본 생성자가 필수(파라미터가 없는 public 또는 protected 생성자)
- final 클래스, enum, interface, inner 클래스에는 사용 불가
- 저장할 필드에 final 사용 불가

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

+ Recent posts