代理的三种配置

beans配置文件

<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <bean id="IStudentService" class="com.my.servic.impl.StudentServiceImpl"></bean>
<bean id="IEmpService" class="com.my.servic.impl.EmpServiceImpl"></bean>
<bean id="logAdivce" class="com.my.advice.LogAdivce"></bean>
<bean id="customerAdvice" class="com.my.advice.CustomerAdvice"></bean>
<bean id="annotationAdvice" class="com.my.advice.AnnotationAdvice"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.my.servic.impl.*.*(..))"
id="pointcut" />
<aop:advisor advice-ref="logAdivce" pointcut-ref="pointcut" />
</aop:config>
<!-- 自定义配置通知 --> <aop:config>
<aop:aspect ref="customerAdvice">
<aop:pointcut expression="execution(*
com.my.servic.impl.*.add(..))"
id="pointcut1" />
<aop:pointcut expression="execution(*
com.my.servic.impl.*.delete(..))"
id="pointcut2" />
<aop:around method="roundMethod" pointcut-ref="pointcut1" />
<aop:before method="methodBefore" pointcut-ref="pointcut1" />
<aop:after method="methodAfter" pointcut-ref="pointcut2" />
</aop:aspect>
</aop:config> <!-- 扫描注解 组件 -->
<context:component-scan base-package="*"></context:component-scan> <!-- 注解方式 -->
<aop:aspectj-autoproxy /> <!-- 使用ProxyFactoryBean --> <bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>com.my.servic.IStudentService</value>
<value>com.my.servic.IEmpService</value>
</list>
</property> <property name="target">
<ref local="IStudentService" />
</property>
<property name="interceptorNames">
<list>
<value>logAdivce</value>
</list>
</property>
</bean>
</beans>

注解写法

package com.my.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; @Component
@Aspect
public class AnnotationAdvice { @Before("execution(* com.my.servic.impl.*.*(..))")
public void before(JoinPoint jp){
System.out.println("方法调用前。。");
System.out.println(jp.getTarget()+" +++>>> "+jp.getSignature()+" "+jp.getArgs());
} @Around("execution(* com.my.servic.impl.*.*(..))")
public void round(ProceedingJoinPoint pjp){
System.out.println("环绕前。。");
try {
pjp.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("环绕后前。。");
} }

自定义写法

package com.my.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class CustomerAdvice { public void methodBefore(JoinPoint jp){
System.out.println("方法调用前。。");
System.out.println(jp.getTarget()+" +++>>> "+jp.getSignature()+" "+jp.getArgs()); } public void methodAfter(JoinPoint jp){
System.out.println("方法调用后。。");
} public void roundMethod(ProceedingJoinPoint pjp){
System.out.println("环绕前。。"); try {
pjp.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("环绕后。。");
} }

常规实现接口写法

package com.my.advice;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice; import com.my.bean.Emp;
import com.my.bean.Student; public class LogAdivce implements MethodBeforeAdvice, AfterReturningAdvice,
MethodInterceptor { @Override
public void before(Method method, Object[] arg1, Object target)
throws Throwable { SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println("目标对象" + target.getClass().getName() + "的"
+ method.getName() + " 在" + sf.format(new Date()) + " 被调用了"); for (Object obj : arg1) { if (obj.getClass().getName().equals("com.my.bean.Student")) {
Student stu = (Student) obj;
System.out.println("参数为:" + stu.getName());
} else if (obj.getClass().getName().equals("com.my.bean.Emp")) {
Emp e = (Emp) obj;
System.out.println("参数为:" + e.getName());
}
}
} @Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
// TODO Auto-generated method stub
} @Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("环绕前。。");
Object obj = methodInvocation.proceed();
System.out.println("环绕后。。");
return obj;
} }

JavaEE笔记(十二)的更多相关文章

  1. 《C++游戏开发》笔记十二 战争迷雾:初步实现

    本系列文章由七十一雾央编写,转载请注明出处.  http://blog.csdn.net/u011371356/article/details/9475979 作者:七十一雾央 新浪微博:http:/ ...

  2. python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL

    python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL实战例子:使用pyspider匹配输出带.html结尾的URL:@config(a ...

  3. Go语言学习笔记十二: 范围(Range)

    Go语言学习笔记十二: 范围(Range) rang这个关键字主要用来遍历数组,切片,通道或Map.在数组和切片中返回索引值,在Map中返回key. 这个特别像python的方式.不过写法上比较怪异使 ...

  4. DirectX11笔记(十二)--Direct3D渲染8--EFFECTS

    原文:DirectX11笔记(十二)--Direct3D渲染8--EFFECTS 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010333737 ...

  5. java jvm学习笔记十二(访问控制器的栈校验机制)

    欢迎装载请说明出处:http://blog.csdn.net/yfqnihao 本节源码:http://download.csdn.net/detail/yfqnihao/4863854 这一节,我们 ...

  6. (C/C++学习笔记) 十二. 指针

    十二. 指针 ● 基本概念 位系统下为4字节(8位十六进制数),在64位系统下为8字节(16位十六进制数) 进制表示的, 内存地址不占用内存空间 指针本身是一种数据类型, 它可以指向int, char ...

  7. 《深入理解Java虚拟机》读书笔记十二

    第十二章  Java内存模型与线程 1.硬件效率与一致性 由于计算机的存储设备与处理器的运算速度有几个数量级的差距,所以现代计算机系统都不得不加入一层读写速度尽可能接近处理器运算速度的高速缓存(Cac ...

  8. swift 笔记 (十二) —— 下标

    下标 swift同意我们为 类.结构体,枚举 定义下标,以更便捷的方式訪问一大堆属性.比方Array和Dictionary都是结构体,swift的project师已经为这两个类型提供好了下标操作的代码 ...

  9. JavaScript权威设计--命名空间,函数,闭包(简要学习笔记十二)

    1.作为命名空间的函数 有时候我们需要声明很多变量.这样的变量会污染全局变量并且可能与别人声明的变量产生冲突. 这时.解决办法是将代码放入一个函数中,然后调用这个函数.这样全局变量就变成了 局部变量. ...

  10. MySQL学习笔记十二:数据备份与恢复

    数据备份 1.物理备份与逻辑备份 物理备份 物理备份就是将数据库的数据文件,配置文件,日志文件等复制一份到其他路径上,这种备份速度一般较快,因为只有I/O操作.进行物理备份时,一般都需要关闭mysql ...

随机推荐

  1. Unity Frame Debugger连接Android真机调试

    当用Profiler分析到不是代码导致的性能问题,当前场景最大的性能瓶颈是渲染时,或者自己写的Shader要调试时,都可以用Frame Debugger进行调试. 按下列步骤设置打包,既可以用Prof ...

  2. Unity Profiler GPU Usage(GPU使用情况)

    一般情况下性能瓶颈都在CPU上,这儿也列举下几个常见的GPU耗时函数吧. 1 Render.Mesh 绘制网格面(没批处理的面) 2 Batch.DrawStatic 静态批处理 3 Batch.Dr ...

  3. 解决 ModuleNotFoundError: No module named 'pip'

    安装其它python包时,提示说 pip 10.0.1可用,就更新了一下,但是 更新过程中出现了错误,如图所示 因为这个错误导致 pip找不到, 可以首先执行  python -m ensurepip ...

  4. 【SPL标准库专题(4)】 Datastructures:SplDoublyLinkedList

    简述 双链表是一种重要的线性存储结构,对于双链表中的每个节点,不仅仅存储自己的信息,还要保存前驱和后继节点的地址. 类摘要 SplDoublyLinkedList implements Iterato ...

  5. c#中(&&,||)与(&,|)的区别和应用

    对于(&&,||),运算的对象是逻辑值,也就是True/False &&相当与中文的并且,||相当于中文的或者 .(叫做逻辑运算符又叫短路运算符) 运算结果只有下列四种 ...

  6. [SQLServer大对象]——FileTable初体验 (转载)

    阅读导航启用FILESTREAM设置更改FILESTRAM设置启用数据库非事务性访问级别FileTable 在我接触FileTable之前,存储文件都是存储文件的链接和扩展名到数据,其实并没有实际的把 ...

  7. excel如何冻结首行或首列及首行首列同时冻结

    冻结首行方法: 首先选择首行,在菜单栏选择视图菜单,再选择冻结窗格下拉三角里的冻结首行即可. 效果如下:拖动垂直滚动条 冻结首列方法: 首先选择首列,在菜单栏选择视图菜单,再选择冻结窗格下拉三角里的冻 ...

  8. 【转载】sql注入之入门

    原文在:https://smelond.com MySql基础语法 mysql无非就是增删改查 mysql数据库结构: 数据库 test,test1 表名 admin,manage 数据 id,use ...

  9. 向kindle传送文件

    自从有了kindle以后,只要不是pdf的电子书,基本上都用kindle来看了,毕竟水墨屏还是很护眼的,但是这就带来了向kindle传输文件的问题 绝大部分的电子书用邮件推送就可以解决了,但是还有一些 ...

  10. ConstraintLayout使用手册

    1. 解决痛点 主要用拖拽 解决嵌套过多 2. 简易使用手册 增加约束 四个角直接拖拽就好了 删除约束 match_constraint 属性 这个属性类似于match_parent,去掉margin ...