一、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实操的更多相关文章

  1. spring再学习之AOP事务

    spring中的事务 spring怎么操作事务的: 事务的转播行为: 事务代码转账操作如下: 接口: public interface AccountDao { //加钱 void addMoney( ...

  2. spring再学习之AOP准备

    一.aop思想: 横向重复,纵向抽取 1.乱码 2.事务管理 3,action 二.spring能够为容器中管理的对象生成代理对象 1.spring能帮我们生成代理对象 2.spring实现aop的原 ...

  3. spring再学习之设计模式

    今天我们来聊一聊,spring中常用到的设计模式,在spring中常用的设计模式达到九种. 第一种:简单工厂 三种工厂模式:https://blog.csdn.net/xiaoddt/article/ ...

  4. Spring基础学习(四)—AOP

    一.AOP基础 1.基本需求      需求: 日志功能,在程序执行期间记录发生的活动. ArithmeticCalculate.java public interface ArithmeticCal ...

  5. Spring框架学习06——AOP底层实现原理

    在Java中有多种动态代理技术,如JDK.CGLIB.Javassist.ASM,其中最常用的动态代理技术是JDK和CGLIB. 1.JDK的动态代理 JDK动态代理是java.lang.reflec ...

  6. Spring框架学习05——AOP相关术语详解

    1.Spring AOP 的基本概述 AOP(Aspect Oriented Programing)面向切面编程,AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视.事务管理.安全检查 ...

  7. spring框架学习(三)——AOP( 面向切面编程)

    AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...

  8. spring再学习之注解

    1.使用注解配置spring <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi= ...

  9. spring再学习之配置详解

    applicationContext.xml文件配置: bean元素: <?xml version="1.0" encoding="UTF-8"?> ...

随机推荐

  1. ftp设置二进制上传

    一个不重要的数据库,备份是用expdp导出,然后上传到ftp服务器上面.上周这个主机宕机了,要在别的数据库恢复,发现报如下错误: ORA-39001: invalid argument value O ...

  2. SAP中的事务锁

    我们知道sap中的事物锁tcode是SM01. 细细研究发现,其实无外乎就是将tstc表中的事务码对应的字段CINFO的值加上HEX20 解锁就是还原成原来的值. 当然也发现了,调用了一个系统函数AU ...

  3. PAT练习num4-D进制的A+B

    输入两个非负 10 进制整数 A 和 B (≤),输出 A+B 的 D (1)进制数. 输入格式: 输入在一行中依次给出 3 个整数 A.B 和 D. 输出格式: 输出 A+B 的 D 进制数. 输入 ...

  4. IE浏览器兼容问题总结

    IE浏览器兼容问题总结 引自掘金:https://juejin.cn/post/6844903825854185480 一.标准盒模型和怪异盒模型 浏览器的盒子模型分为两类: 标准的W3C盒子模型. ...

  5. 推荐几个学习Python的免费网站

    想要学好Python,只靠看Python相关的书籍是远远不够的!今天为大家分享几个实用的Python学习网站. 欢迎各位热爱Python的小伙伴进群交流:610380249群里有大佬哦,而且很热心,群 ...

  6. 使用注解的形式对token进行验证

    @[TOC](使用注解的形式对token进行验证)# 前言现在很多系统都是都用上了springboot.springcloud,系统也偏向分布式部署.管理,最早的用户令牌方案:session.cook ...

  7. IDEA 简介

    什么是IDEA IDEA 全称 IntelliJ IDEA,是 Java 语言开发的集成环境,IntelliJ 在业界被公认为最好的 Java 开发工具之一,尤其在智能代码助手.代码自动提示.重构.J ...

  8. 布隆过滤器 数据同步业务 :google-guava+spring-boot-api+mybatis, 缺失值全匹配查找

    布隆过滤器 数据同步业务 :google-guava+spring-boot-api+mybatis, 缺失值全匹配查找

  9. DPDK CAS(compare and set)操作

    前言 rte_ring是一个无锁队列,无锁队列的出队入队操作是rte_ring实现的关键.因此,本文主要讲解dpdk是怎样使用无锁机制实现rte_ring的多生产者入队操作. rte_atomic32 ...

  10. 最简单直接地理解Java软件设计原则之单一职责原则

    理论性知识 定义 单一职责原则, Single responsibility principle (SRP): 一个类,接口,方法只负责一项职责: 不要存在多余一个导致类变更的原因: 优点 降低类的复 ...