작업제목 : 스프링(부트 포함) 배치 프로그램(SELECT 후 INSERT DB)
작업순서 : 
1. 스프링 부트 pom.xml 설정
2. BatchVO.class, BatchConfiguration.class, BatchJob.class, BatchApplication.class 생성
3. BatchConfiguration.class 작성
4. BatchVO.class 작성
5. BatchJob.class 작성
6. BatchApplication.class 작성
7. 정상 작동되는지 테스트 및 확인
8. jar 파일로 생성
9. bat 파일 생성 후 bat 파일로 실행
10. 윈도우 스케줄러에 등록하여 특정 시간에 bat 파일이 작동되도록 구성
BatchConfiguration.class를 작성해보도록하자.

package com.bootbatch.job;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import javax.sql.DataSource;

//@Configuration 하나 이상의 Bean 메소드를 제공하고
//스프링 컨테이너가 Bean 정의를 생성하고 런타임시 그 Bean들이 요청들을 처리할 것을 선언하게 된다.
@Configuration
//@ComponentScan 어노테이션은 @Component 어노테이션 및 streotype(@Service, @Repository, @Controller) 어노테이션이 부여된 Class들을 
//자동으로 Scan하여 Bean으로 등록해주는 역할을 하는 어노테이션이다. 
@ComponentScan("com.bootbatch")
//@PropertySource은 Property 파일을 Environment로 로딩할 수 있다.
@PropertySource("classpath:/database.properties")
//@EnableBatchProcessing 어노테이션은 다른 스프링의 Enable**과 비슷하게 동작하며 배치 Job을 구성하는 기본 설정을 제공한다.
@EnableBatchProcessing
public class BatchConfiguration {

    @Autowired
    //외부 파일을  이용하기 위해 사용하는 Environment 클래스
    private Environment env;

    @Bean
    //DataSource 메소드를 Bean 처리함 dataSource를 리턴 받아서 사용할 수 있게 한다.
    public DataSource dataSource() {
    	//일반적인 웹 프로젝트의 경우 JDBC 연결을 얻듯이
    	//DriverManagerDataSource로 연결을 얻는다.
    	DriverManagerDataSource dataSource = new DriverManagerDataSource();
        //DB 정보를 set한다.
    	//env로 받아옴
    	dataSource.setUrl(env.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.username"));
        dataSource.setPassword(env.getProperty("jdbc.password"));
        return dataSource;
    }

    @Bean
    //DataSourceInitializer 초기화 중 데이터베이스와 소멸 중 데이터베이스를 설정하는데 사용된다.
    public DataSourceInitializer databasePopulator() {
        ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        //Oracle 스크립트 추가
        populator.addScript(new ClassPathResource("org/springframework/batch/core/schema-oracle10g.sql"));
        //해당 total_count.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;
    }

}

+ Recent posts