15,Spring - aop(基于xml实现aop)

在这里插入图片描述

1,xml实现aop代码演示

1.1,导入aop需要的maven

 <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.9</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.7</version>
        </dependency>
    </dependencies>

1.2,新建 UserService

package com.study.service;

public interface UserService {
    void show1();

    void show2();
}

1.2,新建 UserServiceImpl

package com.study.service.impl;

import com.study.service.UserService;

public class UserServiceImpl implements UserService {
    public void show1() {
        System.out.println("UserServiceImpl -- show1");
    }

    public void show2() {
        System.out.println("UserServiceImpl -- show2");
    }
}

1.3,新建增强类 MyAdvice

package com.study.advice;

//增强类 内部提供增强方法
public class MyAdvice {
    public void beforeAdvice(){
        System.out.println("前置增强");
    }

    public void afterAdvice(){
        System.out.println("后置增强");
    }
}

1.4,新建 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"
       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-2.5.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="userService" class="com.study.service.impl.UserServiceImpl"></bean>

<!--    配置通知类-->
    <bean id="myAdvice" class="com.study.advice.MyAdvice"></bean>

<!--    aop配置-->
    <aop:config>
<!--        配置切点表达式,目的是指定那些方法被增强-->
        <aop:pointcut id="myPointcut" expression="execution(void com.study.service.impl.UserServiceImpl.show1())"/>
<!--        配置织入:配置那些切点和那些通知结合-->
        <aop:aspect ref="myAdvice">
            <aop:before method="beforeAdvice" pointcut-ref="myPointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

1.5,新建测试类 AspectDemo

package com.study.demo;

import com.study.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AspectDemo {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService)context.getBean("userService");
        userService.show1();
    }
}

在这里插入图片描述

2 xml配置aop配置详解

在这里插入图片描述
在这里插入图片描述

2.1 修改配置文件

<?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-2.5.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="userService" class="com.study.service.impl.UserServiceImpl"></bean>

<!--    配置通知类-->
    <bean id="myAdvice" class="com.study.advice.MyAdvice"></bean>

<!--    aop配置-->
    <aop:config>
<!--        配置切点表达式,目的是指定那些方法被增强-->
        <aop:pointcut id="myPointcut" expression="execution(void com.study.service..UserServiceImpl.show1(..))"/>
<!--        配置织入:配置那些切点和那些通知结合-->
        <aop:aspect ref="myAdvice">
            <aop:before method="beforeAdvice" pointcut-ref="myPointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

3 aop的五种通知类型

在这里插入图片描述

3.1 后置通知

3.1.1 修改配置文件

<?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-2.5.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="userService" class="com.study.service.impl.UserServiceImpl"></bean>

<!--    配置通知类-->
    <bean id="myAdvice" class="com.study.advice.MyAdvice"></bean>

<!--    aop配置-->
    <aop:config>
<!--        配置切点表达式,目的是指定那些方法被增强-->
        <aop:pointcut id="myPointcut" expression="execution(void com.study.service..UserServiceImpl.show1(..))"/>
        <!--        配置织入:配置那些切点和那些通知结合-->
        <aop:aspect ref="myAdvice">
            <!--前置通知-->
            <aop:before method="beforeAdvice" pointcut-ref="myPointcut"/>
            <!--后置通知-->
            <aop:after-returning method="afterAdvice" pointcut-ref="myPointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

在这里插入图片描述

3.2 环绕通知

3.2.1 修改MyAdvice

package com.study.advice;

import org.aspectj.lang.ProceedingJoinPoint;

//增强类 内部提供增强方法
public class MyAdvice {
    public void beforeAdvice(){
        System.out.println("前置增强");
    }

    public void afterAdvice(){
        System.out.println("后置增强");
    }

    public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("环绕前增强");
        //环绕目标方法
        //执行目标方法
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("环绕后增强");
        return proceed;
    }
}

3.2.2 修改配置文件

<?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-2.5.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="userService" class="com.study.service.impl.UserServiceImpl"></bean>

<!--    配置通知类-->
    <bean id="myAdvice" class="com.study.advice.MyAdvice"></bean>

<!--    aop配置-->
    <aop:config>
<!--        配置切点表达式,目的是指定那些方法被增强-->
        <aop:pointcut id="myPointcut" expression="execution(void com.study.service..UserServiceImpl.show1(..))"/>
        <!--        配置织入:配置那些切点和那些通知结合-->
        <aop:aspect ref="myAdvice">
            <!--前置通知-->
            <aop:around method="aroundAdvice" pointcut-ref="myPointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

在这里插入图片描述

3.3 异常抛出通知

3.3.1 修改 MyAdvice

package com.study.advice;

import org.aspectj.lang.ProceedingJoinPoint;

//增强类 内部提供增强方法
public class MyAdvice {
    public void beforeAdvice(){
        System.out.println("前置增强");
    }

    public void afterAdvice(){
        System.out.println("后置增强");
    }

    public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("环绕前增强");
        //环绕目标方法
        //执行目标方法
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("环绕后增强");
        return proceed;
    }

    public void afterThrowingAdvice(){
        System.out.println("异常抛出通知 报异常时执行");
    }
}

3.3.2 修改 UserServiceImpl

package com.study.service.impl;

import com.study.service.UserService;

public class UserServiceImpl implements UserService {
    public void show1() {
        int i = 1/0;
        System.out.println("UserServiceImpl -- show1");
    }

    public void show2() {
        System.out.println("UserServiceImpl -- show2");
    }
}

3.3.3 修改配置文件

<?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-2.5.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="userService" class="com.study.service.impl.UserServiceImpl"></bean>

<!--    配置通知类-->
    <bean id="myAdvice" class="com.study.advice.MyAdvice"></bean>

<!--    aop配置-->
    <aop:config>
<!--        配置切点表达式,目的是指定那些方法被增强-->
        <aop:pointcut id="myPointcut" expression="execution(void com.study.service.impl.UserServiceImpl.show1())"/>
<!--        配置织入:配置那些切点和那些通知结合-->
        <aop:aspect ref="myAdvice">
            <aop:around method="aroundAdvice" pointcut-ref="myPointcut"/>
            <aop:after-throwing method="afterThrowingAdvice" pointcut-ref="myPointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值