Spring AOP学习笔记(三)-SpringAOP使用XML配置的方式实现切面

一、基于Schema的方式实现切面编程

1.1、Aspect 的声明

基于Schema的声明包括两部分,第一部分是Aspect定义,也就是编写一个类;第二部分就是将Aspect配置到容器当中
定义Aspect

//Aspect一
public class Cousin {
}
//Aspect二
public class Cousin {
}

将Aspect配置到容器中

    <aop:config>
        //Aspect二
        <aop:aspect id="cousinAspect" ref="cousin">
        </aop:aspect>
        //Aspect一
        <aop:aspect id="interviewerAspect" ref="interviewer">
        </aop:aspect>
    </aop:config>
1.2、PointCut的声明

PointCut 的声明有两种方法,第一种方法是在aop-config下使用aop-pointcut声明,这种方式可以让所有的Aspect的Advice共用;第二种方法是声明在aop-aspect当中,这种方式只能有当前的Aspect的Advice使用。
方式一:在aop-config下声明pointcut

<aop:config>
        <aop:pointcut id="IandCAspect" expression="execution(* com.zbt.day02.Experience.intershipInterview(..))">
        <aop:aspect id="cousinAspect" ref="cousin">           
        </aop:aspect>
        <aop:aspect id="interviewerAspect" ref="interviewer">
        </aop:aspect>
    </aop:config>

方式二:在aop-aspect中声明Pointcut

    <aop:config>
        <aop:aspect id="cousinAspect" ref="cousin">
            <aop:pointcut id="" expression="execution(* com.zbt.day02.Experience.intershipInterview(..))">  
        </aop:aspect>
        <aop:aspect id="interviewerAspect" ref="interviewer">
            <aop:pointcut id="" expression="execution(* com.zbt.day02.Experience.intershipInterview(..))">
        </aop:aspect>
    </aop:config>
1.3、Advice的声明

Advice的声明主要分为两部分,第一部分是在Aspect定义Advice,第二部分是在配置到容器中。

    <aop:config>
        <aop:aspect id="cousinAspect" ref="cousin">
            <aop:pointcut id="" expression="execution(* com.zbt.day02.Experience.intershipInterview(..))">
            <aop:before point-ref="cousinAspect" method="dinner"/>
            <aop:after pointcut="execution(* com.zbt.day02.Experience.priorityRecruitment(..))" method="dinner"/>
        </aop:aspect>
        <aop:aspect id="interviewerAspect" ref="interviewer">
            <aop:pointcut id="" expression="execution(* com.zbt.day02.Experience.intershipInterview(..))">
        </aop:aspect>
    </aop:config>

二、编写切面

2.1、切面一:Cousin
package com.zbt.day02;

public class Cousin {
    public void dinner(){
        System.out.println("表哥请我吃晚饭");
    }
}
2.2、切面二:Interviewer
package com.zbt.day02;

public class Interviewer {
    public void notice(){
        System.out.println("不好意思,公司没有符合你的岗位");
    }
}

三、编写切点

package com.zbt.day02;

public class Experience {
    public void intershipInterview(){
        System.out.println("参加实习生面试");
    }
    public void priorityRecruitment(){
        System.out.println("参加优招面试");
    }
}

四、使用XML配置切面

<?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:context="http://www.springframework.org/schema/context"
       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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="experience" class="com.zbt.day02.Experience"></bean>
    <bean id="cousin" class="com.zbt.day02.Cousin"></bean>
    <bean id="interviewer" class="com.zbt.day02.Interviewer"></bean>
    <aop:config>
        <aop:aspect id="cousinAspect" ref="cousin">
            <aop:before pointcut="execution(* com.zbt.day02.Experience.intershipInterview(..))" method="dinner"/>
            <aop:after pointcut="execution(* com.zbt.day02.Experience.priorityRecruitment(..))" method="dinner"/>
        </aop:aspect>

        <aop:aspect id="interviewerAspect" ref="interviewer">
            <aop:after pointcut="execution(* com.zbt.day02.Experience.intershipInterview(..))" method="notice"/>
            <aop:after pointcut="execution(* com.zbt.day02.Experience.priorityRecruitment(..))" method="notice"/>
        </aop:aspect>
    </aop:config>
</beans>

注意我们这里没有使用下面的自动代理方案,从Spring的官方文档中描述:aop-config配置模式已经heavy use 了auto_proxy机制,因此我们对于如果我们使用aop:config的方式的话,那么我们就不需要使用下面的方式开启自动代理了,两种方式最好二选一。

<aop:aspectj-autoproxy/>

五、测试

package com.zbt.day02;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestExperience {
    @Test
    public void testExp(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationConfig.xml");
        Experience experience = context.getBean("experience",Experience.class);
        experience.intershipInterview();
        experience.priorityRecruitment();
    }
}

输出结果

表哥请我吃晚饭
参加实习生面试
不好意思,公司没有符合你的岗位
参加优招面试
不好意思,公司没有符合你的岗位
表哥请我吃晚饭

从输出结果来看没有错,我在调用Experience.internshipInterview()方法之前执行了Cousin.dinner()方法在Experience.internshipInterview()方法之后执行了Interviewer.notic()方法。在调用Experience.priorityRecruitment()方法执行以后执行了Cousin.dinner()方法和Interviewer.notic()方法。但是我们不嫰确定我们哪一个通知应该优先执行。(?难道不能自己确定在after之后那个advice优先执行吗?)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值