More Related Content Similar to CodeFest 2011. Крестьянинов М. — Обзор аспектно-ориентированного программирования Similar to CodeFest 2011. Крестьянинов М. — Обзор аспектно-ориентированного программирования (20) CodeFest 2011. Крестьянинов М. — Обзор аспектно-ориентированного программирования3. Сферический быдлокод в вакууме public class WebService { publicBookDTOgetBook(IntegerbookId) { BookDTObook = bookDAO.readBook(bookId); returnbook; } } 3 4. Сферический быдлокод: Логирование publicBookDTOgetBook(IntegerbookId) { LOG.debug("Call method getBook with id " + bookId); BookDTObook = bookDAO.readBook(bookId); LOG.debug("Book info is: " + book.toString()); returnbook; } 4 5. Сферический быдлокод: Обработкаисключений publicBookDTOgetBook(IntegerbookId) throws ServiceException { LOG.debug("Call method getBook with id " + bookId); BookDTObook = null; try { BookDTObook = bookDAO.readBook(bookId); } catch(SQLException e) { throw new ServiceException(e); } LOG.debug("Book info is: " + book.toString()); returnbook; } 5 6. Сферический быдлокод:Авторизация publicBookDTOgetBook(IntegerbookId) throws ServiceException, AuthException { if (!SecurityContext.getUser().hasRight("GetBook")) throw new AuthException("Permission Denied"); LOG.debug("Call method getBook with id " + bookId); BookDTObook = null; try{ BookDTObook = bookDAO.readBook(bookId); } catch(SQLException e) { throw newServiceException(e); } LOG.debug("Book info is: " + book.toString()); returnbook; } 6 7. Сферический быдлокод:Кеширование publicBookDTOgetBook(IntegerbookId) throws ServiceException, AuthException { if(!SecurityContext.getUser().hasRight("GetBook")) throw newAuthException("Permission Denied"); LOG.debug("Call method getBook with id " + bookId); BookDTObook = null; String cacheKey = "getBook:" + bookId; try{ if (cache.contains(cacheKey)) { book = (BookDTO) cache.get(cacheKey); } else { BookDTObook = bookDAO.readBook(bookId); cache.put(cacheKey, book); } } catch(SQLException e) { throw newServiceException(e); } LOG.debug("Book info is: " + book.toString()); returnbook; } 7 17. AspectJ: Примераспекта @Aspect public classWebServiceLogger { privatefinal static Logger LOG = Logger.getLogger(WebServiceLogger.class); @Pointcut("execution(* example.WebService.*(..)) && @annotation(example.Loggable)") public voidloggableMethod() { } @Around(“loggableMethod()") public ObjectlogWebServiceCall(ProceedingJoinPointthisJoinPoint) { String methodName = thisJoinPoint.getSignature().getName(); Object[] methodArgs = thisJoinPoint.getArgs(); LOG.debug("Call method " + methodName + " with args " + methodArgs); Object result = thisJoinPoint.proceed(); LOG.debug("Method " + methodName + " returns " + result); returnresult; } } 11 35. after throwing — запусксоветапослевыбросаисключения в процессевыполненияточкисоединения; 38. AspectJ: вторая попытка @Aspect public classWebServiceLogger { private final static Logger LOG = Logger.getLogger(WebServiceLogger.class); @Pointcut("execution(* example.WebService.*(..)) && @annotation(example.Loggable)") public voidloggableMethod() { } @Around(“loggableMethod()") public ObjectlogWebServiceCall(ProceedingJoinPointthisJoinPoint) { String methodName = thisJoinPoint.getSignature().getName(); Object[] methodArgs = thisJoinPoint.getArgs(); LOG.debug("Call method " + methodName + " with args " + methodArgs); Object result = thisJoinPoint.proceed(); LOG.debug("Method " + methodName + " returns " + result); return result; } } 16 39. PostSharp Аспектобработкиисключений: public classExceptionDialogAttribute : OnExceptionAspect { public override voidOnException(MethodExecutionEventArgs e) { stringmessage = e.Exception.Message; Window window = Window.GetWindow((DependencyObject) eventArgs.Instance); MessageBox.Show(window, message, "Exception"); eventArgs.FlowBehavior= FlowBehavior.Continue; } } 17 58. Spring AOP: Примеры Ограничениеправдоступа: <sec:protect-pointcut expression="execution(* ru.novotelecom.xxx.Subscription.*(..))" access="ROLE_SUBSCRIPTION_MANAGER"/> Или @Aspect public class SecurityManager{ @Before("execution(* ru.novotelecom.xxx.Subscription.*(..))") public void doAccessCheck() {… } } 27 59. Spring AOP: Примеры Обработкаисключений: @Aspect public classWebServiceException { @Pointcut("execution(* example.WebService.*(..)) && @annotation(example.Loggable)") public voidloggableMethod() { } @Around(“loggableMethod()") public ObjectlogWebServiceCall(ProceedingJoinPointthisJoinPoint) throws ServiceException { Object result = null; try { result = thisJoinPoint.proceed(); } catch (Exception e) { throw new ServiceException(e); } returnresult; } } 28 70. Для Java AOP доступенчерезпроект AspectJ, для .NET – через PostSharp;