1.AOP开发

1.1.简述

  • 作用:面向切面编程:在程序运行期间,在不修改源码的情况下对代码进行增强
  • 优势:减少代码重复,提高开发效率,便于维护
  • 底层:动态代理实现(jdk动态代理,cglib动态代理)

1.2.术语

Target(目标对象):代理前的对象

Proxy (代理):代理后的增强类

Joinpoint(连接点):指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点

Pointcut(切入点):被增强的方法

Advice(通知/ 增强):封装增强业务逻辑的方法

Aspect(切面):切点 + 通知

Weaving(织入):将切点与通知结合的过程

1.3.基于xml

1.3.1.快速入门

a.引入aop依赖包

<!-- 里面包含AOP -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- aspectj的织入 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>

b.创建目标类接口和目标类

interface IUserDao{
void running();
}
class UserDao implements IUserDao{
public void running(){
System.out.println("running running");
}
}

c.创建切面

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

d.spring配置

<!-- 1.配置目标类和切面类-->
<bean id="userDao" class="IUserDao"/>
<bean id="myAspect" class="MyAspect"/>
<!-- 2.配置切面-->
<aop:config>
<aop:aspect ref="myAspect">
<aop:before method="before" pointcut="execution(public void UserDao.running())"/>
</aop:aspect>
</aop:config>

1.3.2.切面配置格式

<aop:config>
<!-- 1.切面配置-->
<aop:aspect ref="切面引用">
<!-- 1.1 配置通知和切点 -->
<aop:通知类型 method="切面类方法名" pointcut="切点表达式" />
<!-- 1.2 配置通知和切点之提取公共切点-->
<aop:pointcut id="公共切点id" expression="切点表达式"></aop:pointcut>
<aop:通知类型 method="切面类方法名" pointcut-ref="公共切点id" />
</aop:aspect>
</aop:config>

1.3.3.通知类型

标签 说明
<aop:before> 前置通知.指定增强方法在切入点方法之前执行
<aop:after-returning> 后置通知.指定增强方法在切入点方法之后执行
<aop:around> 换绕通知.指定增强方法在切入点方法之前和之后都执行
<aop:throwing> 异常抛出通知.指定增强方法在切入点方法出现异常时执行
<aop:after> 最终通知.指定增强方法在切入点方法执行之后执行,无论是否有异常

1.4.切点表达式

  • 语法
execution([修饰符] 返回值类型 包名.类名.方法名(参数))

访问修饰符可以省略

返回值类型、包名、类名、方法名可以使用星号* 代表任意

包名与类名之间一个点 . 代表当前包下的类,两个点 .. 表示当前包及其子包下的类

参数列表可以使用两个点 .. 表示任意个数,任意类型的参数列表

  • 例子
execution(public void xxx.yyy.aop.Target.method())
execution(void xxx.yyy.aop.Target.*(..))
execution(* xxx.yyy.aop.*.*(..))
execution(* xxx.yyy.aop..*.*(..))
execution(* *..*.*(..))

1.5.基于javaConfig

1.5.1.快速入门

a.创建目标类并交给spring管理

@Component("target")
public class Target implements TargetInterface {
@Override
public void method() {
System.out.println("Target running....");
}
}
@Component("myAspect")
public class MyAspect {
public void before(){
System.out.println("前置代码增强.....");
}
}

b.在切面类中使用注解配置织入关系

@Component("myAspect")
@Aspect
public class MyAspect {
// 提取公共切点
@Pointcut("execution(* com.itheima.aop.*.*(..))")
public void myPoint(){}
// 使用公共切点
@Before("MyAspect.myPoint()")
public void before(){
System.out.println("前置代码增强.....");
}
}

c.在配置中开启aop自动代理和组件扫描

  • xml
<!--组件扫描-->
<context:component-scan base-package=""/>
<!--aop的自动代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  • javaconfig
@EnableAspectJAutoProxy

1.5.2.注解通知类型

标签 说明
@Before 前置通知.指定增强方法在切入点方法之前执行
@AfterReturning 后置通知.指定增强方法在切入点方法之后执行
@Around 换绕通知.指定增强方法在切入点方法之前和之后都执行
@AfterThrowing 异常抛出通知.指定增强方法在切入点方法出现异常时执行
@After 最终通知.指定增强方法在切入点方法执行之后执行,无论是否有异常

2.集成Junit

2.1.导入test集成包和junit包

<!--此处需要注意的是,spring5 及以上版本要求 junit 的版本必须是 4.12 及以上-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

2.2.编写测试代码

// 1.替换运行时
@RunWith(SpringJUnit4ClassRunner.class)
// 2.读取spring配置文件
@ContextConfiguration(classes = {SpringConfigurationz.class}) // javaConfig
// @ContextConfiguration(locations = {"classpath:xxx.xml"}) // xml
public class SpringTest01 { // 3.注入依赖
@Autowired
private UserDao userDao; // 4.执行方法测试
@Test
public void t01(){
userDao.xxx.....
}
}

spring入门2-aop和集成测试的更多相关文章

  1. Spring入门4.AOP配置深入

    Spring入门4.AOP配置深入 代码下载 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 之前学习AOP中的一些概念,包括连接点.切入点(pointc ...

  2. Spring入门3.AOP编程

    Spring入门3.AOP编程 代码下载: 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 前面学习的知识是Spring在Java项目中的IoC或DJ,这 ...

  3. Spring入门篇——AOP基本概念

    1.什么是AOP及实现方式 什么是AOP AOP:Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 主要 ...

  4. Spring入门介绍-AOP(三)

    AOP的概念 AOP是面向切面编程的缩写,它是一种编程的新思想.对我们经常提起的oop(面对对象编程)有一定的联系. AOP和OOP的关系 AOP可以说是oop的某一方便的补充,oop侧重于对静态的属 ...

  5. Spring入门之AOP篇

    听了几节IT黑马营的SPRING课程,照着例程写了一个SPRING 中AOP的例子:  一.准备工作 下载复制或配置JAR包.图方便,我将下载的SPRING包都加上去了.另外还需要aspectj的两个 ...

  6. Spring入门之AOP实践:@Aspect + @Pointcut + @Before / @Around / @After

    零.准备知识 1)AOP相关概念:Aspect.Advice.Join point.Pointcut.Weaving.Target等. ref: https://www.cnblogs.com/zha ...

  7. Spring入门(9)-AOP初探

    Spring入门(9)-AOP初探 0. 目录 什么是面向切面编程 AOP常见术语 AOP实例 参考资料 1. 什么是面向切面编程 Aspect Oriented Programming(AOP),即 ...

  8. Spring入门导读——IoC和AOP

    和MyBatis系列不同的是,在正式开始Spring入门时,我们先来了解两个关于Spring核心的概念,IoC(Inverse of Control)控制反转和AOP()面向切面编程. 1.IoC(I ...

  9. Spring框架入门之AOP

    Spring框架入门之AOP 一.Spring AOP简单介绍 AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented ...

  10. Spring入门IOC和AOP学习笔记

    Spring入门IOC和AOP学习笔记 概述 Spring框架的核心有两个: Spring容器作为超级大工厂,负责管理.创建所有的Java对象,这些Java对象被称为Bean. Spring容器管理容 ...

随机推荐

  1. Kali 2.0 安装教程

    本文适合KALI初学者,将详细介绍Kali Linux 2.0的安装过程. 首先我们到KALI的官网下载镜像,大家可以自己选择下载32或64位的KALI 2.0系统. KALI 官网:https:// ...

  2. Avro使用手册

    1. Overview Data serialization is a technique of converting data into binary or text format. There a ...

  3. 基于Python的决策树分类器与剪枝

    作者|Angel Das 编译|VK 来源|Towards Data Science 介绍 决策树分类器是一种有监督的学习模型,在我们关心可解释性时非常有用. 决策树通过基于每个层次的多个问题做出决策 ...

  4. Data-truncation--Incorrect-string-value

    修改表中,format_content 字段的字符集为utf8mb4 alter table 表名 modify column format_content longtext character se ...

  5. uwp 的work project 的 取消闹钟

    private void initalAlarmHanle() { string cancelAlarm = "CancelAlarmEvent"; ConnectionManag ...

  6. c# button Command

    internal class DelegateCommand : ICommand { private readonly Action _execute; private readonly Func& ...

  7. Django常用 命令

    Django常用 命令: 安装: pip install django 指定版本 pip3 install django==2.0 新建项目: django-admin.py startproject ...

  8. shiro(二)

    public class AuthorizerTest { @Test public void testIsPermitted() { login("classpath:shiro-auth ...

  9. Flink中的Time与Window

    一.Time 在Flink的流式处理中,会涉及到时间的不同概念 Event Time(事件时间):是事件创建的时间.它通常由事件中的时间戳描述,例如采集的日志数据中,每一条日志都会记录自己的生成时间, ...

  10. springcloud超时重试机制的先后顺序

    https://blog.csdn.net/zzzgd_666/article/details/83314833