Download presentation
Presentation is loading. Please wait.
Published byGervais Hall Modified over 9 years ago
1
Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용
2
2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든 메써드 시작과 종료시 “ 메써드 시작 ”, “ 메써드 종료 ” 라는 Logging 메시지를 출력한다. 또한 메써드에 인자를 출력한다. Runtime Exception 이 발생할 경우 시스템 관리자에게 에러 메시지에 대한 메일을 발송한다.
3
2007 Grow up to be NHN 人 Spring – AOP
4
2007 Grow up to be NHN 人 Spring – AOP public User findUser(String userId) throws UserNotFoundException { if (logger.isDebugEnabled()) { logger.debug("findUser() 시작 "); logger.debug("User ID : " + userId); } User user = null; try { user = userDAO.findUser(userId); } catch (DataAccessException e) { mailSender.sendMessage(e); throw e; } if (user == null) { throw new UserNotFoundException(context.getMessage( "user.notfound.exception", new Object[] { userId }, null)); } if (logger.isDebugEnabled()) { logger.debug(userId + " 사용자 정보 : " + user); } if (logger.isDebugEnabled()) { logger.debug("findUser() 종료 "); } return user; }
5
2007 Grow up to be NHN 人 Spring – AOP
6
2007 Grow up to be NHN 人 Spring – AOP public User findUser(String userId) throws UserNotFoundException { User user = userDAO.findUser(userId); if (user == null) { throw new UserNotFoundException(context.getMessage( "user.notfound.exception", new Object[] { userId }, null)); } if (logger.isDebugEnabled()) { logger.debug(userId + " 사용자 정보 : " + user); } return user; } UserServiceImpl.java
7
2007 Grow up to be NHN 人 Spring – AOP public class UserLoggingAdvice implements MethodInterceptor { protected final Log logger = LogFactory.getLog(getClass()); public Object invoke(MethodInvocation invocation) throws Throwable { String className = invocation.getThis().getClass().getName(); if (logger.isDebugEnabled()) { logger.debug(className + "." + invocation.getMethod().getName() + "()" + " 시작 !!"); Object[] args = invocation.getArguments(); if ((args != null) && (args.length > 0)) { for (int i = 0; i < args.length; i++) { logger.debug("Argument[" + i + "] : " + args[i]); } //Target 클래스의 메써드를 실행한다. Object retVal = invocation.proceed(); if (logger.isDebugEnabled()) { logger.debug(className + "." + invocation.getMethod().getName() + "()" + " 종료 !!"); } return retVal; }
8
2007 Grow up to be NHN 人 Spring – AOP public class EmailNotificationThrowsAdvice implements ThrowsAdvice { protected final Log logger = LogFactory.getLog(getClass()); private ExceptionMailSender mailSender; public void setMailSender(ExceptionMailSender mailSender) { this.mailSender = mailSender; } public void afterThrowing(RuntimeException ex) throws Throwable { if (logger.isDebugEnabled()) { logger.debug("afterThrowing() 시작 "); logger.debug("Caught : " + ex.getClass().getName()); } mailSender.sendMessage(ex); if (logger.isDebugEnabled()) { logger.debug("afterThrowing() 종료 "); }
9
2007 Grow up to be NHN 人 Spring – AOP
10
2007 Grow up to be NHN 人 Spring – AOP Spring AOP
11
2007 Grow up to be NHN 人 Spring – AOP Spring AOP
12
2007 Grow up to be NHN 人 Spring – AOP public class LoggingPointcut extends StaticMethodMatcherPointcut { public boolean matches(Method method, Class cls) { if( "findUser".equals(method.getName()) ) { return true; } return false; } public ClassFilter getClassFilter() { return new ClassFilter() { public boolean matches(Class cls) { return (cls == UserServiceImpl.class) || (cls == MySQLUserDAO.class); } }; }
13
2007 Grow up to be NHN 人 Spring – AOP
14
2007 Grow up to be NHN 人 Spring – AOP new
15
2007 Grow up to be NHN 人 Spring – AOP Factory method
16
2007 Grow up to be NHN 人 Spring – AOP FactoryBean Interface loggingAdvice emailNotificationThrowsAdvice
17
2007 Grow up to be NHN 人 Spring – AOP ApplicationContext context = new ClassPathXmlApplicationContext(paths); UserService userService = context.getBean( “ userService ” );
18
2007 Grow up to be NHN 人 Spring – AOP loggingAdvice emailNotificationThrowsAdvice ApplicationContext context = new ClassPathXmlApplicationContext(paths); ProxyFactoryBean factoryBean = context.getBean( “ userService ” );
19
2007 Grow up to be NHN 人 Spring – AOP loggingAdvice emailNotificationThrowsAdvice ApplicationContext context = new ClassPathXmlApplicationContext(paths); UserService userService = context.getBean( “ userService ” );
20
2007 Grow up to be NHN 人 Spring – AOP java:comp/env/jdbc/petclinic ApplicationContext context = new ClassPathXmlApplicationContext(paths); JndiObjectFactoryBean factoryBean = context.getBean( “ dataSource ” );
21
2007 Grow up to be NHN 人 Spring – AOP java:comp/env/jdbc/petclinic ApplicationContext context = new ClassPathXmlApplicationContext(paths); DataSource dataSource = context.getBean( “ dataSource ” );
22
2007 Grow up to be NHN 人 Spring – AOP
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.