SpringAOP基本操作

首先创建一个Maven Web项目

 

 

 

 最后点击finish mavenweb项目就创建好了

创建好后src/main下不存在java和resources文件夹需要手动创建

 设置好后在pom.xml中导入spring和aspectjweaver依赖

Aop需要spring-aop和aspectjweaver依赖支持

<!--   spring-webmvc 可以导入spring全部核心jar包     -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.4</version>
    </dependency>

创建applicationContext.xml配置service的bean

<?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="userService" class="com.wang.service.impl.UserServiceImpl"/>

 
</beans>
        

创建service层模拟一个添加方法

package com.wang.service;

public interface UserService {
    public void add();
}
package com.wang.service.impl;

import com.wang.service.UserService;

public class UserServiceImpl implements UserService {
    @Override
    public void add() {
        System.out.println("增加了一个用户");

    }
}

在applicationContext.xml中加入aop的注解支持并注册AnnotationPointCut的bean

<bean id="annotationPointCut" class="com.wang.diy.AnnotationPointCut"/>
    
    <aop:aspectj-autoproxy/>

创建一个AnnotationPointCut类

package com.wang.diy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

//方式三:使用注解方式实现AOP
@Aspect //标注这个类是一个切面
public class AnnotationPointCut {

    @Before("execution(* com.wang.service.impl.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("方法执行前");
    }
    @After("execution(* com.wang.service.impl.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("方法执行后");
    }
    //在环绕增强中,可以给定一个参数,代表要获取处理切入的点
    @Around("execution(* com.wang.service.impl.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕前");
        Signature signature = jp.getSignature();//获得签名
        System.out.println("执行了"+signature+"方法");
        //执行方法
        Object proceed = jp.proceed();
        System.out.println("返回的结果为:"+proceed);
        System.out.println("环绕后");
    }
}

创建一个测试AOP类

package com.wang;

import com.wang.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAOP {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
}

测试成功

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring AOP(面向切面编程)是 Spring 框架的一个模块,它提供了一种在程序运行期间动态代理类的机制,以便能够在不修改原始代码的情况下,实现诸如日志记录、性能统计、安全控制等横向关注点的功能。Spring AOP 基于代理模式实现,通过代理对象包装目标对象,从而实现在目标方法执行前、执行后、执行异常、执行返回时等时刻,插入一些额外的逻辑。下面是 Spring AOP基本操作: 1. 定义切面类:切面类包含了一系列的通知(Advice),通知描述了切面类在何时执行某个操作。 2. 定义切入点:切入点指定了哪些类的哪些方法会被切面类的通知所拦截。 3. 定义通知:通知是切面类中的方法,它描述了切面类在拦截到切入点处的程序执行时,应该执行的操作。Spring AOP 提供了五种类型的通知: - Before:在目标方法执行前执行通知。 - After:在目标方法执行后执行通知。 - AfterReturning:在目标方法执行后返回结果时执行通知。 - AfterThrowing:在目标方法抛出异常时执行通知。 - Around:在目标方法执行前后都执行通知。 4. 配置切面:切面需要在 Spring 的配置文件中进行配置,以便将切面类与切入点关联。 5. 启用 AOP:启用 AOP 需要在 Spring 的配置文件中配置 <aop:aspectj-autoproxy /> 标签。 通过以上基本操作,就可以在 Spring 中使用 AOP 实现对目标对象的拦截和增强。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AqrJan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值