-
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'KIC > Spring' 카테고리의 다른 글
day66 - Spring Framework(mvc) (0) 2021.09.17 day65 - Spring Framework(스프링, Annotation) (0) 2021.09.15 day63 - Spring Framework(스프링, AOP) (0) 2021.09.13 day62 - Spring Framework(스프링, Annotation) (0) 2021.09.10 day61 - Spring Framework(스프링, DI) (0) 2021.09.10