基于xml的AOP开发

基于xml的AOP开发

1.快速入门

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

配置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>org.example</groupId>
    <artifactId>Spring_AOP</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.8.RC3</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>

    </dependencies>

</project>

创建工程包com.AOP,将jdk包下的Target和TargetInterface粘贴过来,在创建文件myAspect

package com.AOP;

public class myAspect {

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

}

在resource下创建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: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.AOP.Target"></bean>
    <!--切面对象-->
    <bean id="myAspect" class="com.AOP.myAspect"></bean>
    <!--配置织入:告诉spring框架,哪些方法(切点)需要进行那些增强(前置、后置)-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
        <!--切面:切点+通知-->
            <aop:before method="before" pointcut="execution(public void com.AOP.Target.save())"></aop:before>
        </aop:aspect>
    </aop:config>

</beans>

创建测试文件,在AOP包下创建文件AopTest

package com.AOP;

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();
    }
}

运行test1方法即可
2.xml配置AOP详解
2.1.切点表达式写法
在这里插入图片描述
2.2.通知类型
在这里插入图片描述
修改myAspect文件

package com.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;
    }

}

配置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: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.AOP.Target"></bean>
    <!--切面对象-->
    <bean id="myAspect" class="com.AOP.myAspect"></bean>
    <!--配置织入:告诉spring框架,哪些方法(切点)需要进行那些增强(前置、后置)-->
    <aop:config>
        <!--声明切面-->
        <aop:aspect ref="myAspect">
        <!--切面:切点+通知-->
            <aop:before method="before" pointcut="execution(public void com.AOP.Target.save())"></aop:before>
            <aop:after-returning method="afterReturning" pointcut="execution(public void com.AOP.Target.save())"></aop:after-returning>
            <aop:around method="around" pointcut="execution(public void com.AOP.Target.save())"></aop:around>
        </aop:aspect>
    </aop:config>

</beans>

运行test1
2.3.切点表达式的抽取
在这里插入图片描述

修改applicationContext配置

<?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.AOP.Target"></bean>
    <!--切面对象-->
    <bean id="myAspect" class="com.AOP.myAspect"></bean>
    <!--配置织入:告诉spring框架,哪些方法(切点)需要进行那些增强(前置、后置)-->
    <aop:config>
    <!--声明切面-->
        <aop:aspect ref="myAspect">
    <!--抽取切点表达式-->
        <aop:pointcut id="myPoint" expression="execution(* com.AOP.*.*(..))"/>

        <aop:around method="around" pointcut-ref="myPoint"/>
        <aop:after-returning method="afterReturning" pointcut-ref="myPoint"/>
        </aop:aspect>
    </aop:config>

</beans>

运行test1
3.知识要点
在这里插入图片描述

XML轻松开发WEB站点(PDF,有源代码)本版书是一本专门介绍如何用XML开发Web网站的书。XML(eXtensible Markup Language) 作为网络发展新一代可扩展标识语言,被誉为“继Java之后最激动人心的技术”。在HTML语言已为广大网络编程者熟悉的现在,随着网络的成长,需要更方便和完美的技术来满足个人设计的需要。 与HTML相比较,XML具有更强的兼容性和扩展性。用XML书写的文档更简洁也更易于执行,使网络时代又走向了新的境界——全面包容、开放的程序设计。 全书由六部分(二十三章)组成。第一部分(XML基础),内容包括:什么是XML语言,新一代网络编程语言XMLXML的应用与发展前景,XML工具;第二部分(XML的语言基础),内容包括:XML语法,XML链接语言,XML指针语言;第三部分(XML的实践),内容包括:创建XML文档,创建属于自己的DTD,内容与形式的结合——XSL,XML DOM技术,同步多媒体合成语言SMIL,ASP与XML的联合开发;第四部分(XML高阶),内容包括:XML在数据库中的应用,XML中的矢量图形处理技术,WML——无线接入的XMLXMLJava;第五部分(基于XML的Web站点应用与开发),内容包括:WIDL自动控制Web站点,频道定义格式推送Web站点,Web站点的设计实现;第六部分(相关协议与标准),内容包括:可扩展标识语言1.0((第二版)规范,XML术语与词汇参考和XML技术动态等。本版书具有技术内涵高、指导性强,内容新颖、丰富,涉及面广,范例实用性和可操作性强的特点。本版书不但是从事网站开发与设计的广大从业人员重要的指导书,同时也是高校相关专业师生教学、自学参考书和社会相关培训班推荐教材、各科研院所图书馆馆藏读物。 本光盘内容包括本版书中部分实例程序的源文件和本版电子书。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值