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"?> ...
随机推荐
- 分布式系统:dubbo的连接机制
目录 研究这个问题的起因 dubbo的连接机制 为什么这么做 dubbo同步转异步 dubbo的实现 纯netty的简单实现 总结 研究这个问题的起因 起因是一次面试,一次面试某电商网站,前面问到缓存 ...
- Sgu149 Computer Network
Sgu149 Computer Network 题目描述 给你一棵N(N<=10000)个节点的树,求每个点到其他点的最大距离. 不难想到一个节点到其他点的最大距离为:max(以它为根的子树的最 ...
- 24V降压5V芯片,5A,4.5V-30V输入,同步降压调节器
PW2205开发了一种高效率的同步降压DC-DC转换器5A输出电流.PW2205在4.5V到30V的宽输入电压范围内工作集成主开关和同步开关,具有非常低的RDS(ON)以最小化传导损失.PW2205采 ...
- H3C、Huawei、Cisco网络设备AAA TACACS认证配置
TACACS技术白皮书 摘要:TACACS是实现AAA功能的一种安全协议,主要是通过TACACS客户端与TACACS服务器通信来实现多种用户的AAA功能. HWTACACS采用TCP协议承载报文,TC ...
- Bitter.Core系列六:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 之 示例 DataTable 模型转换
当我们查询之前,我们先构造一个查询对象的输出DTO.如下图代码: public class TScoreSearchDto { /// <summary> /// 分数 /// </ ...
- (转载)微软数据挖掘算法:Microsoft 关联规则分析算法(7)
前言 本篇继续我们的微软挖掘算法系列总结,前几篇我们分别介绍了:微软数据挖掘算法:Microsoft 决策树分析算法(1).微软数据挖掘算法:Microsoft 聚类分析算法(2).微软数据挖掘算法: ...
- 使用Robo 3T操作MongoDB数据库
安装Robo 3T连接MongoDB数据库教程:https://blog.csdn.net/baidu_39298625/article/details/98845789 在IDEA中用三个jar包链 ...
- 圣诞快乐!OIer挂分小技巧
OIer常犯错误 自己的错误 循环里套return 线段树求和 int 定义,下传 int 定义 cmp<,>号分不清 主观行为举动错误 踢电源线,注意安全(_Destiny) TLE 大 ...
- MongoTemplate聚合(一)$lookup
mongodb 最近入职了新的公司,新公司统一使用的mongodb,es等非关系型数据库.以前对es有一些了解,其实就是灵活的文档类型结构,不受限于关系型数据库的那种字段唯一确定的"死板 ...
- 小白的springboot之路(十九)、集成swagger(com.spring4all篇)
0-前言 集成swagger,有两种方式: 一种在前面已经介绍过了,直接集成官方的springfox-swagger2的方式,这种方式需要在配置类中配置 第二种方式是这里要介绍的方式,国人写的com. ...