├── .classpath
├── .project
├── .settings
└── org.eclipse.jdt.core.prefs
├── bin
├── applicationContext.xml
└── com
│ └── abc
│ ├── advice
│ ├── AccessArgAdviceTest.class
│ ├── AdviceTest.class
│ ├── AfterAdviceTest.class
│ ├── AfterReturningAdviceTest.class
│ ├── AfterThrowingAdviceTest.class
│ ├── AroundAdviceTest.class
│ └── BeforeAdviceTest.class
│ ├── main
│ └── AOPTest.class
│ └── service
│ └── AdviceManager.class
├── lib
├── aopalliance-1.0.jar
├── aspectjrt.jar
├── aspectjweaver.jar
├── commons-logging-1.0.4.jar
├── spring-aop-3.2.4.RELEASE.jar
├── spring-beans-3.2.4.RELEASE.jar
├── spring-context-3.2.4.RELEASE.jar
├── spring-core-3.2.4.RELEASE.jar
└── spring-expression-3.2.4.RELEASE.jar
└── src
├── applicationContext.xml
└── com
└── abc
├── advice
├── AccessArgAdviceTest.java
├── AdviceTest.java
├── AfterAdviceTest.java
├── AfterReturningAdviceTest.java
├── AfterThrowingAdviceTest.java
├── AroundAdviceTest.java
└── BeforeAdviceTest.java
├── main
└── AOPTest.java
└── service
└── AdviceManager.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | SpringAOP
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.7
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.source=1.7
12 |
--------------------------------------------------------------------------------
/bin/applicationContext.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/bin/com/abc/advice/AccessArgAdviceTest.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/bin/com/abc/advice/AccessArgAdviceTest.class
--------------------------------------------------------------------------------
/bin/com/abc/advice/AdviceTest.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/bin/com/abc/advice/AdviceTest.class
--------------------------------------------------------------------------------
/bin/com/abc/advice/AfterAdviceTest.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/bin/com/abc/advice/AfterAdviceTest.class
--------------------------------------------------------------------------------
/bin/com/abc/advice/AfterReturningAdviceTest.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/bin/com/abc/advice/AfterReturningAdviceTest.class
--------------------------------------------------------------------------------
/bin/com/abc/advice/AfterThrowingAdviceTest.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/bin/com/abc/advice/AfterThrowingAdviceTest.class
--------------------------------------------------------------------------------
/bin/com/abc/advice/AroundAdviceTest.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/bin/com/abc/advice/AroundAdviceTest.class
--------------------------------------------------------------------------------
/bin/com/abc/advice/BeforeAdviceTest.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/bin/com/abc/advice/BeforeAdviceTest.class
--------------------------------------------------------------------------------
/bin/com/abc/main/AOPTest.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/bin/com/abc/main/AOPTest.class
--------------------------------------------------------------------------------
/bin/com/abc/service/AdviceManager.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/bin/com/abc/service/AdviceManager.class
--------------------------------------------------------------------------------
/lib/aopalliance-1.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/lib/aopalliance-1.0.jar
--------------------------------------------------------------------------------
/lib/aspectjrt.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/lib/aspectjrt.jar
--------------------------------------------------------------------------------
/lib/aspectjweaver.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/lib/aspectjweaver.jar
--------------------------------------------------------------------------------
/lib/commons-logging-1.0.4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/lib/commons-logging-1.0.4.jar
--------------------------------------------------------------------------------
/lib/spring-aop-3.2.4.RELEASE.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/lib/spring-aop-3.2.4.RELEASE.jar
--------------------------------------------------------------------------------
/lib/spring-beans-3.2.4.RELEASE.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/lib/spring-beans-3.2.4.RELEASE.jar
--------------------------------------------------------------------------------
/lib/spring-context-3.2.4.RELEASE.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/lib/spring-context-3.2.4.RELEASE.jar
--------------------------------------------------------------------------------
/lib/spring-core-3.2.4.RELEASE.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/lib/spring-core-3.2.4.RELEASE.jar
--------------------------------------------------------------------------------
/lib/spring-expression-3.2.4.RELEASE.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/lib/spring-expression-3.2.4.RELEASE.jar
--------------------------------------------------------------------------------
/src/applicationContext.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/src/com/abc/advice/AccessArgAdviceTest.java:
--------------------------------------------------------------------------------
1 | package com.abc.advice;
2 |
3 | import java.util.Date;
4 | import org.aspectj.lang.annotation.AfterReturning;
5 | import org.aspectj.lang.annotation.Aspect;
6 |
7 | @Aspect
8 | public class AccessArgAdviceTest {
9 | @AfterReturning(
10 | pointcut="execution(* com.abc.service.*.access*(..)) && args(time, name)",
11 | returning="returnValue")
12 | public void access(Date time, Object returnValue, String name) {
13 | System.out.println("目标方法中的参数String = " + name);
14 | System.out.println("目标方法中的参数Date = " + time);
15 | System.out.println("目标方法的返回结果returnValue = " + returnValue);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/com/abc/advice/AdviceTest.java:
--------------------------------------------------------------------------------
1 | package com.abc.advice;
2 |
3 | import java.util.Arrays;
4 |
5 | import org.aspectj.lang.JoinPoint;
6 | import org.aspectj.lang.ProceedingJoinPoint;
7 | import org.aspectj.lang.annotation.After;
8 | import org.aspectj.lang.annotation.AfterReturning;
9 | import org.aspectj.lang.annotation.Around;
10 | import org.aspectj.lang.annotation.Aspect;
11 | import org.aspectj.lang.annotation.Before;
12 |
13 | @Aspect
14 | public class AdviceTest {
15 | @Around("execution(* com.abc.service.*.many*(..))")
16 | public Object process(ProceedingJoinPoint point) throws Throwable {
17 | System.out.println("@Around:执行目标方法之前...");
18 | //访问目标方法的参数:
19 | Object[] args = point.getArgs();
20 | if (args != null && args.length > 0 && args[0].getClass() == String.class) {
21 | args[0] = "改变后的参数1";
22 | }
23 | //用改变后的参数执行目标方法
24 | Object returnValue = point.proceed(args);
25 | System.out.println("@Around:执行目标方法之后...");
26 | System.out.println("@Around:被织入的目标对象为:" + point.getTarget());
27 | return "原返回值:" + returnValue + ",这是返回结果的后缀";
28 | }
29 | @Before("execution(* com.abc.service.*.many*(..))")
30 | public void permissionCheck(JoinPoint point) {
31 | System.out.println("@Before:模拟权限检查...");
32 | System.out.println("@Before:目标方法为:" + point.getSignature().getDeclaringTypeName() +
33 | "." + point.getSignature().getName());
34 | System.out.println("@Before:参数为:" + Arrays.toString(point.getArgs()));
35 | System.out.println("@Before:被织入的目标对象为:" + point.getTarget());
36 | }
37 | @AfterReturning(pointcut="execution(* com.abc.service.*.many*(..))", returning="returnValue")
38 | public void log(JoinPoint point, Object returnValue) {
39 | System.out.println("@AfterReturning:模拟日志记录功能...");
40 | System.out.println("@AfterReturning:目标方法为:" + point.getSignature().getDeclaringTypeName() +
41 | "." + point.getSignature().getName());
42 | System.out.println("@AfterReturning:参数为:" + Arrays.toString(point.getArgs()));
43 | System.out.println("@AfterReturning:返回值为:" + returnValue);
44 | System.out.println("@AfterReturning:被织入的目标对象为:" + point.getTarget());
45 |
46 | }
47 | @After("execution(* com.abc.service.*.many*(..))")
48 | public void releaseResource(JoinPoint point) {
49 | System.out.println("@After:模拟释放资源...");
50 | System.out.println("@After:目标方法为:" + point.getSignature().getDeclaringTypeName() +
51 | "." + point.getSignature().getName());
52 | System.out.println("@After:参数为:" + Arrays.toString(point.getArgs()));
53 | System.out.println("@After:被织入的目标对象为:" + point.getTarget());
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/com/abc/advice/AfterAdviceTest.java:
--------------------------------------------------------------------------------
1 | package com.abc.advice;
2 |
3 | import org.aspectj.lang.annotation.After;
4 | import org.aspectj.lang.annotation.Aspect;
5 |
6 | @Aspect
7 | public class AfterAdviceTest {
8 | @After(value="execution(* com.abc.service.*.afterAdvice*(..))")
9 | public void releaseResource() {
10 | System.out.println("模拟释放数据库连接");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/com/abc/advice/AfterReturningAdviceTest.java:
--------------------------------------------------------------------------------
1 | package com.abc.advice;
2 |
3 | import org.aspectj.lang.annotation.AfterReturning;
4 | import org.aspectj.lang.annotation.Aspect;
5 |
6 | @Aspect
7 | public class AfterReturningAdviceTest {
8 | //匹配com.abc.service下的类中以afterReturning开始的方法
9 | @AfterReturning(returning="returnValue", pointcut="execution(* com.abc.service.*.afterReturning(..))")
10 | public void log(Object returnValue){
11 | System.out.println("目标方法返回值:" + returnValue);
12 | System.out.println("模拟日志记录功能...");
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/com/abc/advice/AfterThrowingAdviceTest.java:
--------------------------------------------------------------------------------
1 | package com.abc.advice;
2 |
3 | import org.aspectj.lang.annotation.AfterThrowing;
4 | import org.aspectj.lang.annotation.Aspect;
5 |
6 | @Aspect
7 | public class AfterThrowingAdviceTest {
8 | @AfterThrowing(throwing="ex",pointcut="execution(* com.abc.service.*.afterThrow*(..))")
9 | public void handleException(Throwable ex) {
10 | System.out.println("目标方法抛出异常:" + ex);
11 | System.out.println("模拟异常处理");
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/com/abc/advice/AroundAdviceTest.java:
--------------------------------------------------------------------------------
1 | package com.abc.advice;
2 |
3 | import java.util.Arrays;
4 |
5 | import org.aspectj.lang.ProceedingJoinPoint;
6 | import org.aspectj.lang.annotation.Around;
7 | import org.aspectj.lang.annotation.Aspect;
8 |
9 | @Aspect
10 | public class AroundAdviceTest {
11 | @Around(value="execution(* com.abc.service.*.around*(..))")
12 | public Object process(ProceedingJoinPoint point) throws Throwable {
13 | System.out.println("模拟执行目标方法前的增强处理:事务开始...");
14 | //修改目标方法的参数
15 | Object[] params = new Object[]{"param1", 2, "param3"};
16 | System.out.println("拦截的方法: " + point.getSignature().getDeclaringTypeName() +
17 | "." + point.getSignature().getName());
18 | System.out.println("参数: " + Arrays.toString(point.getArgs()));
19 | //执行目标方法,并保存目标方法执行后的返回值
20 | Object returnValue = point.proceed(params);
21 | System.out.println("模拟执行目标方法后的增强处理:事务结束...");
22 | //返回修改后的返回值
23 | return "方法实际返回值:" + returnValue + ",这是返回值的后缀";
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/com/abc/advice/BeforeAdviceTest.java:
--------------------------------------------------------------------------------
1 | package com.abc.advice;
2 |
3 | import org.aspectj.lang.annotation.Aspect;
4 | import org.aspectj.lang.annotation.Before;
5 |
6 | @Aspect
7 | public class BeforeAdviceTest {
8 | //匹配com.abc.service下的类中以before开始的方法
9 | @Before("execution(* com.abc.service.*.before*(..))")
10 | public void PermissionCheck() {
11 | System.out.println("模拟权限检查");
12 | }
13 | }
--------------------------------------------------------------------------------
/src/com/abc/main/AOPTest.java:
--------------------------------------------------------------------------------
1 | package com.abc.main;
2 |
3 | import java.util.Date;
4 |
5 | import org.springframework.context.ApplicationContext;
6 | import org.springframework.context.support.ClassPathXmlApplicationContext;
7 |
8 | import com.abc.service.AdviceManager;
9 |
10 | @SuppressWarnings("resource")
11 | public class AOPTest {
12 | public static void main(String[] args) {
13 | ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
14 | AdviceManager manager = context.getBean(AdviceManager.class);
15 | //manager.beforeAdvice();
16 | //manager.afterReturning();
17 | //manager.afterThrowing();
18 | //manager.afterAdvice();
19 | //String result = manager.aroundAdvice("aa", 1, "bb");
20 | //System.out.println("返回值:" + result);
21 | //String result = manager.manyAdvices("aa", "bb");
22 | //System.out.println("Test方法中调用切点方法的返回值:" + result);
23 | manager.accessAdvice(new Date(), "test");
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/com/abc/service/AdviceManager.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanxiaolong/SpringAOPDemo/4efaa3a667dd903e43abed1fff2d2ef6f57ba5ff/src/com/abc/service/AdviceManager.java
--------------------------------------------------------------------------------