spring入门2-aop和集成测试
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和集成测试的更多相关文章
- Spring入门4.AOP配置深入
Spring入门4.AOP配置深入 代码下载 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 之前学习AOP中的一些概念,包括连接点.切入点(pointc ...
- Spring入门3.AOP编程
Spring入门3.AOP编程 代码下载: 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 前面学习的知识是Spring在Java项目中的IoC或DJ,这 ...
- Spring入门篇——AOP基本概念
1.什么是AOP及实现方式 什么是AOP AOP:Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 主要 ...
- Spring入门介绍-AOP(三)
AOP的概念 AOP是面向切面编程的缩写,它是一种编程的新思想.对我们经常提起的oop(面对对象编程)有一定的联系. AOP和OOP的关系 AOP可以说是oop的某一方便的补充,oop侧重于对静态的属 ...
- Spring入门之AOP篇
听了几节IT黑马营的SPRING课程,照着例程写了一个SPRING 中AOP的例子: 一.准备工作 下载复制或配置JAR包.图方便,我将下载的SPRING包都加上去了.另外还需要aspectj的两个 ...
- Spring入门之AOP实践:@Aspect + @Pointcut + @Before / @Around / @After
零.准备知识 1)AOP相关概念:Aspect.Advice.Join point.Pointcut.Weaving.Target等. ref: https://www.cnblogs.com/zha ...
- Spring入门(9)-AOP初探
Spring入门(9)-AOP初探 0. 目录 什么是面向切面编程 AOP常见术语 AOP实例 参考资料 1. 什么是面向切面编程 Aspect Oriented Programming(AOP),即 ...
- Spring入门导读——IoC和AOP
和MyBatis系列不同的是,在正式开始Spring入门时,我们先来了解两个关于Spring核心的概念,IoC(Inverse of Control)控制反转和AOP()面向切面编程. 1.IoC(I ...
- Spring框架入门之AOP
Spring框架入门之AOP 一.Spring AOP简单介绍 AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented ...
- Spring入门IOC和AOP学习笔记
Spring入门IOC和AOP学习笔记 概述 Spring框架的核心有两个: Spring容器作为超级大工厂,负责管理.创建所有的Java对象,这些Java对象被称为Bean. Spring容器管理容 ...
随机推荐
- Java之Spring基础与IoC
Spring About Spring 开源免费框架,轻量级,非入侵式框架.Spring就是一个轻量级的控制反转(IOC)和面向切片编程(AOP)的框架 Maven repo:Spring Web M ...
- javascript - 将字符串转换为模板字符串
参考: https://www.itranslater.com/qa/details/2325714161562551296 是否可以将模板字符串创建为常用字符串 let a="b:${b} ...
- 配置之XML--读取XML文件 转存为Key-Value
将XML文件读取 绑定数据至Dictionary Eg: Xml文件 <?xml version="1.0" encoding="utf-8" ?> ...
- c# 对 struct为什么不能继承类和结构的思考
1.类.结构在使用的时候可以不调用构造函数,如果能够继承类,这种情况下不能够初始化基类,因为不执行构造函数 2.结构.所有的结构类型都默认是 sealed,通过 反汇编可以看到 ,这就阻止了结构被继 ...
- C# volatile 的使用
class Program { private static volatile bool bChanged; static void Main(string[] args) { Thread t1 = ...
- 深入浅出Mybatis系列(十)---延迟加载
一.延迟加载 resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能. 延迟加载:先 ...
- mzy git学习,git推送到远程库(八)
git在同步到远程库 关于git中多个用户切换的事情: 完全使用账户密码策略连接远程库: 之前一直尝试在本地切换多个用户,发现一直不行,很奇怪?后面发现必须要去win10的凭据管理器删除当前git的凭 ...
- JDBC基础篇(MYSQL)——使用statement执行DQL语句(select)
注意:其中的JdbcUtil是我自定义的连接工具类:代码例子链接: package day02_statement; import java.sql.Connection; import java.s ...
- JAVA中直接用Jdbc就能操作数据库了,为什么还要用spring框架?
不过随着业务的扩展,你就会发现jdbc建立一个连接居然要几百毫秒,而执行一个普通的SQL仅仅需要几毫秒. 这么重量级的资源建立了就释放了不合适,得找个容器存起来,谁要就来取,不用了就还给容器,毕竟容器 ...
- for in和for of的简单区别
//for in可以遍历数组和对象,但是for of只能遍历数组,不可以遍历对象 var arr = [1,4,5,6,7,8]; var obj = { name:'za', age:19, say ...