spring再学习之AOP实操
一、spring导包

2、目标对象
public class UserServiceImpl implements UserService {
@Override
public void save() {
System.out.println("保存用户!");
//int i = 1/0;
}
@Override
public void delete() {
System.out.println("删除用户!");
}
@Override
public void update() {
System.out.println("更新用户!");
}
@Override
public void find() {
System.out.println("查找用户!");
}
}
3、准备通知
//通知类
public class MyAdvice {
/**
* 前置通知
*-目标方法运行前调用
*后置通知(如果出现异常不会调用)
*-目标方法运行之后调用
*环绕通知
*-在目标方法之前之后调用
*异常拦截通知
*-在目标方法运行之后调用
*后置通知(无论是否出现异常都会调用)
*目标方法运行后调用
*/ //前置通知
public void before() {
System.out.println("这是前置通知!!!");
}
//后置通知通知
public void afterReturning() {
System.out.println("这是后置通知(如果出现异常不会调用)!!!");
}
//环绕通知
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("这是环绕通知之前的部分");
Object proceed = pjp.proceed();//调用目标方法
System.out.println("这是环绕通知之后的部分");
return proceed;
}
//异常通知
public void afterException() {
System.out.println("出事了,出现异常了");
}
//后置通知
public void after() {
System.out.println("出事了,出现异常了");
} }
4、配置进行织入
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> <!-- 导入安aop(约束)命名空间 -->
<!-- 1.配置目标对象 -->
<bean name="userServiceTarget" class="cn.itcast.service.UserServiceImpl"></bean>
<!-- 2.配置通知对象 -->
<bean name="myAdvice" class="cn.itcast.d_springaop.MyAdvice"></bean>
<!-- 3.配置将通知织入目标对象 -->
<aop:config>
<!-- 配置切入点
书写expression="execution(* cn.itcast.service.*ServiceImpl.*(..))"
public void cn.itcast.service.UserServiceImpl.save()
一般 public省略掉 ,一般对返回值不做要求用*表示,类下的放大,用*表示全部的方法
* cn.itcast.service.UserServiceImpl.*()
继续演化..表示不对参数有任何要求
* cn.itcast.service.UserServiceImpl.*(..)
继续演化,不对集体的类有要求
* cn.itcast.service.*ServiceImpl.*(..)
继续演化 ,不只找service中的类而且找子包
* cn.itcast.service..*ServiceImpl.*(..)
-->
<aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="pc"/>
<aop:aspect ref="myAdvice">
<aop:before method="before" pointcut-ref="pc"/>
<aop:after-returning method="afterReturning" pointcut-ref="pc"/>
<aop:around method="around" pointcut-ref="pc"/>
<aop:after method="after" pointcut-ref="pc"/>
<aop:after-throwing method="afterException" pointcut-ref="pc"/>
</aop:aspect> </aop:config> </beans>
测试:
/**
* @RunWith :帮我们创建容器
* @ContextConfiguration :指定创建容器时使用哪个配置文件
* @author zws
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:cn/itcast/e_annotationaop/applicationContext.xml")
public class Demo { @Resource(name="userServiceTarget")
private UserService us; @Test
public void fun1(){
us.save();
} }
结果:
这是环绕通知之前的部分
这是前置通知!!!
保存用户!
这是环绕通知之后的部分
出事了,出现异常了
这是后置通知(如果出现异常不会调用)!!!
注解配置:
//通知类
@Aspect
//表示该类是一个通知类
public class MyAdvice { //前置通知
// -目标方法运行前调用
//后置通知(如果出现异常不会调用)
// -目标方法运行之后调用
//环绕通知
// -在目标方法之前之后调用
//异常拦截通知
// -在目标方法运行之后调用
//后置通知(无论是否出现异常都会调用)
//前置通知
@Before("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public void before() {
System.out.println("这是前置通知!!!");
} //后置通知通知
@AfterReturning("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public void afterReturning() {
System.out.println("这是后置通知(如果出现异常不会调用)!!!");
} //环绕通知
@Around("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("这是环绕通知之前的部分");
Object proceed = pjp.proceed();//调用目标方法
System.out.println("这是环绕通知之后的部分");
return proceed;
}
//异常通知
@AfterThrowing("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public void afterException() {
System.out.println("出事了,出现异常了");
}
//后置通知
@After("execution(* cn.itcast.service.*ServiceImpl.*(..))")
public void after() {
System.out.println("出事了,出现异常了");
} }
配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> <!-- 导入安aop(约束)命名空间 -->
<!-- 1.配置目标对象 -->
<bean name="userServiceTarget" class="cn.itcast.service.UserServiceImpl"></bean>
<!-- 2.配置通知对象 -->
<bean name="myAdvice" class="cn.itcast.e_annotationaop.MyAdvice"></bean>
<!-- 3.配置将通知织入目标对象 使用注解完成织如-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
测试:
/**
* @RunWith :帮我们创建容器
* @ContextConfiguration :指定创建容器时使用哪个配置文件
* @author zws
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:cn/itcast/e_annotationaop/applicationContext.xml")
public class Demo { @Resource(name="userServiceTarget")
private UserService us; @Test
public void fun1(){
us.save();
} }
结果;
这是环绕通知之前的部分
这是前置通知!!!
保存用户!
这是环绕通知之后的部分
出事了,出现异常了
这是后置通知(如果出现异常不会调用)!!!
spring再学习之AOP实操的更多相关文章
- spring再学习之AOP事务
spring中的事务 spring怎么操作事务的: 事务的转播行为: 事务代码转账操作如下: 接口: public interface AccountDao { //加钱 void addMoney( ...
- spring再学习之AOP准备
一.aop思想: 横向重复,纵向抽取 1.乱码 2.事务管理 3,action 二.spring能够为容器中管理的对象生成代理对象 1.spring能帮我们生成代理对象 2.spring实现aop的原 ...
- spring再学习之设计模式
今天我们来聊一聊,spring中常用到的设计模式,在spring中常用的设计模式达到九种. 第一种:简单工厂 三种工厂模式:https://blog.csdn.net/xiaoddt/article/ ...
- Spring基础学习(四)—AOP
一.AOP基础 1.基本需求 需求: 日志功能,在程序执行期间记录发生的活动. ArithmeticCalculate.java public interface ArithmeticCal ...
- Spring框架学习06——AOP底层实现原理
在Java中有多种动态代理技术,如JDK.CGLIB.Javassist.ASM,其中最常用的动态代理技术是JDK和CGLIB. 1.JDK的动态代理 JDK动态代理是java.lang.reflec ...
- Spring框架学习05——AOP相关术语详解
1.Spring AOP 的基本概述 AOP(Aspect Oriented Programing)面向切面编程,AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视.事务管理.安全检查 ...
- spring框架学习(三)——AOP( 面向切面编程)
AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...
- spring再学习之注解
1.使用注解配置spring <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi= ...
- spring再学习之配置详解
applicationContext.xml文件配置: bean元素: <?xml version="1.0" encoding="UTF-8"?> ...
随机推荐
- C# 请求被中止: 未能创建 SSL/TLS 安全通道。 设置SecurityProtocol无效
今天为了获取一张图片,用了一段代码: ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateV ...
- CSS3+JS完美实现放大镜模式
最近看到一篇讲放大镜的文章,实践后感觉效果非常好,这里分享给大家. 效果如下: 其实现核心: CSS函数,如:calc() -- 动态计算:var() -- 使用自定义变量 CSS伪元素:::befo ...
- 配置Charles 设置手机代理并允许https请求
前言: 在h5开发调试时,为实现手机app访问localhost地址,可以使用ip地址的方式,但一般公司app出于安全考虑,会限制只能访问其自有域名.因此,使用charles代理的方式 步骤 用手机代 ...
- Spring基于注解开发的注解使用之AOP(部分源代码分析)
AOP底层实现动态代理 1.导入spring-aop包依赖 <!--aopV1--> <dependency> <groupId>org.springframewo ...
- Less中Css预处理器
Less.js 安装 npm install -g less 变量 basic 变量采用@进行变量定义.变量可以直接参加运算. @width:100px; .variables{ width:@wid ...
- 解决JavaScript中构造函数浪费内存的问题!
解决JavaScript中构造函数浪费内存的问题! 把构造函数中的公共的方法放到构造函数的原型对象上! // 构造函数的问题! function Gouzaohanshu(name, age, gen ...
- 不占用额外内存空间能否做到 将图像旋转90度 N × N矩阵表示的图像,其中每个像素的大小为4字节
给定一幅由N × N矩阵表示的图像,其中每个像素的大小为4字节,编写一种方法,将图像旋转90度. 不占用额外内存空间能否做到? 示例 1: 给定 matrix = [ [1,2,3], [4,5,6] ...
- TCMalloc源码学习(三)(小块内存分配)
线程本地cache 线程本地cache对应的是类 ThreadCache,每一个thread一个实例,初始化代码在static函数CreateCacheIfNecessary中, 在该线程第一次申请内 ...
- Webpack4.0各个击破(5)module篇
一. 模块化乱炖 脚本合并是基于模块化规范的,javascript模块化是一个非常混乱的话题,各种[*MD]规范乱飞还要外加一堆[*.js]的规范实现.现代化前端项目多基于框架进行开发,较为流行的框架 ...
- CF1416D 做题心得
CF1416D 做题心得 上次在某trick中提到了这个题,一开始觉得太毒瘤没有写,现在把它补上了. 感觉实现这个东西,比单纯收获一个trick,收获的东西多太多了. 主要思路 它的主要trick是& ...