Preloader image
DDD

Java TIP

SPRING 3.x @ 어노테이션(annotation) 사용

작성자 관리자 (admin)
조회수 72
입력일 2020-09-10 03:10:44

SPRING 3.x @ 어노테이션(annotation) 사용

선언부

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.0.xsd">


1. 외부 xml에 선언해서 사용하는 방법

    <context:component-scan base-package="패키지명"/>
    <task:scheduled-tasks>
        <task:scheduled ref="scheduleTest" method="testScheduleTest" cron="0 30 16 * * ?" />
    </task:scheduled-tasks>


사용할 자바 클래스

@Component
public class ScheduleTest {

    @Autowired
    GallService gallService;

    public void testScheduleTest()
    {
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("SCHEDULE 현재 시각: " +  dateFormat.format(calendar.getTime()));
        gallService.testSchedule();
    }

}

2. 클래스에 직접 지정

    <context:component-scan base-package="패키지명"/>
    <task:annotation-driven />


사용할 자바 클래스

@Component
public class ScheduleTest
{
    @Autowired
    GallService gallService;

    @Scheduled(cron="0 19 16 * * ?")
    public void testScheduleTest() {

        java.util.Calendar calendar = java.util.Calendar.getInstance();
        java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("SCHEDULE 현재 시각: " +  dateFormat.format(calendar.getTime()));
        gallService.testSchedule();
    }
}
 

^