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 ...
随机推荐
- Redis需要多少内存预留-内存占用多少才安全
转: Redis需要多少内存预留-内存占用多少才安全 2018年02月10日 18:13:37 常城 阅读数:10280 版权声明:本文为博主原创文章,未经博主允许不得转载. https://bl ...
- struts2 OGNL配和通用标签和其它标签的使用
三.OGNL配合通用标签的其他使用 1.iterator标签(很重要) 动作类 package com.itheima.web.action; import java.util.ArrayList; ...
- tensor flow中summary用法总结
对于用法的总结详细的参见博文https://www.cnblogs.com/lyc-seu/p/8647792.html
- id选择器为变量时
使用angularjs或者freemarker的同学基本都接触过一个问题:当使用list遍历数组值显示在页面,并要对所显示的数值进行操作时,如何选取数值所在标签? 以下是一个把地址id转为中文地址的函 ...
- Jquery Mobile事件
Jquery Mobile事件参考手册 on()方法用于添加事件处理程序 1.Touch类事件 在用户触摸屏幕时触发 1.1 tap事件 用户敲击某个元素时发生 $("p").on ...
- 100.Same Tree(E)
100. same tree 100. Same Tree Given two binary trees, write a function to check if they are the same ...
- Ajxa验证用户和二级联动的实例(五)
验证用户: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...
- M1-Flask-Day3
内容概要: websocket mysql连接池 sqlalchemy flask-sqlalchemy 练习: 1. 谈谈Flask和Django的认识? Django大而全的框架,把Web相关设计 ...
- Java抽象类、接口整理
抽象类 5.1抽象类产生(上标为A) 编写一个类时,会给该类定义一些方法,这些方法是用来描述功能和具体实现的方式,这些方法都有方法体 例如:一个图形类应该有周长的方法,但是不同的图形求周长方法不一样. ...
- TCP详解——连接建立与断开
一.报文结构介绍 在开始讲TCP连接过程时,还是先看看TCP报文的格式如图1所示.IP数据报此时由IP头部+TCP头部+TCP数据组成.不带选项的TCP头部是20字节长,而带选项的,TCP头部最长可达 ...