Spring AOP : 프록시 기반 AOP

 

AOP 특징

 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;


@Component
public class AppRunner implements ApplicationRunner {


    @Autowired
    EventService eventService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        eventService.createEvent();
        eventService.publishEveent();

    }
}
package com.kyhslam.demo;

public interface EventService {

    void createEvent();

    void publishEveent();

    void deleteEvent();
}
package com.kyhslam.demo;

import org.springframework.stereotype.Service;

@Service
public class SimpleEventService implements EventService {

    @Override
    public void createEvent() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("Created an event");
    }

    @Override
    public void publishEveent() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("Published as event");
    }

    @Override
    public void deleteEvent() {
        System.out.println("DeleteEvent as event");
    }
}
package com.kyhslam.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

@Primary
@Service
public class ProxySimpleEventService implements EventService {

    @Autowired
    SimpleEventService simpleEventService;

    @Override
    public void createEvent() {
        long begin = System.currentTimeMillis();
        simpleEventService.createEvent();
        System.out.println(System.currentTimeMillis() - begin);
    }

    @Override
    public void publishEveent() {
        long begin = System.currentTimeMillis();
        simpleEventService.publishEveent();
        System.out.println(System.currentTimeMillis() - begin);
    }

    @Override
    public void deleteEvent() {
        simpleEventService.deleteEvent();
    }
}

 

결과

Created an event
1013
Published as event
2008
DeleteEvent as event

'📚 Spring > Spring 개념' 카테고리의 다른 글

Filter  (0) 2021.09.05
Spring AOP : @AOP  (0) 2020.11.24
SpEL(스프링 Expression Language)  (0) 2020.10.05
데이터 바인딩 추상화 : Converter와 Formatter  (0) 2020.10.05
데이터 바인딩 추상화 : PropertyEditor  (0) 2020.09.14

댓글

Designed by JB FACTORY