Spring学习(七)——AOP小demo

Advice应用

  • aop:after:所指定的切面方法,会在对应类中的方法执行完后再执行。
  • aop:before:所指定的切面方法 会在监控的方法执行前执行;
  • after-returning所指定的切面方法 会在监控方法正常执行结束后执行;
  • after-throwing所指定的切面方法 会在监控方法抛出异常时执行;
  • after所指定的切面方法 会在监控方法执行结束后,无论方法执行时是否有异常抛出,均会执行

xml文件:Beans.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="moocAspect" class="com.test.MoocAspect"></bean>
<bean id="aspectBiz" class="com.test.AspectBiz"></bean>
<aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:pointcut expression="execution(* com.test.AspectBiz.biz(..))" id="moocPointcut"></aop:pointcut>
<aop:before method="before" pointcut-ref="moocPointcut"></aop:before>
<aop:after-returning method="afterReturning" pointcut-ref="moocPointcut"></aop:after-returning>
<aop:after-throwing method="afterThrowing" pointcut-ref="moocPointcut"></aop:after-throwing>
<aop:after method="after" pointcut-ref="moocPointcut"></aop:after>
<aop:around method="around" pointcut-ref="moocPointcut"></aop:around>
<aop:around method="aroundInit" pointcut="execution(* com.test.AspectBiz.init(String,int)) and args(name,times)"></aop:around>
<aop:declare-parents types-matching="com.test.AspectBiz"
implement-interface="com.test.Fit"
default-impl="com.test.FitImpl"/>
</aop:aspect>
</aop:config>
</beans>

切面类:MoocAspect.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.test;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Service;
@Service
public class MoocAspect {
public void before(){
System.out.println("MoocAspect before.");
}
public void afterReturning(){
System.out.println("MoocAspect afterReturning");
}
public void afterThrowing(){
System.out.println("MoocAspect afterThrowing");
}
public void after(){
System.out.println("MoocAspect after");
}
public Object around(ProceedingJoinPoint pjp){
Object obj = null;
try {
System.out.println("MoocAspect around1");
obj = pjp.proceed();
System.out.println("MoocAspect around2");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return obj;
}
public Object aroundInit(ProceedingJoinPoint pjp,String name,int times){
System.out.println("name:"+name+",times:"+times);
Object obj = null;
try {
System.out.println("MoocAspect around1");
obj = pjp.proceed();
System.out.println("MoocAspect around2");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return obj;
}
}

业务类:AspectBiz.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.test;
import org.springframework.stereotype.Service;
@Service
public class AspectBiz {
public void biz(){
System.out.println("AspectBiz biz.");
// throw new RuntimeException();
}
public void init(String name , int times){
System.out.println("AspectBiz init:"+name+";"+times);
}
}

接口:Fit.java

1
2
3
4
5
package com.test;
public interface Fit {
void filter();
}

接口实现:FitImpl.java

1
2
3
4
5
6
7
8
9
package com.test;
public class FitImpl implements Fit{
@Override
public void filter() {
System.out.println("FitImpl filter");
}
}

测试类:TestAOPSchemeAdvice.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import static org.junit.Assert.*;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestAOPSchemeAdvice extends UnitTestBase{
public TestAOPSchemeAdvice(){
super("classpath:Beans.xml");
}
@Test
public void testBiz() {
AspectBiz biz = super.getBean("aspectBiz");
biz.biz();
}
@Test
public void testInit(){
AspectBiz biz = super.getBean("aspectBiz");
biz.init("Kitty",3);
}
@Test
public void testFit(){
Fit fit = (Fit)super.getBean("aspectBiz");
fit.filter();
}
}
您的支持将鼓励我继续创作~