JavaWeb_(Spring框架)认识Spring中的aop
1、aop思想介绍(面向切面编程):将纵向重复代码,横向抽取解决,简称:横切

2、Spring中的aop:无需我们自己写动态代理的代码,spring可以将容器中管理对象生成动态代理对象,前提是我们对他进行一些设置;
3、Spring-aop是基于动态代理的 – 优先选用JDKProxy动态代理;
a)Proxy动态代理:被代理的对象必须要实现接口;
b)Cglib动态代理:被代理的对象不能被final修饰,基于继承;

package com.Gary.service;
public interface UserService {
//增
void save();
//删
void delete();
//改
void update();
//查
void find();
}
UserService.java
package com.Gary.service;
public class UserServiceImpl implements UserService{
@Override
public void save() {
System.out.println("save()");
}
@Override
public void delete() {
System.out.println("delete()");
}
@Override
public void update() {
System.out.println("update()");
}
@Override
public void find() {
System.out.println("find()");
}
}
UserServiceImpl.java
package com.Gary.test; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import com.Gary.service.UserService;
import com.Gary.service.UserServiceImpl; //UserService代理类
public class UserServiceProxy {
//代理对象 UserServiceProxy
//被代理对象 UserService
public UserService getUserServiceProxy(UserService us) {
return (UserService) Proxy.newProxyInstance(UserServiceProxy.class.getClassLoader(),
UserServiceImpl.class.getInterfaces(),
new InvocationHandler(){
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//增强代码
System.out.println("开启事务");
//需要调用原始的方法
Object invoke = method.invoke(us, args);
System.out.println("提交/回滚"); return invoke;
}
}); } }
UserServiceProxy.java
package com.Gary.test; import org.junit.Test; import com.Gary.service.UserService;
import com.Gary.service.UserServiceImpl; public class AopTest { @Test
public void Test1() {
UserServiceProxy usProxy = new UserServiceProxy();
UserService us = new UserServiceImpl();
UserService us_PowerUp = usProxy.getUserServiceProxy(us); us_PowerUp.find(); } }
AopTest.java
4、Spring aop相关名词说明
代理写小学生暑假作业,3年级以下
目标对象:UserService(小学生作业)
被代理对象:被UserServiceProxy增强后的UserService(用你的知识去写的作业)
JoinPoint、连接点、目标对象中,哪些方法会被拦截;save,delete,update,find (所有要写的作业:数学、语文、英语、王者荣耀代上王者)
Pointcut切入点:筛选连接点,你最终要增强的方法;save,update,delete (小学生只让你给写数学语文英语,王者荣耀他自己上王者)
Advice通知/增强:要执行的增强代码 (你用你N年积攒的知识去完成小学暑假作业)
Introduction介入/引入:在执行时期动态加入一些方法或行为
Aspect切面:通知 + 切入点,通知应用到哪个切点
target目标:被代理对象 (小学生的作业)
weaving织入:把切面的代码应用到目标对象来创建新的代理对象的过程 (将你的脑子应用到写小学生的暑假作业上)
proxy代理:把切面的代码应用到目标对象来创建新的代理对象(利用你的知识去完成作业)
5、Spring aop配置:
a)导包:
i.基本包;
ii.spring-aspects和spring-aop ;
iii.aop联盟包 – aopalliance;
iv.aop织入包 - aspectj.weaver;

b)自定义通知,五种自定义通知类型:
i.before 前置通知
ii.after 最终通知(后置通知)
iii.afterReturning 成功通知(后置通知)
iv.afterThrowing 异常通知(后置通知)
v.around 环绕通知
在aop层创建一个MyAdvice.java自定义通知类
package com.Gary.aop; import org.aspectj.lang.ProceedingJoinPoint; //自定义通知类
public class MyAdvice { //before 前置通知 在目标方法前调用
public void before() {
System.out.println("before()");
} //after 最终通知(后置通知)在目标方法后调用,无论是否出现异常都会执行 finally
public void after() {
System.out.println("after()");
} //afterReturning 成功通知(后置通知) 在目标方法执行后,并执行成功,如果方法出现异常,则不会调用
public void afterReturning(){
System.out.println("afterReturning()");
} //afterThrowing 异常通知(后置通知) 在目标方法执行出现异常的时候才会调用
public void afterThrowing() {
System.out.println("after()");
} //around 环绕通知 需要我们手动调用目标方法,并且可以设置通知
public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("around before");
Object proceed = pjp.proceed();
System.out.println("around after");
return proceed;
} }
MyAdvice.java
c)配置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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 目标对象 -->
<bean name="userService" class="com.Gary.service.UserServiceImpl"></bean> <!-- 通知对象 -->
<bean name="myAdvice" class="com.Gary.aop.MyAdvice"></bean> <aop:config>
<!-- 切入点 expression 切入点表达式 可以配置要增强的方法
public void com.Gary.service.UserServiceImpl.save()
* com.Gary.service.*ServiceImpl.*(..)
id 就是唯一标识
-->
<aop:pointcut expression="execution(* com.Gary.service.*ServiceImplabc.*(..))" id="servicePc"/> <!-- 切面 通知+切入点 -->
<aop:aspect ref="myAdvice">
<!-- 通知类型 -->
<aop:before method="before" pointcut-ref="servicePc"/>
<!-- 最终通知 后置通知 -->
<aop:after method="after" pointcut-ref="servicePc"/>
<!-- 成功通知 后置通知 -->
<aop:after-returning method="afterReturning" pointcut-ref="servicePc"/>
<!-- 异常通知 后置通知 -->
<aop:after-throwing method="afterThrowing" pointcut-ref="servicePc"/>
<!-- 环绕通知 -->
<aop:around method="around" pointcut-ref="servicePc"/>
</aop:aspect>
</aop:config> </beans>
applicationContext.xml
JavaWeb_(Spring框架)认识Spring中的aop的更多相关文章
- Eclipse/JavaWeb (三)三大框架之Spring框架 持续更新中...
(一)发展历史 现在我们有三个层了,可是每层之间的调用是怎样的呢?比如显示层的struts需要调用一个业务类,就需要new一个业务类出来,然后使用:业务层需要调用持久层的类,也需要new一个持久层类出 ...
- Spring框架的核心功能之AOP技术
技术分析之Spring框架的核心功能之AOP技术 AOP的概述 1. 什么是AOP的技术? * 在软件业,AOP为Aspect Oriented Programming的 ...
- spring框架学习(六)AOP
AOP(Aspect-OrientedProgramming)面向方面编程,与OOP完全不同,使用AOP编程系统被分为方面或关注点,而不是OOP中的对象. AOP的引入 在OOP面向对象的使用中,无可 ...
- Spring框架的核心功能之AOP概述
1. 什么是AOP的技术? * 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程 * AOP是一种编程范式,隶属于软工范畴,指导开发者如何组织程序结构 ...
- Spring框架 之IOC容器 和AOP详解
主要分析点: 一.Spring开源框架的简介 二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置 一.S ...
- Spring 框架的核心功能之AOP技术
1. AOP 的概述 AOP, Aspect Oriented Programming, 面向切面编程; 通过预编译方式和运行期动态代理实现程序功能的统一维护的技术; AOP 采取横向抽取机制,取代了 ...
- spring框架学习(四)——注解方式AOP
注解配置业务类 使用@Component("s") 注解ProductService 类 package com.how2java.service; import org.spri ...
- JavaWeb_(SpringMVC框架)SpringMVC&Spring&MyBatis整合
JavaWeb_(SpringMVC框架)测试SpringMVC&Spring&MyBatis三大整合 传送门 1.整合ssm 3大框架 过程 a)导包 -> spring_Ja ...
- Spring 框架(持续完善中)
目录标题 一.Spring 框架 Spring 是什么? Spring Framework 核心概念 了解Spring 框架的架构图 二.Spring Framework 之 IOC 开发的步骤流程 ...
随机推荐
- winform PictureBox图片上动态添加Label或其他控件
效果图: 代码: //分页或者多次加载时,需要删除之前产生的lable等控件 ; tabID < ; tabID++) { foreach (Control control in this.ta ...
- nodejs连接mysql数据库,报错Client does not support authentication protocol requested by server的解决方法
最近想要尝试nodejs连接本地数据库,往全栈方向做一个小小的尝试,于是下载了一个 MySQL8.0,发现Navicat连接不上,结果就下载了mysql自身的Workbench,继续使用. 然而,难受 ...
- vue-cli || webpack 打包的时候css里面写的背景图片的路径出错问题
.bg width 100% position fixed left 0 top 0 height 100vh z-index -1 background url('~@/assets/imgs/bg ...
- css滑动门技术
滑动门的核心技术: 为了使各种特殊形状的背景能够自适应元素中文本内容的多少,以使自由拉伸滑动 利用css精灵(主要是背景位置)和盒子padding撑开宽度,以便适应不同字数的导航栏 一般经典布局 &l ...
- 在nuxt项目中使用component组件
编写组件页面 1.在components下新建一个新建组件页面,如下所示 2.新建完成之后初始页面的代码如下所示: 3.下面从element-ui上找一个顶部导航菜单写到组件页面. <el-me ...
- python2,3区别
Python2 Python3 default charset ascii(can change) utf-8 print 可不加括号 必须加 range 有xrange()生成器 可转换为ran ...
- 【JavaWeb】通过邮件找回密码
前言 本文将介绍忘记密码时通过发送重置密码邮件找回密码的实现思路.整个实现过程中最重要的就是以下三点: 如何发送邮件到用户指定邮箱 邮件中的重置密码链接构成是怎么样的 验证重置密码链接的合法性(是否过 ...
- 前端笔记-css
css(穿着) 1. 第一种<head><style></style></head>中可以写css样式 css选择器 定位到哪个标签的css id选择器 ...
- python画图matplolib
http://python.jobbole.com/85106/ 1.画二维图 2.画三维图 我的电脑只能在jupyter notebook上面运行才能看的到,常规import库 %matplotli ...
- httprunner---->最最基础小白笔记
1.安装httprunner pip install httprunner 2.cmd 执行hrun --startproject Api_api 出现了: 3.Fiddler抓包后 ...