Spring17_基于XML的AOP开发10

一、快速入门

 1. 导入 AOP 相关坐标
 2. 创建目标接口和目标类(内部有切点)
 3. 创建切面类(内部有增强方法)
 4. 将目标类和切面类的对象创建权交给spring
 5. 在applicationContext.xml中配置织入关系
 6. 测试代码

 代码实现:

  新建一个module:itheima_spring_aop

  1. 导入 AOP 相关坐标

   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.itheima</groupId>
    <artifactId>itheima_spring_aop</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- 导入spring的context坐标,context依赖aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.22.RELEASE</version>
        </dependency>
        <!-- aspectj的织入 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.19</version>
        </dependency>
    </dependencies>
</project>

  2. 创建目标接口和目标类(内部有切点)

package com.itheima.aop;

public interface TargetInterface {

    public void save();
}
package com.itheima.aop;

public class Target implements TargetInterface {
    public void save() {
        System.out.println("save running ...");
    }
}

  3. 创建切面类(内部有增强方法)

package com.itheima.aop;

public class MyAspect {

    public void before(){
        System.out.println("前置增强......");
    }
}

  4. 将目标类和切面类的对象创建权交给spring

   resources->applicationContext.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"       
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置目标类,目标对象--> <bean id="target" class="com.itheima.aop.Target"></bean> <!--配置切面类,切面对象--> <bean id="myAspect" class="com.itheima.aop.MyAspect"></bean> </beans>

  5. 在applicationContext.xml中配置织入关系

   导入aop命名空间

<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">

   配置切点表达式和前置增强的织入关系

    <!--配置织入:告诉spring框架 哪些方法(切点)需要进行哪些增强(前置、后置...) -->
    <aop:config>
        <!--引用myAspect的Bean为切面对象,即声明切面-->
        <aop:aspect ref="myAspect">
            <!--配置Target的save方法执行时要进行myAspect的before方法前置增强,即切面:切点+通知-->
            <aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.save())"></aop:before>
        </aop:aspect>
    </aop:config>

   6. 测试代码

   itheima_spring_aop模块下src->test->java包下新建测试类

package com.itheima.test;

import com.itheima.aop.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {

    @Autowired
    private TargetInterface target;

    @Test
    public void test1(){
        target.save();
    }
}

   先把applicationContext.xml中配置织入关系的代码注掉,执行test1,检查控制台输出:save running ...

   把applicationContext.xml中配置织入关系代码加上,执行test1,检查控制台输出:

    

二、XML配置AOP详解

 1. 通知的类型

  通知的配置语法:

<aop:通知类型 method=“切面类中方法名” pointcut=“切点表达式"></aop:通知类型>

  

  代码实现:

  a. 前置通知、后置通知

package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect {

    public void before(){
        System.out.println("前置增强......");
    }

    public void afterReturning(){
        System.out.println("后置通知");
    }
}
<?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="target" class="com.itheima.aop.Target"></bean>
    <bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>
    <aop:config>
        <aop:aspect ref="myAspect">
            <aop:before method="before" pointcut="execution(public void com.itheima.aop.*.*(..))"></aop:before>
            <aop:after-returning method="afterReturning" pointcut="execution(public void com.itheima.aop.*.*(..))"></aop:after-returning>
        </aop:aspect>
    </aop:config>
</beans>

   执行AopTest类的test1,检查控制台输出:

    

  b. 环绕通知

package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect {

    public void before(){
        System.out.println("前置增强......");
    }

    public void afterReturning(){
        System.out.println("后置通知");
    }

    //ProceedingJoinPoint 正在执行的连接点=切点
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强");
        //切点方法
        Object proceed = pjp.proceed();
        System.out.println("环绕后增强");
        return proceed;
    }
}
<?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="target" class="com.itheima.aop.Target"></bean>
    <bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>
    <aop:config>
        <aop:aspect ref="myAspect">
            <aop:around method="around" pointcut="execution(public void com.itheima.aop.*.*(..))"></aop:around>
        </aop:aspect>
    </aop:config>
</beans>

   执行AopTest类的test1,检查控制台输出:

    

  c. 异常抛出通知

   增加方法执行时出现异常执行异常抛出通知方法,后置通知不会执行。

package com.itheima.aop;

public class Target implements TargetInterface {

    public void save() {
        int i = 1/0;
        System.out.println("save running ...");
    }
}
package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect {

    //ProceedingJoinPoint 正在执行的连接点=切点
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强");
        //切点方法
        Object proceed = pjp.proceed();
        System.out.println("环绕后增强");
        return proceed;
    }

    public void afterThrowing(){
        System.out.println("异常抛出异常...");
    }

}
<?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="target" class="com.itheima.aop.Target"></bean>
    <bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>
    <aop:config>
        <aop:aspect ref="myAspect">
            <aop:around method="around" pointcut="execution(public void com.itheima.aop.*.*(..))"></aop:around>
            <aop:after-throwing method="afterThrowing" pointcut="execution(public void com.itheima.aop.*.*(..))"></aop:after-throwing>
        </aop:aspect>
    </aop:config>
</beans>

   执行AopTest类的test1,检查控制台输出,可以看到没有输出save running,因为异常是在输出语句之前就抛出了

    

package com.itheima.aop;

public class Target implements TargetInterface {
    public void save() {
        System.out.println("save running ...");
        int i = 1/0;
    }
}

   执行AopTest类的test1,检查控制台输出,可以看到输出save running了

    

  d. 最终通知

   不管增强方法执行是否有异常都会执行。

package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect {

    //ProceedingJoinPoint 正在执行的连接点=切点
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强");
        //切点方法
        Object proceed = pjp.proceed();
        System.out.println("环绕后增强");
        return proceed;
    }

    public void afterThrowing(){
        System.out.println("异常抛出异常...");
    }

    public void after(){
        System.out.println("最终增强...");
    }
}
<?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="target" class="com.itheima.aop.Target"></bean>
    <bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>
    <aop:config>
        <aop:aspect ref="myAspect">
            <aop:around method="around" pointcut="execution(public void com.itheima.aop.*.*(..))" />
            <aop:after-throwing method="afterThrowing" pointcut="execution(public void com.itheima.aop.*.*(..))" />
            <aop:after method="after" pointcut="execution(public void com.itheima.aop.*.*(..))" />
        </aop:aspect>
    </aop:config>
</beans>

   执行AopTest类的test1,检查控制台输出,可以看到虽然抛出了异常,最终增强还是执行了。

    

2. 切点表达式的写法

  表达式语法:

execution([修饰符] 返回值类型 包名.类名.方法名(参数))
  • 访问修饰符可以省略
  • 返回值类型、包名、类名、方法名可以使用星号* 代表任意
  • 包名与类名之间一个点 . 代表当前包下的类,两个点 .. 表示当前包及其子包下的类
  • 参数列表可以使用两个点 .. 表示任意个数,任意类型的参数列表

  例如:

execution(public void com.itheima.aop.Target.save())    
execution(void com.itheima.aop.Target.*(..)  表示Target类下的任意方法
execution(* com.itheima.aop.*.*(..))         表示aop包下的任意类下的任意方法,最常用
execution(* com.itheima.aop..*.*(..))        表示aop包及其子包下的任意类的任意方法,即aop包下有a包,a包下的任意类的任意方法也会被增强
execution(* *..*.*(..))             意义不大

 3. 切点表达式的抽取

  当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽取后的切点表达式。

<?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="target" class="com.itheima.aop.Target"></bean>
    <bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>
    <aop:config>
        <aop:aspect ref="myAspect">
            <aop:pointcut id="myPointCut" expression="execution(public void com.itheima.aop.*.*(..))"/>
            <aop:around method="around" pointcut-ref="myPointCut" />
            <aop:after method="after" pointcut-ref="myPointCut" />
        </aop:aspect>
    </aop:config>
</beans>
package com.itheima.aop;

public class Target implements TargetInterface {
    public void save() {
        System.out.println("save running ...");
    }
}

  执行AopTest类的test1,检查控制台输出

   

三、知识要点

  • aop织入的配置
<aop:config>    
    <aop:aspect ref=“切面类”>        
        <aop:before method=“通知方法名称” pointcut=“切点表达式"></aop:before>
    </aop:aspect>
</aop:config>
  • 通知的类型:前置通知、后置通知、环绕通知、异常抛出通知、最终通知

  • 切点表达式的写法:
execution([修饰符] 返回值类型 包名.类名.方法名(参数))

 

posted on 2023-05-05 18:53  花溪月影  阅读(6)  评论(0编辑  收藏  举报