Spring AOP开发

文章介绍了如何使用XML配置和注解方式在Spring框架中实现AOP编程,包括前置、后置、环绕和最终增强方法。在Cooker实体类中模拟了厨师烹饪过程,通过抛出数学异常模拟菜做糊的情况。在XML配置中详细设置了切入点和增强方法,而在注解方式中,利用@Aspect和@Pointcut等注解实现相同的功能。最后提供了测试代码来验证AOP的实现。
摘要由CSDN通过智能技术生成

题目要求:设计一个cooker实体类,其中属性name与rank,分别表示厨师的姓名与等级,使用注入赋值,自行设计cook()方法的内容,以cook()方法作为目标方法(即被增强方法)。

  1. 使用前置增强给出“烹饪前请准备食材。。。”的通知;
  2. 后置增强给出“烹饪后请盛入盘中。。。”的通知;
  3. 使用环绕增强实现上述两项功能;
  4. 在cook()方法中设计一种异常(数学异常)来模拟菜做糊了,并会发出“天哪,菜做糊了。。。”的通知;
  5. 使用最终增强来发出“刷洗锅具。。。”的通知。

控制台输出参考示例如图1:

 

1.使用xml配置方式进行AOP编程

1.导入AOP坐标

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--         aspectj的织入 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
  

2.创建目标接口和目标类

//接口
public interface CookerImpl {
    void cook();
}
import DAO.CookerImpl;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

//目标类
public class Cooker  implements CookerImpl {
    private String name;
    private int rank;

    public Cooker() {
    }

    public Cooker(String name, int rank) {
        this.name = name;
        this.rank = rank;
    }
    @Override
    public void cook(){
        System.out.println("我是厨师"+this.name+","+this.rank+"级厨师,现在开始烹饪");
//        System.out.println(1/0);
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "Cooker{" +
                "name='" + name + '\'' +
                ", rank=" + rank +
                '}';
    }
}

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

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

//切面类
public class MyAspect {

    //前置增强方法
    public void before(){
        System.out.println("烹饪前准备食材。。。");
    }

    //后置增强方法
    public void afterReturn()  {
        System.out.println("烹饪后请盛入盘中。。。");
    }
    //环绕通知
    //需要一个切点对象(Proceeding:执行 JoinPoint:连接点)  正在执行的切点 = 切点
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("烹饪前准备食材。。。");
        Object proceed = pjp.proceed(); //执行切点方法(抛出异常)
        System.out.println("烹饪后请盛入盘中。。。");
        return proceed;
    }
    //最终增强方法
    public void after(){
        System.out.println("刷洗锅具。。。");
    }
    //异常
    public void afterThrowing(){
        try{
            System.out.println("天哪,菜做糊了。。。");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

4.配置beans.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: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="cooker" class="pojo.Cooker">
                <constructor-arg name="name" value="铁根" />
                <constructor-arg name="rank" value="4"/>
        </bean>
        <bean id="myAspect" class="pojo.MyAspect"></bean>
        <aop:config>
                <!--引用myAspect的Bean为切面对象-->
<!--                <aop:aspect ref="myAspect">-->
<!--                        &lt;!&ndash;配置Cooker的cook方法执行时要进行myAspect的before方法前置增强&ndash;&gt;-->
<!--                        <aop:before method="before" pointcut="execution(public void pojo.Cooker.cook())"/>-->
<!--                </aop:aspect>-->
<!--                <aop:aspect ref="myAspect">-->
<!--                        &lt;!&ndash;配置Cooker的cook方法执行时要进行myAspect的afterReturn方法后置增强&ndash;&gt;-->
<!--                        <aop:after method="afterReturn" pointcut="execution(public void pojo.Cooker.cook())"/>-->
<!--                </aop:aspect>-->
                <aop:aspect ref="myAspect">
                        <!--配置Cooker的cook方法执行时要进行myAspect的after方法最终增强-->
                        <aop:after-returning method="after" pointcut="execution(public void pojo.Cooker.cook())"/>
                </aop:aspect>
                <aop:aspect ref="myAspect">
                        <!--配置Cooker的cook方法执行时要进行myAspect的afterThrowing方法异常增强-->
                        <aop:after-throwing method="afterThrowing" pointcut="execution(* pojo.Cooker.cook())"/>
                </aop:aspect>
                <aop:aspect ref="myAspect">
                        <!--配置Cooker的cook方法执行时要进行myAspect的around方法环绕增强-->
                        <aop:around method="around" pointcut="execution(public void pojo.Cooker.cook())"/>
                </aop:aspect>
        </aop:config>
</beans>

 5.测试

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.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:beans.xml")
public class AOPTest {
    @Autowired
    CookerImpl cooker;

    @Test
    public void AopTest() {
        cooker.cook();
    }
}

学习链接

(5条消息) 16. 用XML 配置的方式 进行 AOP 切面编程_咸瑜的博客-CSDN博客 

 2.使用注解方式方式进行AOP编程

1.导包

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--         aspectj的织入 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.19</version>
        </dependency>

接口和实现类和上面的一样 

2.切面类

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
//切面类
public class MyAspect {
    @Pointcut("execution(* pojo.Cooker.cook())")
    public void pointcut(){}

    //前置增强方法
    @Before("pointcut()")
    public void before(){
        System.out.println("烹饪前准备食材。。。");
    }

    //后置增强方法
    @AfterReturning("pointcut()")
    public void afterReturn()  {
        System.out.println("烹饪后请盛入盘中。。。");
    }
    //环绕通知
    //需要一个切点对象(Proceeding:执行 JoinPoint:连接点)  正在执行的切点 = 切点
//    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("烹饪前准备食材。。。");
        Object proceed = pjp.proceed(); //执行切点方法(抛出异常)
        System.out.println("烹饪后请盛入盘中。。。");
        return proceed;
    }
    //最终增强方法
    @After("pointcut()")
    public void after(){
        System.out.println("刷洗锅具。。。");
    }
    //异常
    @AfterThrowing("pointcut()")
    public void afterThrowing(){
        try{
            System.out.println("天哪,菜做糊了。。。");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

 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: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.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="cooker" class="pojo.Cooker">
        <constructor-arg name="name" value="铁根" />
        <constructor-arg name="rank" value="4"/>
    </bean>
    <!-- 开启注解扫描:指定扫描哪个包下的注解 -->
    <context:component-scan base-package="DAO,pojo"/>
    <!-- 开启 AOP 注解方式 -->
    <aop:aspectj-autoproxy />
</beans>

 我看了很多文章的内容,可能会出现以下错误,

Caused by: org.xml.sax.SAXParseException; lineNumber: 25; columnNumber: 9; cvc-complex-type.2.3: 元素 ‘beans’ 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”。

 原因是你导入的包context和beans.xml文件中的引用不相同,只需要把以下内容删除,在自动扫描的那里自动加载就可以了,也有可能是其他原因,

 4.测试

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.SpringRunner;


@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:beans.xml")
public class AOPTest {
    @Autowired
    CookerImpl cooker;

    @Test
    public void AopTest() {
        cooker.cook();
    }
}

 学习链接放下:

(5条消息) Aop编程——注解方式_枫叶的博客-CSDN博客 

(5条消息) Spring 通过注解方式实现AOP切面编程_白天的猫头鹰的博客-CSDN博客 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值