首页 > 其他分享 >关于AOP的一些理解

关于AOP的一些理解

时间:2023-08-17 10:48:18浏览次数:20  
标签:spring springframework double 理解 关于 AOP org public result

1:什么是AOP

AOP(Aspect Oriented Programming)面向切面思想,是spring三大核心思想之一。AOP:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑各部分进行隔离,从而降低业务逻辑之间的耦合度,提高程序的可重用性,通是提高开发效率。

AOP作为面向切面编程的语言,它可以让你的业务代码和非业务代码进行隔离。在不改变业务代码的前提下,可以增加新的业务代码。

2:为什么使用AOP

Java是一个面向对象(OOP)的编程语言,但它有个弊端就是当需要为多个不具有继承关系的对象引入一个公共行为时,例如日志记录、权限 校验、事务管理、统计等功能,只能在每个对象里都引用公共行为,这样做不便于维护,而且有大量重复代码,AOP的出现弥补了OOP的 这点不足。 例如下图:

有多少个业务操作,就要写多少重复的校验和日志记录代码,这显然会增加我们的工作量。当然用面向对象的思想,可以把这些重复的代码抽离出来,写成公共方法,就是下面这样

代码冗余和可维护性的问题得到了解决,但每个业务方法中依然要依次手动调用这些公共方法,也是略显繁琐。 有没有更好的方式呢?有的,那就 是AOP,AOP将权限校验、日志记录等非业务代码完全提取出来,与业务代码分离,并寻找节点切入业务代码中 。从而实现业务代码和非业务代码分离。如下图

 

3:AOP的应用场景

1:记录日志。

2:权限校验

3:spring事务管理。

4:性能监测。

4:AOP的结构

AOP要做的就三件事:

1:在哪切入:也就是权限校验,等非业务操作在那些业务中执行。

2:什么时候切入:是业务代码执行前还是执行后。

3:切入后要做的事:比如权限校验、日志记录。

- Aspect: 切面
- PointCut:切点:---方式: 路径表达式  (2)注解形式
- Advice: 处理的时机。

5:如何使用AOP

package com.zyj.aop;

import org.springframework.stereotype.Service;

@Service
public class SumImpl implements Sum {
    @Override
    public double add(double a,double b) {
          double result=a+b;
        System.out.println("The add method result"+result);
        return result;
    }

    @Override
    public double sub(double a, double b) {
        double result=a-b;
        System.out.println("The add method result"+result);
        return result;
    }

    @Override
    public double mul(double a, double b) {
        double result=a*b;
        System.out.println("The add method result"+result);
        return result;
    }

    @Override
    public double div(double a, double b) {
        double result=a/b;
        System.out.println("The add method result"+result);
        return result;
    }
}

从上可知:我们在每个操作后,都要记录日志,如果后期日志内容发生改变。需要在每个操作后都进行修改。不利于代码的维护。

(1)引入依赖

 <dependencies>
        <!--引入spring核心依赖库-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>

        <!--引入spring切面依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>
    </dependencies>

(2)创建一个切面类

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect//标记该类为切面类
@Component//该类对象的创建交于spring容器来管理-----等价于@Service @Controller
public class MyAspects {
@Pointcut(value = "execution(public double com.zyj.aop.SumImpl.add(double ,double ))")
public void mmathService(){}
@After(value = "mmathService()")
public void b(){
System.out.println("A---The method result");
}
}

(3)创建一个spring配置文件。

<?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:context="http://www.springframework.org/schema/context"
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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描包-->
<context:component-scan base-package="com.zyj.aop"/>
<!-- 开启aop切面注解驱动 -->
<aop:aspectj-autoproxy/>
</beans>

(4)测试。

public class Text {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring.xml");
Sum sumImpl = (Sum) context.getBean("sumImpl");
sumImpl.add(10,20);
}
}

使用通配符来配置路径

@Aspect//标记该类为切面类
@Component//该类对象的创建交于spring容器来管理-----等价于@Service @Controller
public class MyAspects {
//通配符:
/**
* 第一个* : 表示任意修饰符 任意返回类型。
* 第二个* : 表示该包下所有的类。
* 第三个* : 类下所有的方法
* ..: 表示任意参数*/
@Pointcut(value = "execution(* com.zyj.aop.*.*(..))")//定义切点
public void mmathService(){}
@After(value = "mmathService()")
public void b(){
System.out.println("A---The method result");
}
}

5.2:注解模式

1:自定义注解

@Target({ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "";
}

2:修改切面类

 @Pointcut(value = "@annotation(com.zyj.aop.MyAnnotation)")//定义切点找注解
private void mypointcut2(){}
// 在使用MyAnntation注解的方法之后执行的内容
@After(value = "mypointcut2()")
public void b(){
System.out.println("A-The add method result");
}

5.3:AOP切面通知的类型

 @Aspect//标记该类为切面类
@Component//该类对象的创建交于spring容器来管理-----等价于@Service @Controller
public class MyAspects {
//通配符:
/**
* 第一个* : 表示任意修饰符 任意返回类型。
* 第二个* : 表示该包下所有的类。
* 第三个* : 类下所有的方法
* ..: 表示任意参数*/
//@Pointcut(value = "execution(* com.zyj.aop.*.*(..))")//定义切点
// public void mmathService(){}
// @After(value = "mmathService()")
// public void b(){
// System.out.println("A---The method result");
// }
// 自动注解
@Pointcut(value = "@annotation(com.zyj.aop.MyAnnotation)")//定义切点找注解
private void mypointcut2(){}
// 在使用MyAnntation注解的方法之后执行的内容
// @After(value = "mypointcut2()")
// public void b(){
// System.out.println("A-The add method result");
// }
//前置通知:
// @Before(value = "mypointcut2()")
// public void b(){
// System.out.println("====方法执行前执行切面的内容 前置通知====");
// }
// //后置返回通知,碰到return如果方法一场;这种通知不会被执行后
// @AfterReturning(value = "mypointcut2()",returning = "r")
// public void aftertReturning(Object r){
//// 参数名必须和returning的名称一致
// System.out.println("-----后置通知-----
// 异常通知:当被切入的方法出现异常时才会执行
// @AfterThrowing(value = "mypointcut2()")
// public void afterThrowable(){
// System.out.println("----异常处理----");

// 环绕通知
@Around(value = "mypointcut2()")
public Object around(ProceedingJoinPoint joinPoint){
//joinPoint:连接点 被执行的对象
System.out.println("业务代码执行前的内容======");
try {
Object proceed = joinPoint.proceed();//执行你的链接点
System.out.println("方法执行完毕·······");
return proceed;
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("方法出现异常时执行~~~~~~");
}finally {
System.out.println("无论如何都会执行");
}
return 0.0;
}
}
从上可知:

@Before 前置通知. 被代理的方法执行前--执行

@After: 后置通知: 被代理的方法执行完后--执行

@AfterReturning: 后置返回通知: 被代理的方法碰到return.--才会执行

@AfterThrowing: 后置异常通知: 当被代理的方法出现异常时--才会执行。

@Around: 环绕通知。

6:spring如何操作事物

6.1:什么是事务

事务就是一系列动作,它们被作为一个单独的工作单元,这些动作要么全部完成,要不全部失效。

例子:

public class Text {
public static void main(String[] args) {
Connection conn=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/dbaaa?serverTimeZone=Asia/Shanghai","root","2001124,zyjl");
conn.setAutoCommit(false);//设置事物手动提交
PreparedStatement ps = conn.prepareStatement("update moneys set money=money-300 where id=1");
ps.executeUpdate();
PreparedStatement px = conn.prepareStatement("update moneys set money=money+300 where id=2");
px.executeUpdate();
conn.commit();
} catch (Exception e) {
e.printStackTrace();
//事务回滚
try {
conn.rollback();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}finally {

}
}
}

6.2:spring如何实现事务

1:添加依赖

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<!--spring事务依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>

<!--mybatis的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!--mybatis和spring整合的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!--druid的连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
</dependencies>

2:spring配置文件

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--springmvc的配置-->
<!--包扫描 扫描com.ykq以及该包下的子包-->
<context:component-scan base-package="com.zyj"/>

<!--spring整合mybatis的配置-->

<!--数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<!--mysql驱动为8.0以后必须使用时区-->
<property name="url" value="jdbc:mysql://localhost:3306/dbaaa?serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="2001124,zyjl"/>
</bean>

<!--spring封装了一个类SqlSessionFactoryBean类,可以把mybatis中的配置-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:/mapper/*.xml"/>
</bean>

<!--为指定dao包下的接口生产代理实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sessionFactory"/>
<!--它会为com.ykq.dao包下的所有接口生产代理实现类-->
<property name="basePackage" value="com.zyj.dao"/>
</bean>

<!--关于事务的配置-->
<!--事务切面管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!--开启事务管理注解的驱动-->
<tx:annotation-driven/>
</beans>

3:dao类和xml

public interface MoneyDao {
//1.修改账号余额
public void updateBalance(@Param("id") int id, @Param("money") double money);
}

4:service

@Service
public class MoneyServiceImpl implements MoneyService {
@Autowired
private MoneyDao moneyDao;
@Transactional//该方法交于spring的事物来管理---默认spring不识别的该注解
public void updateBalance(int id, int uid, double money) {
// 1:扣钱
moneyDao.updateBalance(id,-money);
// 2:收钱
moneyDao.updateBalance(uid,+money);
}
// ---事务管理:(1)自己写事务管理 --自己手动定义事务切面类
// (2)使用spring提供的四五切面类
}

5:测试

public class Test {
public static void main(String[] args) {
ApplicationContext app =new ClassPathXmlApplicationContext("classpath:spring.xml");
MoneyService moneyServiceImpl =(MoneyService) app.getBean("moneyServiceImpl");
moneyServiceImpl.updateBalance(1,2,200);
}
}

 

 

标签:spring,springframework,double,理解,关于,AOP,org,public,result
From: https://www.cnblogs.com/zyjxm/p/17636158.html

相关文章

  • 关于博客园美化
    参考这个https://github.com/BNDong/Cnblogs-Theme-SimpleMemoryDocument(bndong.github.io)音乐播放器<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/APlayer.min.css"><script src="https://files.cnbl......
  • Spring-Aop
       1、面向切面编程Aspect,通过预编译方式和运行期间动态代理实现程序的统一维护的一种技术    2、AOP应用场景        (1)记录日志        (2)权限校验        (3)Spring事务管理   3、AOP的结构        AOP要做的三件事,在哪里切入,......
  • 探索递归:深入理解和应用递归算法
    递归是计算机科学中一种重要的编程技术,它在解决问题和构建算法时具有广泛的应用。本文将深入探讨递归的概念、原理和应用,帮助读者更好地理解和使用递归算法。一.什么是递归递归是指一个函数在其定义中调用自身的过程。它通过将复杂问题拆分成相同结构的子问题,并通过解决子问题来解......
  • js 关于dom
    可编辑的div//加上contenteditable="true"即可,需要注意的是内部添加的子元素默认也可编辑<divcontenteditable="true"class="edit"></div><script>vardiv=document.querySelector(".edit");//创建一个MutationObserver实例varobserver......
  • java Sping aop 以及Spring aop 的应用事务管理
    1.回顾线程死锁概念和如何避免死锁的发生:线程的通信waitnotify()notify():---Object类线程的状态:NEW--->start()--->就绪状态---CPU时间片---运行状态RUNNABLE]--->sleep()--->TIMED_WAITING--->wait()---->WAITING----sysn---Blocked---->终止状态[T]线程池:常见......
  • AOP
    AOP一、什么是AOP?Aop的专业术语(来源百度):在软件业,AOP为AspectOrientedProgramming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范......
  • javaSpring之AOP
    1.什么是AOP翻译中文为:面向切面编程,可以让你在业务代码与非业务代码隔离,增加新的非业务代码2.AOP使用场景1.我一直遵循着存在即合理的观念,在各大需求中,有一种需求是每当操作时,就会在数据库记录日志,那么在不更改业务代码的情况下应该怎么做,没错,AOP可以解决2.当然,其也可以做权限......
  • 关于使用Terraform为Azure创建一个资源组的简单案例
    使用Terraform创建AzureCloud平台的资源需要,得还有如下主要的环境及条件a、安装有Terraformb、解决身份认证及相关的权限++++++++++++++++++++++++++++++++++++++++++++++本文的目标,创建一个rg-开着的随机名称的资源组先得准备有4个文件,【providers.tf】、【main.tf】、【v......
  • 【Azure Service Fabric】关于Service Fabric的相关问题
    问题一:ServiceFabric是否支持PrivateLink?在AzurePrivateEndpoint文档中,罗列出了Azure上支持PrivateLink的服务。ServiceFabric不在其中。AzurePrivateLinkavailability:https://learn.microsoft.com/en-us/azure/private-link/availability 问题二:是否可以Disable......
  • 对于Java中String的简单理解
    String的三种初始化方式 publicclassTest{publicstaticvoidmain(String[]args){Stringstr1="Hello,World!";//直接初始化Stringstr2=newString("Hello,World!");//直接创建一个对象Stringstr3=str1;//str3作为st......