spring 프레임워크의 이해 4. spring aop 이해 및 활용. 2007 grow up to be nhn 人 spring...
TRANSCRIPT
Spring 프레임워크의 이해
4. Spring AOP 이해 및 활용
2007 Grow up to be NHN 人Spring – AOP
요구사항 !!
• 비즈니스 계층과 퍼시스턴스 계층의 모든 메써드 시작과 종료시 “메써드 시작” , “ 메써드 종료”라는 Logging 메시지를 출력한다 . 또한 메써드에 인자를 출력한다 .
• Runtime Exception 이 발생할 경우 시스템 관리자에게 에러 메시지에 대한 메일을 발송한다 .
2007 Grow up to be NHN 人Spring – AOP
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; }
2007 Grow up to be NHN 人Spring – AOP
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
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; }}
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() 종료 "); } }}
2007 Grow up to be NHN 人Spring – AOP
2007 Grow up to be NHN 人Spring – AOP
Spring AOP
2007 Grow up to be NHN 人Spring – AOP
Spring AOP
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);}
};}
}
2007 Grow up to be NHN 人Spring – AOP
2007 Grow up to be NHN 人Spring – AOP
new
<bean id=“userDAO" class="net.javajigi.user.dao.MySQLUserDAO" />
2007 Grow up to be NHN 人Spring – AOP
Factory method
<bean id="loggingAspect" class="net.javajigi.advice.LoggingAspect" factory-method="aspectOf" />
2007 Grow up to be NHN 人Spring – AOP
FactoryBean Interface
<bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"> <ref local="userServiceTarget" /> </property> <property name="interceptorNames"> <list> <value>loggingAdvice</value> <value>emailNotificationThrowsAdvice</value> </list> </property></bean>
2007 Grow up to be NHN 人Spring – AOP
<bean id="userService" class="net.javajigi.user.service.UserServiceImpl"> <property name="userDAO"> <ref local="userDAO" /> </property></bean>
ApplicationContext context = new ClassPathXmlApplicationContext(paths);UserService userService = context.getBean(“userService”);
2007 Grow up to be NHN 人Spring – AOP
<bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"> <ref local="userServiceTarget" /> </property> <property name="interceptorNames"> <list> <value>loggingAdvice</value> <value>emailNotificationThrowsAdvice</value> </list> </property></bean>
ApplicationContext context = new ClassPathXmlApplicationContext(paths);ProxyFactoryBean factoryBean = context.getBean(“userService”);
2007 Grow up to be NHN 人Spring – AOP
<bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target"> <ref local="userServiceTarget" /> </property> <property name="interceptorNames"> <list> <value>loggingAdvice</value> <value>emailNotificationThrowsAdvice</value> </list> </property></bean>
ApplicationContext context = new ClassPathXmlApplicationContext(paths);UserService userService = context.getBean(“userService”);
2007 Grow up to be NHN 人Spring – AOP
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:comp/env/jdbc/petclinic</value> </property></bean>
ApplicationContext context = new ClassPathXmlApplicationContext(paths);JndiObjectFactoryBean factoryBean = context.getBean(“dataSource”);
2007 Grow up to be NHN 人Spring – AOP
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:comp/env/jdbc/petclinic</value> </property></bean>
ApplicationContext context = new ClassPathXmlApplicationContext(paths);DataSource dataSource = context.getBean(“dataSource”);
2007 Grow up to be NHN 人Spring – AOP