22
Spring 프프프프프프 프프 4. Spring AOP 프프 프 프프

Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

Embed Size (px)

Citation preview

Page 1: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

Spring 프레임워크의 이해

4. Spring AOP 이해 및 활용

Page 2: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

2007 Grow up to be NHN 人Spring – AOP

요구사항 !!

• 비즈니스 계층과 퍼시스턴스 계층의 모든 메써드 시작과 종료시 “메써드 시작” , “ 메써드 종료”라는 Logging 메시지를 출력한다 . 또한 메써드에 인자를 출력한다 .

• Runtime Exception 이 발생할 경우 시스템 관리자에게 에러 메시지에 대한 메일을 발송한다 .

Page 3: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

2007 Grow up to be NHN 人Spring – AOP

Page 4: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 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; }

Page 5: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

2007 Grow up to be NHN 人Spring – AOP

Page 6: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 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

Page 7: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

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; }}

Page 8: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

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() 종료 "); } }}

Page 9: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

2007 Grow up to be NHN 人Spring – AOP

Page 10: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

2007 Grow up to be NHN 人Spring – AOP

Spring AOP

Page 11: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

2007 Grow up to be NHN 人Spring – AOP

Spring AOP

Page 12: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 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);}

};}

}

Page 13: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

2007 Grow up to be NHN 人Spring – AOP

Page 14: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 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" />

Page 15: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

2007 Grow up to be NHN 人Spring – AOP

Factory method

<bean id="loggingAspect" class="net.javajigi.advice.LoggingAspect" factory-method="aspectOf" />

Page 16: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

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>

Page 17: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

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”);

Page 18: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

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”);

Page 19: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

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”);

Page 20: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

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”);

Page 21: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

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”);

Page 22: Spring 프레임워크의 이해 4. Spring AOP 이해 및 활용. 2007 Grow up to be NHN 人 Spring – AOP 요구사항 !! 비즈니스 계층과 퍼시스턴스 계층의 모든

2007 Grow up to be NHN 人Spring – AOP