转自 http://www.iteye.com/topic/40725
Before Advice
代码
1. package test3;
2. public class MessageWriter {
3. public void writeMessage() {
4. System.out.print("World");
5. }
6. }
7.
8. package test3;
9. import java.lang.reflect.Method;
10. import org.springframework.aop.MethodBeforeAdvice;
11. import org.springframework.aop.framework.ProxyFactory;
12. public class SimpleBeforeAdvice implements MethodBeforeAdvice{
13. public void before(Method method,Object[] args,Object target) throws Throwable{
14. System.out.println("Before method:"+method.getName());
15. }
16. public static void main(String[] args){
17. MessageWriter target = new MessageWriter();
18. ProxyFactory pf = new ProxyFactory();
19. pf.addAdvice(new SimpleBeforeAdvice());
20. pf.setTarget(target);
21. MessageWriter proxy = (MessageWriter)pf.getProxy();
22. proxy.writeMessage();
23. }
24. }
AfterReturningAdvice
代码
1. package test3;
2. import java.lang.reflect.Method;
3. import org.springframework.aop.AfterReturningAdvice;
4. import org.springframework.aop.framework.ProxyFactory;
5. public class SimpleAfterReturningAdvice implements AfterReturningAdvice{
6. public void afterReturning(Object returnValue,Method method,Object[] args,Object target){
7. System.out.println("");
8. System.out.println("After method:"+method.getName());
9. }
10. public static void main(String[] args) {
11. MessageWriter target = new MessageWriter();
12. ProxyFactory pf = new ProxyFactory();
13. pf.addAdvice(new SimpleAfterReturningAdvice());
14. pf.setTarget(target);
15. MessageWriter proxy = (MessageWriter)pf.getProxy();
16. proxy.writeMessage();
17. }
18. }
Around Advice
代码
1. package test3;
2. import org.aopalliance.intercept.MethodInterceptor;
3. import org.aopalliance.intercept.MethodInvocation;
4. public class MessageDecorator implements MethodInterceptor{
5. public Object invoke(MethodInvocation invocation) throws Throwable{
6. System.out.print("Hello");
7. Object retVal = invocation.proceed();
8. System.out.println("!");
9. return retVal;
10. }
11. }
12.
13. package test3;
14. import org.springframework.aop.framework.ProxyFactory;
15. public class HelloWorldAOPExample {
16. public static void main(String[] args) {
17. MessageWriter target=new MessageWriter();
18. ProxyFactory pf = new ProxyFactory();
19. pf.addAdvice(new MessageDecorator());
20. pf.setTarget(target);
21. MessageWriter proxy = (MessageWriter)pf.getProxy();
22. target.writeMessage();
23. System.out.print(" ");
24. proxy.writeMessage();
25. }
26. }
ThrowsAdvice
代码
1. package test3;
2. public class ErrorBean {
3. public void errorProneMethod() throws Exception{
4. throw new Exception("Foo");
5. }
6. public void otherErrorProneMethod() throws IllegalArgumentException{
7. throw new IllegalArgumentException("Bar");
8. }
9. }
10.
11. package test3
12. import java.lang.reflect.Method;
13. import org.springframework.aop.ThrowsAdvice;
14. import org.springframework.aop.framework.ProxyFactory;
15.
16. public class SimpleThrowsAdvice implements ThrowsAdvice{
17. public static void main(String[] args) throws Exception {
18. ErrorBean errorBean = new ErrorBean();
19. ProxyFactory pf = new ProxyFactory();
20. pf.setTarget(errorBean);
21. pf.addAdvice(new SimpleThrowsAdvice());
22. ErrorBean proxy = (ErrorBean)pf.getProxy();
23. try
24. {
25. proxy.errorProneMethod();
26. }catch(Exception ignored)
27. {
28. //ignored.printStackTrace();
29. }
30. try
31. {
32. proxy.otherErrorProneMethod();
33. }catch(Exception ignored)
34. {
35. //ignored.printStackTrace();
36. }
37. }
38. public void afterThrowing(Exception ex) throws Throwable{
39. System.out.println("***");
40. System.out.println("Generic Exception Capture");
41. System.out.println("Caught: "+ex.getClass().getName());
42. System.out.println("***\n");
43. }
44. public void afterThrowing(Method method,Object[] args,Object target,IllegalArgumentException ex) throws Throwable{
45. System.out.println("***");
46. System.out.println("IllegalArgumentException Capture");
47. System.out.println("Caught: "+ex.getClass().getName());
48. System.out.println("***\n");
49. }
50. }
StaticPointcut
代码
1. package test3;
2. public class BeanOne {
3. public void foo() {
4. System.out.println("foo");
5. }
6. public void bar() {
7. System.out.println("bar");
8. }
9. }
10.
11. package test3;
12. public class BeanTwo {
13. public void foo() {
14. System.out.println("foo");
15. }
16. public void bar() {
17. System.out.println("bar");
18. }
19. }
20.
21. package test3;
22.
23. import org.aopalliance.intercept.MethodInterceptor;
24. import org.aopalliance.intercept.MethodInvocation;
25. public class SimpleAdvice implements MethodInterceptor{
26. public Object invoke(MethodInvocation invocation) throws Throwable
27. {
28. System.out.println(">> Invoking "+invocation.getMethod().getName());
29. Object retVal = invocation.proceed();
30. System.out.println(">> Done");
31. return retVal;
32. }
33. }
34.
35. package test3;
36.
37. import java.lang.reflect.Method;
38. import org.springframework.aop.ClassFilter;
39. import org.springframework.aop.support.StaticMethodMatcherPointcut;
40. public class SimpleStaticPointcut extends StaticMethodMatcherPointcut {
41. public boolean matches(Method method, Class cls) {
42. return ("foo".equals(method.getName()));
43. }
44. public ClassFilter getClassFilter() {
45. return new ClassFilter() {
46. public boolean matches(Class cls) {
47. return (cls == BeanOne.class);
48. }
49. };
50. }
51. }
52.
53. package test3;
54. import org.aopalliance.aop.Advice;
55. import org.springframework.aop.Advisor;
56. import org.springframework.aop.Pointcut;
57. import org.springframework.aop.framework.ProxyFactory;
58. import org.springframework.aop.support.DefaultPointcutAdvisor;
59.
60. public class StaticPointcutExample {
61. public static void main(String[] args) {
62. BeanOne one = new BeanOne();
63. BeanTwo two = new BeanTwo();
64.
65. BeanOne proxyOne;
66. BeanTwo proxyTwo;
67.
68. Pointcut pc = new SimpleStaticPointcut();
69. Advice advise = new SimpleAdvice();
70. Advisor advisor = new DefaultPointcutAdvisor(pc,advise);
71.
72. ProxyFactory pf = new ProxyFactory();
73. pf.addAdvisor(advisor);
74. pf.setTarget(one);
75. proxyOne = (BeanOne)pf.getProxy();
76.
77. pf = new ProxyFactory();
78. pf.addAdvisor(advisor);
79. pf.setTarget(two);
80. proxyTwo = (BeanTwo)pf.getProxy();
81.
82. proxyOne.foo();
83. proxyTwo.foo();
84.
85. proxyOne.bar();
86. proxyTwo.bar();
87. }
88. }
Dynamic pointcut
代码
1. package test3;
2. public class SampleBean {
3. public void foo(int x){
4. System.out.println("Invoked foo() with: "+x);
5. }
6. public void bar(){
7. System.out.println("Invoked bar()");
8. }
9. }
10.
11. package test3;
12.
13. import java.lang.reflect.Method;
14. import org.springframework.aop.ClassFilter;
15. import org.springframework.aop.support.DynamicMethodMatcherPointcut;
16. public class SimpleDynamicPointcut extends DynamicMethodMatcherPointcut{
17. public boolean matches(Method method, Class cls) {
18. System.out.println("Static check for " + method.getName());
19. return ("foo".equals(method.getName()));
20. }
21. public boolean matches(Method method, Class cls, Object[] args) {
22. System.out.println("Dynamic check for " + method.getName());
23. int x = ((Integer) args[0]).intValue();
24. return (x != 100);
25. }
26. public ClassFilter getClassFilter() {
27. return new ClassFilter() {
28. public boolean matches(Class cls) {
29. return (cls == SampleBean.class);
30. }
31. };
32. }
33. }
34.
35. package test3;
36.
37. import org.springframework.aop.Advisor;
38. import org.springframework.aop.framework.ProxyFactory;
39. import org.springframework.aop.support.DefaultPointcutAdvisor;
40. public class DynamicPointcutExample {
41. public static void main(String[] args) {
42. SampleBean target = new SampleBean();
43. Advisor advisor = new DefaultPointcutAdvisor(new SimpleDynamicPointcut(),new SimpleAdvice());
44.
45. ProxyFactory pf = new ProxyFactory();
46. pf.setTarget(target);
47. pf.addAdvisor(advisor);
48. SampleBean proxy = (SampleBean)pf.getProxy();
49.
50. proxy.foo(1);
51. proxy.foo(10);
52. proxy.foo(100);
53.
54. proxy.bar();
55. proxy.bar();
56. proxy.bar();
57. }
58. }
NamePointcut
代码
1. package test3;
2. public class NameBean {
3. public void foo() {
4. System.out.println("foo");
5. }
6. public void foo(int x) {
7. System.out.println("foo " + x);
8. }
9. public void bar() {
10. System.out.println("bar");
11. }
12. public void yup() {
13. System.out.println("yup");
14. }
15. }
16.
17. package test3;
18.
19. import org.aopalliance.intercept.MethodInterceptor;
20. import org.aopalliance.intercept.MethodInvocation;
21.
22. public class SimpleAdvice implements MethodInterceptor{
23. public Object invoke(MethodInvocation invocation) throws Throwable
24. {
25. System.out.println(">> Invoking "+invocation.getMethod().getName());
26. Object retVal = invocation.proceed();
27. System.out.println(">> Done");
28. return retVal;
29. }
30. }
31.
32. package test3;
33. import org.springframework.aop.Advisor;
34. import org.springframework.aop.framework.ProxyFactory;
35. import org.springframework.aop.support.DefaultPointcutAdvisor;
36. import org.springframework.aop.support.NameMatchMethodPointcut;
37.
38. public class NamePointcutExample {
39. public static void main(String[] args) {
40. NameBean target = new NameBean();
41. NameMatchMethodPointcut pc = new NameMatchMethodPointcut();
42. pc.addMethodName("foo");
43. pc.addMethodName("bar");
44. Advisor advisor = new DefaultPointcutAdvisor(pc,new SimpleAdvice());
45. ProxyFactory pf = new ProxyFactory();
46. pf.setTarget(target);
47. pf.addAdvisor(advisor);
48. NameBean proxy = (NameBean)pf.getProxy();
49.
50. proxy.foo();
51. proxy.foo(999);
52. proxy.bar();
53. proxy.yup();
54. }
55. }
NamePointcutAdvisor
代码
1. package test3;
2. import org.springframework.aop.framework.ProxyFactory;
3. import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;
4.
5. public class NamePointcutUsingAdvisor {
6. public static void main(String[] args) {
7. NameBean target = new NameBean();
8.
9. NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor(new SimpleAdvice());
10.
11. advisor.addMethodName("foo");
12. advisor.addMethodName("bar");
13. ProxyFactory pf = new ProxyFactory();
14. pf.setTarget(target);
15. pf.addAdvisor(advisor);
16. NameBean proxy = (NameBean)pf.getProxy();
17. proxy.foo();
18. proxy.foo(999);
19. proxy.bar();
20. proxy.yup();
21. }
22. }
RegexpPointcut
代码
1. package test3;
2. public class RegexpBean {
3. public void foo1() {
4. System.out.println("foo1");
5. }
6. public void foo2() {
7. System.out.println("foo2");
8. }
9. public void bar() {
10. System.out.println("bar");
11. }
12. }
13.
14. package test3;
15. import org.springframework.aop.Advisor;
16. import org.springframework.aop.framework.ProxyFactory;
17. import org.springframework.aop.support.DefaultPointcutAdvisor;
18. import org.springframework.aop.support.JdkRegexpMethodPointcut;
19. public class RegexpPointcutExample {
20. public static void main(String[] args) {
21. RegexpBean target = new RegexpBean();
22. JdkRegexpMethodPointcut pc = new JdkRegexpMethodPointcut();
23. pc.setPattern(".*foo.*");
24. Advisor advisor = new DefaultPointcutAdvisor(pc,new SimpleAdvice());
25. ProxyFactory pf = new ProxyFactory();
26. pf.setTarget(target);
27. pf.addAdvisor(advisor);
28. RegexpBean proxy = (RegexpBean)pf.getProxy();
29. proxy.foo1();
30. proxy.foo2();
31. proxy.bar();
32. }
33. }
ControlFlow Pointcut
代码
1. package test4;
2.
3. public class TestBean {
4. public void foo() {
5. System.out.println("foo()");
6. }
7. }
8.
9. package test4;
10. import java.lang.reflect.Method;
11. import org.springframework.aop.MethodBeforeAdvice;
12. public class SimpleBeforeAdvice implements MethodBeforeAdvice {
13. public void before(Method method,Object[] args,Object target) throws Throwable
14. {
15. System.out.println("before method: "+method);
16. }
17. }
18.
19. package test4;
20. import org.springframework.aop.Advisor;
21. import org.springframework.aop.Pointcut;
22. import org.springframework.aop.framework.ProxyFactory;
23. import org.springframework.aop.support.ControlFlowPointcut;
24. import org.springframework.aop.support.DefaultPointcutAdvisor;
25.
26. public class ControlFlowExample {
27.
28. public static void main(String[] args) {
29. ControlFlowExample ex = new ControlFlowExample();
30. ex.run();
31. }
32. public void run()
33. {
34. TestBean target = new TestBean();
35. Pointcut pc = new ControlFlowPointcut(ControlFlowExample.class,"test");
36. Advisor advisor = new DefaultPointcutAdvisor(pc,new SimpleBeforeAdvice());
37. ProxyFactory pf = new ProxyFactory();
38. pf.setTarget(target);
39. pf.addAdvisor(advisor);
40. TestBean proxy = (TestBean)pf.getProxy();
41. System.out.println("Trying normal invoke");
42. proxy.foo();
43. System.out.println("Trying under ControlFlowExample.test()");
44. test(proxy);
45. }
46. private void test(TestBean bean){
47. bean.foo();
48. }
49. }
ComposablePointcut
代码
1. package test4;
2. public class SampleBean {
3. public String getName() {
4. return "Rob Harrop";
5. }
6. public void setName(String abc) {
7. }
8. public int getAge()
9. {
10. return 100;
11. }
12. }
13.
14. package test4;
15. import java.lang.reflect.Method;
16. import org.springframework.aop.Advisor;
17. import org.springframework.aop.ClassFilter;
18. import org.springframework.aop.framework.ProxyFactory;
19. import org.springframework.aop.support.ComposablePointcut;
20. import org.springframework.aop.support.DefaultPointcutAdvisor;
21. import org.springframework.aop.support.StaticMethodMatcher;
22. public class ComposablePointcutExample {
23. public static void main(String[] args) {
24. SampleBean target = new SampleBean();
25. ComposablePointcut pc = new ComposablePointcut(ClassFilter.TRUE,new GetterMethodMatcher());
26. System.out.println("Test 1");
27. SampleBean proxy = getProxy(pc,target);
28. testInvoke(proxy);
29.
30. System.out.println("Test 2");
31. pc.union(new SetterMethodMatcher());
32. proxy = getProxy(pc,target);
33. testInvoke(proxy);
34.
35. System.out.println("Test 3");
36. pc.intersection(new GetAgeMethodMatcher());
37. proxy = getProxy(pc,target);
38. testInvoke(proxy);
39. }
40.
41. private static SampleBean getProxy(ComposablePointcut pc,SampleBean target)
42. {
43. Advisor advisor = new DefaultPointcutAdvisor(pc,new SimpleBeforeAdvice());
44.
45. ProxyFactory pf = new ProxyFactory();
46. pf.setTarget(target);
47. pf.addAdvisor(advisor);
48.
49. return (SampleBean)pf.getProxy();
50. }
51.
52. private static void testInvoke(SampleBean proxy)
53. {
54. proxy.getAge();
55. proxy.getName();
56. proxy.setName("Rob Harrop");
57. }
58.
59. private static class GetterMethodMatcher extends StaticMethodMatcher{
60. public boolean matches(Method method,Class cls)
61. {
62. return (method.getName().startsWith("get"));
63. }
64. }
65.
66. private static class GetAgeMethodMatcher extends StaticMethodMatcher{
67. public boolean matches(Method method,Class cls)
68. {
69. return "getAge".equals(method.getName());
70. }
71. }
72.
73. private static class SetterMethodMatcher extends StaticMethodMatcher{
74. public boolean matches(Method method,Class cls)
75. {
76. return (method.getName().startsWith("set"));
77. }
78. }
79. }
Introduction
代码
1. package test4;
2. public class TargetBean {
3. private String name;
4. public void setName(String name) {
5. this.name = name;
6. }
7. public String getName() {
8. return name;
9. }
10. }
11.
12. package test4;
13. public interface IsModified {
14. public boolean isModified();
15. }
16.
17. package test4;
18.
19. import java.lang.reflect.Method;
20. import java.util.HashMap;
21. import java.util.Map;
22.
23. import org.aopalliance.intercept.MethodInvocation;
24. import org.springframework.aop.support.DelegatingIntroductionInterceptor;
25.
26. public class IsModifiedMixin extends DelegatingIntroductionInterceptor implements IsModified {
27. private boolean isModified=false;
28. private Map methodCache = new HashMap();
29. public boolean isModified()
30. {
31. return isModified;
32. }
33. public Object invoke(MethodInvocation invocation) throws Throwable
34. {
35. if (!isModified)
36. {
37. if ((invocation.getMethod().getName().startsWith("set"))&&(invocation.getArguments().length==1))
38. {
39.
40. Method getter = getGetter(invocation.getMethod());
41.
42. if (getter!=null)
43. {
44. Object newVal = invocation.getArguments()[0];
45. Object oldVal = getter.invoke(invocation.getThis(),null);
46. if ((newVal == null)&&(oldVal == null))
47. {
48. isModified = false;
49. }else if ((newVal == null)&&(oldVal!=null))
50. {
51. isModified = true;
52. }else if ((newVal!=null)&&(oldVal==null))
53. {
54. isModified = true;
55. }else
56. {
57. isModified = (!newVal.equals(oldVal));
58. }
59. }
60. }
61. }
62. return super.invoke(invocation);
63. }
64.
65. private Method getGetter(Method setter)
66. {
67. Method getter = null;
68.
69. getter = (Method)methodCache.get(setter);
70.
71. if (getter!=null)
72. {
73. return getter;
74. }
75.
76. String getterName = setter.getName().replaceFirst("set","get");
77. try
78. {
79. getter = setter.getDeclaringClass().getMethod(getterName,null);
80. synchronized(methodCache)
81. {
82. methodCache.put(setter,getter);
83. }
84. return getter;
85. }catch(NoSuchMethodException ex)
86. {
87. return null;
88. }
89. }
90. }
91.
92. package test4;
93. import org.springframework.aop.support.DefaultIntroductionAdvisor;
94. public class IsModifiedAdvisor extends DefaultIntroductionAdvisor{
95.
96. public IsModifiedAdvisor(){
97. super(new IsModifiedMixin());
98. }
99. }
100.
101. package test4;
102.
103. import org.springframework.aop.IntroductionAdvisor;
104. import org.springframework.aop.framework.ProxyFactory;
105.
106. public class IntroductionExample {
107.
108. public static void main(String[] args) {
109. TargetBean target = new TargetBean();
110. target.setName("Rob Harrop");
111.
112. IntroductionAdvisor advisor = new IsModifiedAdvisor();
113.
114. ProxyFactory pf = new ProxyFactory();
115. pf.setTarget(target);
116. pf.addAdvisor(advisor);
117. pf.setOptimize(true);
118.
119. TargetBean proxy = (TargetBean)pf.getProxy();
120. IsModified proxyInterface = (IsModified)proxy;
121.
122. System.out.println("Is TargetBean?: "+(proxy instanceof TargetBean));
123. System.out.println("Is IsModified?: "+(proxy instanceof IsModified));
124.
125. System.out.println("Has been Modified?: "+proxyInterface.isModified());
126. proxy.setName("Rob Harrop");
127. System.out.println("Has been Modified?: "+proxyInterface.isModified());
128. proxy.setName("Joe Schmoe");
129. System.out.println("Has been Modified?: "+proxyInterface.isModified());
130. }
131. }