Spring-SpringAOP使用XML配置的方式实现切面

一、 导入相关的Jar包

spring-aop、aspectjweaver、spring-context、junit

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.zbt</groupId>
  <artifactId>spring_aop</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>spring_aop</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.6.RELEASE</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
  </build>
</project>

二、编写切面

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优先执行吗?)

猜你喜欢

转载自blog.csdn.net/makeliwei1/article/details/81455637