基于 XML 的 AOP 配置

本文连接:https://www.cnblogs.com/qzhc/p/11969734.html

接下来我将用一个很简单的实例

1、 环境搭建

1.1、 第一步:准备必要的代码

业务层代码:

AccountServiceImpl.java

package com.henu.service.impl;

import com.henu.service.AccountService;

public class AccountServiceImpl implements AccountService {
    public void saveAccount() {
        System.out.println("执行了保存");
    }
}

测试类:

AOPtest.java

package com.henu.test;

import com.henu.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPtest {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        AccountService as = (AccountService) ac.getBean("accountService");
        as.saveAccount();
    }
}

1.2、导入所需要的jar包

我的demo是用maven管理的: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.henu</groupId>
    <artifactId>day03_spring_adviceType</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
    </dependencies>

</project>

1.3、创建 spring 的配置文件并导入约束和配置spring的ioc

<?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">
    <!--配置spring的Ioc,把service对象配置起来-->

    <bean id="accountService" class="com.henu.service.impl.AccountServiceImpl"></bean>
   

1.4、写一个公共类作为通知

logger.java

package com.henu.utils;
//记录日志
public class Logger {
    /**
     * 前置通知
     * */
    public void befterPrintLog(){
        System.out.println("前置通知logger开始记录日志了");
    }
    /**
     * 后置通知
     * */
    public void afterReturningPrintLog(){
        System.out.println("后置通知logger开始记录日志了");
    }
    /**
     * 异常通知
     * */
    public void afterThrowingPrintLog(){
        System.out.println("异常通知logger开始记录日志了");
    }
    /**
     * 最终通知
     * */
    public void afterPrintLog(){
        System.out.println("最终通知logger开始记录日志了");
    }

}

2、aop配置步骤(先给代码,在讲)

<?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">
    <!--配置spring的Ioc,把service对象配置起来-->

    <bean id="accountService" class="com.henu.service.impl.AccountServiceImpl"></bean>
    <!--

    -->
    <!--配置logger类 -->
    <bean id="logger" class="com.henu.utils.Logger"></bean>
    <!--配置AOP-->
    <aop:config>
        <!--配置切面-->
        <aop:aspect id="logAdvice" ref="logger">

        <!--配置通知的类型,并且建立通知方法和切入点方法的关联-->
            <!--配置前置通知:在切入点方法执行之前-->
            <aop:before method="befterPrintLog" pointcut="execution(* com.henu.service.impl.*.*(..))"></aop:before>
            <!--配置后置通知:在切入点方法正常执行之后。它与异常只执行一个-->
            <aop:after-returning method="afterReturningPrintLog" pointcut="execution(* com.henu.service.impl.*.*(..))"></aop:after-returning>
            <!--配置异常通知:在切入点方法执行产生异常之后。它与后置只执行一个-->
            <aop:after-throwing method="afterThrowingPrintLog" pointcut="execution(* com.henu.service.impl.*.*(..))"></aop:after-throwing>
            <!--配置最终通知:无论切入点是否正常执行,它都会在其后面执行-->
            <aop:after method="afterPrintLog" pointcut="execution(* com.henu.service.impl.*.*(..))"></aop:after>
        </aop:aspect>
    </aop:config>

</beans>

2.1、把通知类用bean标签配置起来

<!--配置logger类 -->
    <bean id="logger" class="com.henu.utils.Logger"></bean>

2.2、使用aop:config声明aop配置

  aop:config:

   作用:用于声明开始 aop 的配置 

    <aop:config>

       <!--配置的代码都写在此处-->        

    </aop:config>

2.3、使用 aop:aspect 配置切面

  

aop:aspect:
  作用:
    用于配置切面。
  属性:
    id:给切面提供一个唯一标识。
    ref:引用配置好的通知类 bean 的 id。
<aop:aspect id="logAdvice" ref="logger">
<!--配置通知的类型要写在此处-->
</aop:aspect>

2.4、使用aop:xxx配置对应的通知类型

     aop:before
        作用:
          用于配置前置通知。指定增强的方法在切入点方法之前执行
        属性:
          method:用于指定通知类中的增强方法名称
          ponitcut-ref:用于指定切入点的表达式的引用
          poinitcut:用于指定切入点表达式
        执行时间点:
          切入点方法执行之前执行
<aop:before method="beginTransaction" pointcut-ref="pt1"/>
        aop:after-returning
        作用:
          用于配置后置通知
        属性:
          method:指定通知中方法的名称。
          pointct:定义切入点表达式
          pointcut-ref:指定切入点表达式的引用
        执行时间点:
          切入点方法正常执行之后。它和异常通知只能有一个执行
<aop:after-returning method="commit" pointcut-ref="pt1"/>
           aop:after-throwing
        作用:
          用于配置异常通知
        属性:
          method:指定通知中方法的名称。
          pointct:定义切入点表达式
          pointcut-ref:指定切入点表达式的引用
        执行时间点:
          切入点方法执行产生异常后执行。它和后置通知只能执行一个
<aop:after-throwing method="rollback" pointcut-ref="pt1"/>
         aop:after
        作用:
          用于配置最终通知
        属性:
          method:指定通知中方法的名称。
          pointct:定义切入点表达式
          pointcut-ref:指定切入点表达式的引用
        执行时间点:
          无论切入点方法执行时是否有异常,它都会在其后面执行。
<aop:after method="release" pointcut-ref="pt1"/>

2.5、切入点表达式说明

execution:匹配方法的执行(常用)
        execution(表达式)
        表达式语法:execution([修饰符] 返回值类型 包名.类名.方法名(参数))
        写法说明:
        全匹配方式:
          public void com.henu.service.impl.AccountServiceImpl.saveAccount()
        访问修饰符可以省略
          void com.itheima.service.impl.AccountServiceImpl.saveAccount()
        返回值可以使用*号,表示任意返回值
          * com.henu.service.impl.AccountServiceImpl.saveAccount()
        包名可以使用*号,表示任意包,但是有几级包,需要写几个*.
          * *.*.*.*.AccountServiceImpl.saveAccount()
        使用..来表示当前包,及其子包
          * com..AccountServiceImpl.saveAccount()
        类名和方法名可以使用*号来实现通配
          * *..*.*()
        参数列表可以使用*,表示参数可以是任意数据类型,但是必须有参数
          * com..*.*(*)
        参数列表可以使用..表示有无参数均可,有参数可以是任意类型
          * com..*.*(..)
        全通配方式:
          * *..*.*(..)
    注:
        通常情况下,我们都是对业务层的方法进行增强,所以切入点表达式都是切到业务层实现类。
        execution(* com.henu.service.impl.*.*(..))   

讲到这里基本都差不多了,少啥以后再补把。

原文地址:https://www.cnblogs.com/qzhc/p/11969734.html

时间: 2024-07-31 08:11:50

基于 XML 的 AOP 配置的相关文章

基于XML的AOP配置

创建spring的配置文件并导入约束 此处要导入aop的约束 <?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://

基于XML的AOP配置-转

http://www.cnblogs.com/yangy608/archive/2010/11/14/1876839.html AOP(Aspect-Oriented Programming,面向切面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入封装.继承和多态性等概念来建立一种对象层次结构,用以模拟公共行为的一个集合.当我们需要为分散的对象引入公共行为的时候,OOP则显得无能为力.也就是说,OOP允许你定义从上到下的关系,但

spring 系列6 基于xml的aop

spring中基于xml的AOP配置步骤 把通知Bean交给spring来管理 使用aop:config标签表明AOP配置 使用aop:aspect标签表明配置切面 id属性:给切面提供一个唯一标识 ref属性:指定通知类bean的id 4.在aop:aspect标签的内部使用对应标签来配置通知的类型 切入点表达式关键字execution(表达式): 访问修饰符 返回类型 包名.包名...类名.方法名(参数列表) 其中: 访问修饰符可以省略 标准表达式写法:public void com.man

Spring : 基于XML Schema的配置(一)

[本教程翻译自Spring官方文档,并有适当增删] (是针对Spring 4.0.6 Release版本的) 基于XML Schema的配置在Spring 2.0开始被引入,并在2.5和3.0版本得到增强和扩展. 转向基于XML Schema的动机是使得Spring XML配置更简单.传统的基于 <bean/>的方法是很好,但它的通用特性带来了很大的配置开销. 从Spring 依赖注入容器的观点来看,一切都是bean.这对Spring 容器是个好消息,因为如果一切都是bean,那么一对象都能以

Spring 框架的概述以及Spring中基于XML的IOC配置

Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器.框架.一站式 优势: 方便解耦:做到编译期不依赖,运行期才依赖 AOP的支持 声明式事务的支持 方便程序的测试 方便整合各种框架 降低JavaEE API的使用难度 Spring源码很厉害 解耦: 耦合包括:类之间的和方法之间的 解决的思路: 在创建对象的时候用反射来创建,而不是new 读取配置文件

面向切面编程AOP:基于XML文件的配置

除了使用AspectJ注解声明切面,Spring也支持在bean的配置文件中声明切面,这种声明是通过aop scheme中的XML元素完成的. 首先建立一个类: package com.sevenhu.AOPTests; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Before; import java.util.Arrays; /** * Created by hu on 2016/4/1. */

spring--声明式事务(包含基于注解和基于xml文件的配置方式)

一.基于注解 步骤如下: 引入jar(mysql驱动,c3p0数据源,spring的必要jar) applicationContext.xml的配置 Service和Dao的类上都加上对应的注解使其在ioc容器中,service的方法上面加上注解@Transactional applicationContext.xml的配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt

阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置

复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置这里就可以删除了 配置注解 使用@Service注解 开始AOP配置 把通知类交给Spring来管理 在Logger上加注解.之类注意,@Service和@Repository都不合适.因为logger属于三层 所以这里用@Component这个注解来配置 写完上面的@Component的注解后.b

disconf实践(三)基于XML的分布式配置文件管理,自动reload

上一篇介绍了基于xml的非自动reload的分布式配置文件管理,这一篇介绍自动reload的方式(基于disconf实践二). 1. 修改RedisConfig.java 1 package org.springinaction.weather.config; 2 3 public class RedisConfig { 4 5 private String host; 6 7 private String port; 8 9 public String getHost() { 10 retur