Spring4之AOP
[www.dev1234.com]一头扎进Spring4视频教程\一头扎进Spring4源码\[www.java1234.com]《一头扎进Spring4》第七讲 源码
[www.dev1234.com]一头扎进Spring4视频教程\一头扎进Spring4源码\[www.java1234.com]《一头扎进Spring4》第八讲 源码
配置:以下重点划出的是新添加进去的
<?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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
、、切面配置如下
<bean id="studentServiceAspect" class="com.java1234.advice.StudentServiceAspect"></bean> //先创建好两个bean
<bean id="studentService" class="com.java1234.service.impl.StudentServiceImpl"></bean> <aop:config>//config aop标志
<aop:aspect id="studentServiceAspect" ref="studentServiceAspect"> //aspect定义一个切面,studentServiceAspect是切面类
//定义切点,是方法级别的,要写表达式{第一个*表示任何是任意,后面的两点..表示参数是任意的},指定方法
<aop:pointcut expression="execution(* com.java1234.service.*.*(..))" id="businessService"/>
<aop:before method="doBefore" pointcut-ref="businessService"/>
<aop:after method="doAfter" pointcut-ref="businessService"/>
<aop:around method="doAround" pointcut-ref="businessService"/>//环绕
<aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>//返回
<aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>//异常
</aop:aspect>
</aop:config>
package com.java1234.advice; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class StudentServiceAspect { public void doBefore(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("开始添加学生:"+jp.getArgs()[0]);
} public void doAfter(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("学生添加完成:"+jp.getArgs()[0]);
} public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("添加学生前");
Object retVal=pjp.proceed();
System.out.println(retVal);
System.out.println("添加学生后");
return retVal;
} public void doAfterReturning(JoinPoint jp){
System.out.println("返回通知");
} public void doAfterThrowing(JoinPoint jp,Throwable ex){
System.out.println("异常通知");
System.out.println("异常信息:"+ex.getMessage());
}
}
Spring4之AOP的更多相关文章
- Spring4 AOP详解
Spring4 AOP详解 第一章Spring 快速入门并没有对Spring4 的 AOP 做太多的描述,是因为AOP切面编程概念不好理解.所以这章主要从三个方面详解AOP:AOP简介(了解),基于注 ...
- Spring Aop的执行顺序
Spring Aop的执行顺序 首先回忆一下 AOP 的常用注解 @Before:前置通知:目标方法之前执行 @After:后置通知:目标方法之后执行 @AfterReturning:返回后通知:执行 ...
- Spring 4 bak
IOC (参考<Spring企业开发>.<Spring实战 第三版 第四版>) IoC概述 1. 控制反转 2.依赖注入 控制反转:大多数情况下,想要 ...
- Spring4学习笔记-AOP
1.加入jar包 com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEAS ...
- Spring4.0学习笔记(10) —— Spring AOP
个人理解: Spring AOP 与Struts 的 Interceptor 拦截器 有着一样的实现原理,即通过动态代理的方式,将目标对象与执行对象结合起来,降低代码之间的耦合度,主要运用了Proxy ...
- Spring4笔记9--Spring的事务管理(AOP应用的例子)
Spring的事务管理: 事务原本是数据库中的概念,在 Dao 层.但一般情况下,需要将事务提升到业务层,即 Service 层.这样做是为了能够使用事务的特性来管理具体的业务. 在 Spring ...
- Spring4笔记7--AspectJ 对 AOP 的实现
AspectJ 对 AOP 的实现: 对于 AOP 这种编程思想,很多框架都进行了实现.Spring 就是其中之一,可以完成面向切面编程.然而,AspectJ 也实现了 AOP 的功能,且其实现方式更 ...
- Spring4笔记6--Spring与AOP
Spring与AOP: AOP的引入: 主业务经常需要调用系统级业务(交叉业务),如果在主业务代码中大量的调用系统级业务代码,会使系统级业务与主业务深度耦合在一起,大大影响了主业务逻辑的可读性,降低了 ...
- 峰Spring4学习(6)spring AOP的应用例子
一.AOP简介: 二.AOP实例: 三.使用的例子 需求:在student添加的前后,打印日志信息: 0)spring AOP需要引用的jar包: 1)StudentService.java接口: p ...
随机推荐
- Elastic 基础篇(2)
1.基本概念 1)Elastic和RDMS对比 RDMS Elastic 数据库database 索引index 表table 类型type 行row 文档document 列column 字段fie ...
- MySQL存储过程实现动态执行SQL
--存储过程名和参数,参数中in表示传入参数,out标示传出参数,inout表示传入传出参数 create procedure p_procedurecode(in sumdate varchar(1 ...
- 异步请求 ajax的使用详解
https://blog.csdn.net/happyaliceyu/article/details/52381446 可以说是很详细了,赞
- 使用PreparedStatement 查询一条数据 封装成一个学生的Student1对象
package cn.lijun.entity; public class Student1 { private int id; private String sname; private int g ...
- bzoj1009 KMP+矩阵dp
https://www.lydsy.com/JudgeOnline/problem.php?id=1009 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(<=Xi<=), ...
- wz
chcp 437 monkeyrunner wz.py wz.bat #coding=utf-8 from com.android.monkeyrunner import MonkeyDevice,M ...
- 利用salt搭建hadoop集群
自动化工具有很多..今天总结一下salt安装hadoop 步骤,学习过程. 1,机器列表 hosts文件 只需要将namenode的两台机器上配置 ,不解释了. 2.salt-master在10 ...
- SpringBoot笔记十五:任务
目录 异步任务 定时任务 异步任务 注解:@Async,@EnableAsync 我新建一个Service,就叫AsyncService package com.example.service; im ...
- 利用 JMetal 实现大规模聚类问题的研究(二) JMetal代码总览
之前完成了导入JMetal到自己的工程这一步. 在开始我们研究的问题之前,我们先对JMetal的源代码一个全局的认识. JMetal很好地利用了面向对象的特性,对所有的优化问题做了高度的抽象,建议有空 ...
- 设计模式---领域规则模式之解析器模式(Interpreter)
前提:领域规则模式 在特定领域内,某些变化虽然频繁,但可以抽象为某种规则.这时候,结合特定领域,将问题抽象为语法规则,从而给出该领域下的一般性解决方案. 典型模式 解析器模式:Interpreter ...