Spring学习(22)--- AOP之Advice应用(下)

(六)Advice parameters(advice带参数的情况)

例子:

修改MyAspect(添加around_init方法):

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.aop.schema;
 
import org.aspectj.lang.ProceedingJoinPoint;
 
/**
*
* 切面类
*
*/
public class MyAspect {
 
    public void before(){
        System.out.println("MyAspect.before");
    }
     
    public void afterreturning(){
        System.out.println("MyAspect.afterreturning");
    }
     
    public void afterthrowing(){
        System.out.println("MyAspect.afterthrowing");
    }
     
    public void after(){
        System.out.println("MyAspect.after");
    }
     
    public void around(ProceedingJoinPoint pjp) {
        try {
            System.out.println("MyAspect.around_1");
            Object obj=pjp.proceed();
            System.out.println("MyAspect.around_2");
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
     
    public void around_init(ProceedingJoinPoint pjp,String name,int age) {
        System.out.println(name+"  "+age);
        try {
            System.out.println("MyAspect.around_1");
            Object obj=pjp.proceed();
            System.out.println("MyAspect.around_2");
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

修改ApsectBiz类(添加init方法):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.aop.schema;
/**
*
* 业务类
*
*/
public class ApsectBiz {
 
    public void biz(){
        System.out.println("ApsectBiz.biz");
        //throw new RuntimeException();  //故意抛出异常
    }
     
    public void init(String name,int age){
        System.out.println("ApsectBiz.init : "+ name +"  " +age);
    }
}

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
30
31
32
<?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"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
          
     <bean id="myAspect" class="com.aop.schema.MyAspect"></bean>
       
     <bean id="apsectBiz" class="com.aop.schema.ApsectBiz"></bean>
       
     <aop:config>
          <aop:aspect id="myAspectAOP" ref="myAspect">
          <!--  先注释掉,便于观察结果
            <aop:pointcut id="myPointcut" expression="execution(* com.aop.schema.ApsectBiz.*(..))" />
            <aop:before method="before" pointcut-ref="myPointcut"/>
            <aop:after-returning method="afterreturning" pointcut-ref="myPointcut"/>
            <aop:after-throwing method="afterthrowing" pointcut-ref="myPointcut"/>
            <aop:after method="after" pointcut-ref="myPointcut"/>
            <aop:around method="around" pointcut-ref="myPointcut"/>
           -->
              
            <aop:around method="around_init" pointcut="execution(* com.aop.schema.ApsectBiz.init(String,int)) and args(name,age)"/>
          </aop:aspect>
     </aop:config>
  
</beans>

单元测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.aop.schema;
 
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class UnitTest {
 
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-aop.xml");
        ApsectBiz biz = (ApsectBiz)context.getBean("apsectBiz");
        biz.init("Json",25);
    }
}

结果:

七月 09, 2015 11:48:42 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@118e0f0f: startup date [Thu Jul 09 23:48:42 CST 2015]; root of context hierarchy
七月 09, 2015 11:48:42 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-aop.xml]
Json  25
MyAspect.around_1
ApsectBiz.init : Json  25
MyAspect.around_2

 

posted @   Json_wangqiang  阅读(355)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2014-07-09 JS中innerHTML,innerText,value
点击右上角即可分享
微信分享提示