AOP(面向切面编程)基于XML方式配置

 概念解释:(理解基本概念方可快速入手)

  1. 连接点(joinpoint)

    被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法。

  2. 切入点(pointcut)

    切入点是指我们要对哪些连接点进行拦截的定义

  3. 通知(advice)

    所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

  4. 切面(aspect)

    是切入点和通知的结合

 通知顺序:

前置通知:aop:before
            后置通知:aop:after-returning【try】
            最终通知:aop:after【finally】
            异常通知:aop:after-throwing【catch】
            环绕通知:aop:around


            try{
                ...
                return aop:after-returning
            }catch(Exception e){
                ...
                aop:after-throwing
            }finally{
                ...
                aop:after
            }

切点表达式:
        格式:execution([修饰符] 返回值 报名.类名.方法名(参数))
        eg:execution(* com.by.service.*.*(..))

 实例演示:

pom.xml:

<dependencies>
        <!--ioc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.12</version>
        </dependency>
        <!--支持切点表达式AOP -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.19</version>
        </dependency>
    </dependencies>

UserDaoImpl:

package com.by.dao;

public class UserDaoImpl implements UserDao {

    @Override
    public void addUser(){
        System.out.println("insert into tb_user......");
    }
}

UserServiceImpl:

package com.by.service;

import com.by.dao.UserDao;

public class UserServiceImpl implements UserService {

    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void addUser(){
        userDao.addUser();
        System.out.println(8/0);
    }
}

MyLogActive:(增强类)

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.advice;

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * <p>Project: Spring - MyLogAdvice</p>
 * <p>Powered by scl On 2024-01-05 15:04:11</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
public class MyLogAdvice {
    public void after() {
        System.out.println("最终通知、、、");
    }

    public void before() {
        System.out.println("前置通知、。、");
    }

    public void afterReturn(){
        System.out.println("后置通知");
    }

    public void afterThrowing(){
        System.out.println("异常通知");
    }

    public void around(ProceedingJoinPoint joinPoint) {
        try {
            System.out.println("前环绕通知。。。");
            joinPoint.proceed();
            System.out.println("后环绕通知。。。");
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}

 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="userDao" class="com.by.dao.UserDaoImpl"></bean>
    <bean id="userService" class="com.by.service.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
    <!--增强-->
    <bean id="myLogAdvice" class="com.by.advice.MyLogAdvice"></bean>
    <!--aop-->
    <aop:config>
        <!--切点-->
        <aop:pointcut id="pointcut" expression="execution(* com.by.service.*.*(..))"/>
        <!--切面-->
        <aop:aspect ref="myLogAdvice">
            <aop:before method="before" pointcut-ref="pointcut"></aop:before>
            <aop:after method="after" pointcut-ref="pointcut"></aop:after>
            <aop:around method="around" pointcut-ref="pointcut"></aop:around>
            <aop:after-returning method="afterReturn" pointcut-ref="pointcut"></aop:after-returning>
            <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"></aop:after-throwing>
         </aop:aspect>
    </aop:config>

</beans>

没增强前结果展示:

增强之后结果展示:

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Spring框架实现AOP面向切面编程的测试代码示例,包括配置文件的内容。 首先,创建一个切面类 `LoggingAspect`,用于定义切面逻辑: ```java import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void beforeAdvice(JoinPoint joinPoint) { System.out.println("Before method: " + joinPoint.getSignature().getName()); } @After("execution(* com.example.service.*.*(..))") public void afterAdvice(JoinPoint joinPoint) { System.out.println("After method: " + joinPoint.getSignature().getName()); } } ``` 然后,创建一个测试服务类 `UserService`,用于演示AOP的应用: ```java import org.springframework.stereotype.Service; @Service public class UserService { public void createUser(String username) { System.out.println("Creating user: " + username); } public void deleteUser(String username) { System.out.println("Deleting user: " + username); } } ``` 接下来,创建Spring配置文件 `applicationContext.xml`,配置AOP和其他相关内容: ```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"> <!-- 开启AOP自动代理 --> <aop:aspectj-autoproxy/> <!-- 声明切面类 --> <bean id="loggingAspect" class="com.example.aspect.LoggingAspect"/> <!-- 声明服务类 --> <bean id="userService" class="com.example.service.UserService"/> </beans> ``` 最后,创建一个测试类 `MainApp`,加载Spring配置文件并使用服务类进行测试: ```java import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = context.getBean(UserService.class); userService.createUser("John"); userService.deleteUser("John"); } } ``` 在上述示例中,`LoggingAspect` 切面类使用 `@Before` 和 `@After` 注解分别定义了在目标方法执行前和执行后的逻辑。切面逻辑会应用于 `UserService` 类中的所有方法。 在Spring配置文件中,通过 `<aop:aspectj-autoproxy/>` 开启了AOP的自动代理功能。然后声明了切面类 `loggingAspect` 和服务类 `userService` 的实例。 运行 `MainApp` 类,将会加载Spring配置文件并执行相应的AOP切面逻辑。 这是一个简单的AOP面向切面编程的测试代码示例,通过配置文件将切面类和服务类注册到Spring容器中,实现AOP的应用。你可以根据实际需求进行更复杂的切面逻辑定义和配置文件的编写。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值