[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的更多相关文章

  1. Spring4 AOP详解

    Spring4 AOP详解 第一章Spring 快速入门并没有对Spring4 的 AOP 做太多的描述,是因为AOP切面编程概念不好理解.所以这章主要从三个方面详解AOP:AOP简介(了解),基于注 ...

  2. Spring Aop的执行顺序

    Spring Aop的执行顺序 首先回忆一下 AOP 的常用注解 @Before:前置通知:目标方法之前执行 @After:后置通知:目标方法之后执行 @AfterReturning:返回后通知:执行 ...

  3. Spring 4 bak

    IOC (参考<Spring企业开发>.<Spring实战 第三版  第四版>) IoC概述 1.           控制反转 2.依赖注入   控制反转:大多数情况下,想要 ...

  4. Spring4学习笔记-AOP

    1.加入jar包 com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEAS ...

  5. Spring4.0学习笔记(10) —— Spring AOP

    个人理解: Spring AOP 与Struts 的 Interceptor 拦截器 有着一样的实现原理,即通过动态代理的方式,将目标对象与执行对象结合起来,降低代码之间的耦合度,主要运用了Proxy ...

  6. Spring4笔记9--Spring的事务管理(AOP应用的例子)

    Spring的事务管理: 事务原本是数据库中的概念,在 Dao 层.但一般情况下,需要将事务提升到业务层,即 Service 层.这样做是为了能够使用事务的特性来管理具体的业务.   在 Spring ...

  7. Spring4笔记7--AspectJ 对 AOP 的实现

    AspectJ 对 AOP 的实现: 对于 AOP 这种编程思想,很多框架都进行了实现.Spring 就是其中之一,可以完成面向切面编程.然而,AspectJ 也实现了 AOP 的功能,且其实现方式更 ...

  8. Spring4笔记6--Spring与AOP

    Spring与AOP: AOP的引入: 主业务经常需要调用系统级业务(交叉业务),如果在主业务代码中大量的调用系统级业务代码,会使系统级业务与主业务深度耦合在一起,大大影响了主业务逻辑的可读性,降低了 ...

  9. 峰Spring4学习(6)spring AOP的应用例子

    一.AOP简介: 二.AOP实例: 三.使用的例子 需求:在student添加的前后,打印日志信息: 0)spring AOP需要引用的jar包: 1)StudentService.java接口: p ...

随机推荐

  1. PHP中的数据结构:DS扩展

    PHP7以上才能安装和使用该数据结构扩展,安装比较简单: 1. 运行命令 pecl install ds 2. 在php.ini中添加 extension=ds.so 3. 重启PHP或重载配置 在这 ...

  2. RabbitMQ入门-发布订阅模式

    兔子的Publish/Subscribe是这样的: 有个生产者P,X代表交换机,交换机绑定队列,消费者从队列中取得消息.每次有消息,先发到交换机中,然后由交换机负责发送到它已知的队列中. 生产者代码: ...

  3. 安装webpack-dev-server始终不成功

    先安装了webpack,后来安装webpack-dev-server会一直出现这个问题,我把webpack提示的1.0.0 , 2.0.0 ,3.0.0全都在全局装了一遍都没用,还是会出现这个问题.最 ...

  4. HDU 1003 Max Sum 求区间最大值 (尺取法)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  5. (set) 人见人爱A-B hdu2034

    人见人爱A-B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  6. php将用户信息提交到表单并且以txt文档打印出来

    1.分析: ====不推荐这种======== <?php function foo(){ // global $message; if(empty($_POST['username'])){ ...

  7. 你一直在找的 Mac 系统优化工具,可能就是这一款!

    文章来源:知乎 文章收录于:风云社区 www.scoee.com,提供上千款各类mac软件下载 尽管 Mac 上的维护工具总会被人诟病(通常是那些极客),但对于普通用户来说,像 CleanMyMac. ...

  8. docker 基础之私有仓库

    docker-registry 是官方提供的工具,可以用于构建私有的镜像仓库.安装运行 docker-registry容器 在安装了 Docker 后,可以通过获取官方 registry 镜像来运行. ...

  9. MySQL入门介绍(mysql-8.0.13)

    MySQL入门介绍(mysql-8.0.13单机部署) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL数据库介绍 1>.MySQL是一种开放源代码的关系型数据库 ...

  10. ruby select 方法,可用于先查询结果后,再次用条件限制

    1. 用于条件过滤 @works=DworkPro.all.order(:work_type) @work_pro=@works.select{ |x| x.job_type == 7} 2. sel ...