ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • day64 - Spring Framework(스프링, AOP)
    KIC/Spring 2021. 9. 14. 23:46
    반응형

     

    [Main3.java]

    package ch03_AOPXmlAnno;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import di06.Article;
    import di06.ArticleNotFoundException;
    import di06.Member;
    import di06.MemberService;
    import di06.ReadArticleService;
    import di06.UpdateInfo;
    
    public class Main3 {
    	public static void main(String[] args) {
    		
    		String[] config = {"ch03_AOPXmlAnno/annotation.xml"}; 
    		ApplicationContext ctx = new ClassPathXmlApplicationContext(config); 
    		ReadArticleService service = ctx.getBean("readArticleService", ReadArticleService.class);
    	
    		try {
    			Article a1 =  service.getArticleAndReadCnt(1); //A 
    			Article a2 = service.getArticleAndReadCnt(1); //B 
    			System.out.println("[main] a1 == a2 : " + (a1==a2));
    			service.getArticleAndReadCnt(0); //C
    		} catch (ArticleNotFoundException e) {
    	
    		System.out.println("[main]" + e.getMessage());
    		}
    		System.out.println();
    		System.out.println("=== updateMemberInfoTraceAspect 연습");
    		MemberService memberService = ctx.getBean("memberService", MemberService.class);
    		memberService.regist(new Member());
    		memberService.update("hong", new UpdateInfo());//traceAspect AOP 대상이 되는 메서드
    		memberService.delete("hong2", "test");
    	}
    }

     

     

    [LoggingAspect.java]

    package di07;
    
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    @Component
    @Aspect
    @Order(1)
    public class LoggingAspect {
    	final String publicMethod = "execution(public * di06.*Service.*(..))";
    	
    	@Before(publicMethod)
    	public void before() {
    		System.out.println("[LA]메서드 실행 전(before) 전처리 수행함.");
    	}
    	
    	@AfterReturning(pointcut = publicMethod, returning = "ret")
    	public void afterReturning(Object ret) {
    		System.out.println("[LA]메서드 정상 처리(afterReturning) 후 수행함 리턴값: " + ret);
    	}
    	
    	@AfterThrowing(pointcut = publicMethod, throwing = "ex")
    	public void afterThrowing(Throwable ex) {
    		System.out.println("[LA]메서드 예외 발생 후(afterThrowing) 수행함 예외:" +ex.getMessage());
    	}
    	
    	@After(publicMethod)
    	public void afterFinally() {
    		System.out.println("[LA]메서드 실행 후(afterFinally) 후처리 수행함");
    	}
    }

     

    [ArticleCacheAdvice.java]

    package di07;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    import di06.Article;
    
    @Component
    @Aspect
    @Order(2)
    public class ArticleCacheAdvice {
    	private Map<Integer, Article> cache = new HashMap<Integer, Article>();
    	
    	@Around("execution(public * *..ReadArticleServiceImpl.*(..))")
    	public Article cache(ProceedingJoinPoint joinPoint) throws Throwable {
    		System.out.println("[ACA] cache before 실행");
    		
    		Integer id = (Integer)joinPoint.getArgs()[0];
    		Article article = cache.get(id);
    		if(article != null) {
    			System.out.println("[ACA] cache에서 Article[" + id +"] 가져옴");
    			return article;
    		}
    		Article ret = (Article)joinPoint.proceed();
    		System.out.println("[ACA] cache after 실행");
    		if(ret != null) {
    			cache.put(id,  ret);
    			System.out.println("[ACA] cache에 Article[" +id +"] 객체를 추가함");
    		}
    		return ret;
    	}
    }
    300x250

    댓글

Designed by Tistory.