首页 > 其他分享 >Spring17_基于XML的AOP开发10

Spring17_基于XML的AOP开发10

时间:2023-05-05 19:00:15浏览次数:37  
标签:XML 10 xml spring Spring17 aop AOP public itheima

一、快速入门

 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>

 

   

 

标签:XML,10,xml,spring,Spring17,aop,AOP,public,itheima
From: https://www.cnblogs.com/ajing2018/p/17375114.html

相关文章

  • 信奥赛题1105:数组逆序重存放
    新奥赛一本通,题11051105:数组逆序重存放时间限制:1000ms         内存限制:65536KB提交数:70600                通过数:47540【题目描述】将一个数组中的值按逆序重新存放。例如,原来的顺序为8,6,5,4,1。要求改为1,4,5,6,8。【输入】两行:第......
  • UVA11107 Life Forms
    怎么没有正常的后缀数组二分的题解啊给定\(n\)个字符串,求出最长的,在多于\(\left\lfloor\frac{n}{2}\right\rfloor\)个字符串中出现的子串,并按照字典序从小到大输出。\(n\leq100\),\(|S|\leq1000\),根据套路可以将所有字符串连成一个,不同的字符串用特殊符号隔开,然后建出新......
  • Xilinx Artix-7系列 FPGA器件XC7A100T-1FGG484I、XC7A200T-L2FFG1156E现场可编程门阵
    产品简介:Xilinx®Artix-7系列FPGA重新定义了成本敏感型解决方案,功耗比上一代产品降低了一半,同时为高带宽应用提供一流的收发器和信号处理能力。这些设备基于28纳米HPL工艺构建,提供一流的性能功耗比。与MicroBlaze™软处理器一起,Artix-7FPGA非常适用于便携式医疗设备、......
  • 3个月,从功能测试进阶到自动化测试涨薪10k,我悟了....
    ​因为我最近在分享自动化测试技术,经常被问到:功能测试想转自动化,请问应该怎么入手?有没有好的资源推荐?那么,接下来我就结合自己的经历聊一聊我是如何在工作中做自动化测试的。(学习路线和网盘资源放在文末了)3个月是怎么从功能测试进阶到自动化测试并且涨薪呢?今天我们就来聊一聊一......
  • node:internal/modules/cjs/loader:1078 throw err; ^ Error: Cannot find module
    终端运行.js文件,之后又报错了,node:internal/modules/cjs/loader:1078throwerr;^......
  • STM32单片机引脚要职能配置为输入或者输出模式,并不能像51一样准双向,那么如何进行但总
    如题随便找个端口举例对应的程序为 难道需要写之后立即初始化为输入?然后赶紧读?然后再赶紧初始化为输出?再往外写?是的,还真他妈就是这么傻逼的操作 ......
  • 在Ubuntu18.04安装Freeswitch1.10
    在Ubuntu18.04安装Freeswitch1.10一、版本选择二、Ubuntu的安装1、修改Ubuntu镜像(1)进入配置的目录,并进行配置文件备份(2)打开和修改源三、Freeswitch安装1、添加第三方源2、添加第三方源3、安装部分依赖包4、配置使用gawk5、编译安装程序依赖包(1)安装cmake(2)安装libks(3)安装signalwire-......
  • LeetCode 1049. 最后一块石头的重量 II
    思路任何时刻,某个石头的重量永远都是若干石头加减运算的绝对值如a-b+c合并石头都是减法,但仍可能出现+运算符,如a-(b-c)=a-b+c任何一种合并方法,最后一个石头的重量都可以表示成一种代数形式,如a+b-c+d+e+f-g不是所有的代数形式都可以转换为一种合并方法,如a+b+c因此......
  • ETF2100/5900 Introductory Econometrics
    ETF2100/5900IntroductoryEconometricsAssignment2—ACaseStudyontheBirth-weightofBabyImportantnotes:1.Thisisanindividualassignment.Thisassignmentisworth20%ofthisunit’stotalmark.Markswillbedeductedforlatesubmissiononthefoll......
  • 5G组网方案:2大方案(NSA和SA)、5系、10选项,看完秒懂!
    本文来源:鲜枣课堂,原标题:关于5G的NSA和SA,看完秒懂!由“物联网时空”微信公众号(IoT-LPWAN)整理发布,转载请注明来源。 第一个5G正式标准马上就要发布了,相信大家一定都在翘首企盼。之前我曾经和大家介绍过,去年12月份的时候,我们其实已经发布了“半个”5G标准。是的没错,那个时候是“非独......