Posted By

rahulshivsharan on 09/16/11


Tagged

SpringAOP


Versions (?)

Spring


 / Published in: Java
 

URL: SpringAOP /AspectJExpressionPointcut

  1. package com.jal;
  2. import org.aopalliance.intercept.*;
  3.  
  4. public class AudienceAroundAdvice implements MethodInterceptor{
  5.  
  6. public Audience audience;
  7.  
  8. public void setAudience(Audience audience){
  9. this.audience = audience;
  10. }
  11.  
  12. public Audience getAudience(){
  13. return audience;
  14. }
  15.  
  16. public AudienceAroundAdvice(){
  17. System.out.println(" AudienceAroundAdvice initialised..... ");
  18. }
  19.  
  20.  
  21. @Override
  22. public Object invoke(MethodInvocation invocation) throws java.lang.Throwable {
  23. System.out.println(" METHODS-BEFORE-ADVICE ");
  24. Object obj = null;
  25. try{
  26.  
  27. this.audience.takeSeats();
  28. this.audience.turnOffCellPhones();
  29. obj = invocation.proceed();
  30. System.out.println(" METHODS-AFTER-ADVICE ");
  31. this.audience.applause();
  32. }catch(Exception exception){
  33. System.out.println(" AFTER-THROWING ");
  34. System.out.println(exception.getMessage());
  35. this.audience.demandRefund();
  36. throw exception;
  37. }
  38.  
  39. return obj;
  40. }
  41.  
  42.  
  43. }
  44.  
  45.  
  46.  
  47.  
  48. <?xml version="1.0" encoding="UTF-8"?>
  49. <beans xmlns="http://www.springframework.org/schema/beans"
  50. xmlns:p="http://www.springframework.org/schema/p"
  51. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52. xmlns:context="http://www.springframework.org/schema/context"
  53. xmlns:aop="http://www.springframework.org/schema/aop"
  54. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  55. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  56. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  57. <bean id="performer-inj" class="com.jal.Performer" scope="prototype" />
  58.  
  59. <bean id="audience-inj" class="com.jal.Audience" scope="prototype" />
  60.  
  61. <bean id="audienceAroundAdvice-inj"
  62. class="com.jal.AudienceAroundAdvice"
  63. scope="prototype">
  64. <property name="audience" ref="audience-inj" />
  65. </bean>
  66.  
  67. <bean id="performance-pointcut-inj"
  68. class="org.springframework.aop.aspectj.AspectJExpressionPointcut"
  69. scope="prototype">
  70. <property name="expression" value="execution(* com.jal.Performer+.perform(..))" />
  71. </bean>
  72.  
  73. <bean id="audienceAdvisor-inj-1"
  74. class="org.springframework.aop.support.DefaultPointcutAdvisor"
  75. scope="prototype">
  76. <property name="pointcut" ref="performance-pointcut-inj" />
  77. <property name="advice" ref="audienceAroundAdvice-inj" />
  78. </bean>
  79.  
  80. <bean id="aa-inj"
  81. class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
  82. scope="singleton">
  83. <property name="advisorBeanNamePrefix" value="audienceAdvice-inj-1" />
  84. </bean>
  85.  
  86. </beans>

Report this snippet  

You need to login to post a comment.