Java企业开发学习笔记(1.5.1)采用配置方式使用AOP

该文章主要为完成实训任务,详细实现过程及结果见【http://t.csdn.cn/FBkpc


一、采用配置方式使用AOP

1.1 创建所需子包

  • cn.kox.spring包里创建day05.aop_xml子包
    在这里插入图片描述

1.2 创建杀龙任务类

  • aop_xml子包里创建杀龙任务类 - SlayDragonQuest
    在这里插入图片描述
package cn.kox.spring.day05.aop_xml;

import org.springframework.stereotype.Component;

/*
* 杀龙任务类
* */
@Component
public class SlayDragonQuest {
    public void embark() {
        System.out.println("执行杀龙任务...");
    }
}

1.3 创建勇敢骑士类

  • aop_xml子包里创建勇敢骑士类 - BraveKnight
    在这里插入图片描述
package cn.kox.spring.day05.aop_xml;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/*
* 勇敢骑士类
* */

@Component("RobinHood")
public class BraveKnight {
    @Value("罗宾汉")
    private String name;
    @Autowired
    private SlayDragonQuest slayDragonQuest;

    public void embarkOnQuest() {
        System.out.print("勇敢骑士[" + name + "]");
        slayDragonQuest.embark();
    }
}

1.4 创建游吟诗人类

  • aop_xml子包里创建游吟诗人类 - Minstrel
    在这里插入图片描述
package cn.kox.spring.day05.aop_xml;

import org.springframework.stereotype.Component;

@Component
public class Minstrel {
    /**
     * 骑士出发前唱赞歌
     */
    public void singBeforeQuest() {
        System.out.println("啦啦啦,骑士出发了~");
    }

    /**
     * 骑士凯旋时唱赞歌
     */
    public void singAfterQuest() {
        System.out.println("真棒啊!骑士完成了任务~");
    }
}

1.5 创建Spring配置文件

  • resources里创建aop_xml目录,在里面创建spring-config.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
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--组件扫描-->
    <context:component-scan base-package="cn.kox.spring.day05.aop_xml"/>

    <!--配置AOP-->
    <aop:config>
        <!--定义切面-->
        <aop:aspect ref="minstrel">
            <!--定义切点-->
            <aop:pointcut id="embark" expression="execution(* cn.kox.spring.day05..*.embarkOnQuest(..))"/>
            <!--声明前置通知-->
            <aop:before method="singBeforeQuest" pointcut-ref="embark"/>
            <!--声明后置通知-->
            <aop:after method="singAfterQuest" pointcut-ref="embark"/>
        </aop:aspect>
    </aop:config>
</beans>

1.6 添加AOP相关依赖

  • 在pom.xml文件里添加AOP相关依赖
    在这里插入图片描述
        <!--Spring AOP-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId> 
            <version>5.3.25</version>
        </dependency>
        <!--AspectJ支持-->
        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.5.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.19</version>
            <scope>runtime</scope>
        </dependency>

1.7 创建测试类 - TestKnight

  • 在test/java里创建cn.kox.spring.day05.aop_xml包,在包里创建TestKnight
    在这里插入图片描述
package cn.kox.spring.day05.aop_xml;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestKnight {
    private ClassPathXmlApplicationContext context; // 基于类路径XML配置文件的应用容器

    @Before
    public void init() {
        // 基于Spring配置文件创建应用容器
        context = new ClassPathXmlApplicationContext("aop_xml/spring-config.xml");
    }

    @Test
    public void testBraveKnight() {
        // 根据名称从应用容器里获取勇敢骑士对象
        BraveKnight braveKnight = (BraveKnight) context.getBean("RobinHood");
        // 勇敢骑士执行任务
        braveKnight.embarkOnQuest();
    }

    @After
    public void destroy() {
        // 关闭应用容器
        context.close();
    }
}

1.8 运行测试方法,查看结果

  • 运行testBraveKinght()方法
    在这里插入图片描述

1.9 练习

1.9.1 创建救美任务类与救美骑士类

  • 创建RescueDamselQuest类和RescueDamselKnight
  • RescueDamselQuest类
package cn.kox.spring.day05.aop_xml;

import org.springframework.stereotype.Component;

/*
* 救美任务类
* */
@Component
public class RescueDamselQuest {
    public void embark() {
        System.out.println("执行救美任务...");
    }
}

  • RescueDamselKnight类
package cn.kox.spring.day05.aop_xml;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/*
* 救美骑士类
* */
@Component("Galahad")
public class RescueDamselKnight {
    @Value("格拉海德")
    private String name;
    @Autowired
    private RescueDamselQuest rescueDamselQuest;

    public void embarkOnQuest() {
        System.out.print("救美骑士[" + name + "]");
        rescueDamselQuest.embark();
    }
}

1.9.2 在测试类里增加测试方法

  • 测试方法 - testRescueDamselKnight()
package cn.kox.spring.day05.aop_xml;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestKnight {
    private ClassPathXmlApplicationContext context; // 基于类路径XML配置文件的应用容器

    @Before
    public void init() {
        // 基于Spring配置文件创建应用容器
        context = new ClassPathXmlApplicationContext("aop_xml/spring-config.xml");
    }

    @Test
    public void testBraveKnight() {
        // 根据名称从应用容器里获取勇敢骑士对象
        BraveKnight braveKnight = (BraveKnight) context.getBean("RobinHood");
        // 勇敢骑士执行任务
        braveKnight.embarkOnQuest();
    }

    @Test
    public void testRescueDamselKnight() {
        RescueDamselKnight rescueDamselKnight = (RescueDamselKnight) context.getBean("Galahad");
        rescueDamselKnight.embarkOnQuest();
    }

    @After
    public void destroy() {
        // 关闭应用容器
        context.close();
    }
}

1.9.3 运行测试类,查看结果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值