spring中JoinPoint参数介绍
来源:互联网 发布:天津淘宝托管 编辑:程序博客网 时间:2024/05/21 16:58
import org.aspectj.lang.JoinPoint;public class SecurityHandler { private void checkSecurity(JoinPoint joinPoint) { Object[] args = joinPoint.getArgs(); for (int i=0; i<args.length; i++) { System.out.println(args[i]); } System.out.println(joinPoint.getSignature().getName()); System.out.println("----------checkSecurity()---------------"); } }
目标对象:
package com.bjsxt.spring;public class UserManagerImpl implements UserManager { public void addUser(String username, String password) { System.out.println("-------UserManagerImpl.addUser()----------"); } public void deleteUser(int id) { System.out.println("-------UserManagerImpl.deleteUser()----------"); } public String findUserById(int id) { System.out.println("-------UserManagerImpl.findUserById()----------"); return null; } public void modifyUser(int id, String username, String password) { System.out.println("-------UserManagerImpl.modifyUser()----------"); } // private void checkSecurity() {// System.out.println("----------checkSecurity()---------------");// }}spring上下文配置:
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="securityHandler" class="com.bjsxt.spring.SecurityHandler"/> <bean id="userManager" class="com.bjsxt.spring.UserManagerImpl"/> <aop:config> <aop:aspect id="security" ref="securityHandler"> <aop:pointcut id="allAddMethod" expression="execution(* com.bjsxt.spring.UserManagerImpl.add*(..)) || execution(* del*(..))"/> <aop:before method="checkSecurity" pointcut-ref="allAddMethod"/> </aop:aspect> </aop:config> </beans>
测试类
import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Client { public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); UserManager userManager = (UserManager)factory.getBean("userManager"); userManager.addUser("kangyue", "123"); userManager.deleteUser(1); }}
输出结果:
kangyue // 参数
123
addUser // 方法名
----------checkSecurity()---------------
-------UserManagerImpl.addUser()----------
1
deleteUser
----------checkSecurity()---------------
-------UserManagerImpl.deleteUser()----------
spring对AOP的支持1:
Aspect默认情况下不用实现接口,但对于目标对象,在默认情况下必须实现接口
如果没有实现接口必须引入CGLIB库
我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得
参数值、方法名等等
spring对AOP的支持2:
1、如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP
2、如果目标对象实现了接口,可以强制使用CGLIB实现AOP
3、如果目标对象没有实现了接口,必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换
在实现接口的情况下,如何强制使用CGLIB实现AOP?
* 添加CGLIB库,SPRING_HOME/cglib/*.jar
* 在spring配置文件中加入<aop:aspectj-autoproxy proxy-target-class="true"/>
JDK动态代理和CGLIB字节码生成的区别?
* JDK动态代理只能对实现了接口的类生成代理,而不能针对类
* CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法
因为是继承,所以该类或方法最好不要声明成final
- spring中JoinPoint参数介绍
- spring中JoinPoint的使用
- spring Aop 配置文件方式+JoinPoint获取参数
- spring Aop 配置文件方式+JoinPoint获取参数
- Aop 编程中joinpoint中参数列表
- spring中用joinpoint来访问目标方法的参数
- spring中用joinpoint来访问目标方法的参数
- spring aop 中 org.aspectj.lang.JoinPoint-中文简要API
- Spring AOP中JoinPoint的表达式定义描述
- spring aspectj JoinPoint
- spring aop JoinPoint 用法
- 菜鸟学习Spring——60s利用JoinPoint获取参数的值和方法名称
- spring aop 利用JoinPoint获取参数的值和方法名称
- spring aop 3 Spring AOP中JoinPoint的表达式定义描述
- aop 中joinpoint的使用方法
- 菜鸟学习Spring——60s利用JoinPoint(连接点)获取参数的值和方法名称()
- Spring之——JoinPoint对象
- spring sop 术语:Advice、JoinPoint、PointCut
- 解决Linux3.16内核下Bumblebee无法关闭显卡问题
- 手机页面禁止页面拖动
- LeetCode:Binary Tree Right Side View
- OpenCV学习笔记(6):滤波filter2D()
- Struts2文件上传
- spring中JoinPoint参数介绍
- HPUX中vg00根盘镜像配置方法
- Myeclipse中遇到的一些配置问题
- AFNnetworking 快速教程,官方入门教程译
- 《Netty 权威指南》—— 选择Netty的理由
- 【数据结构-链表】单链表
- sublime配置全攻略
- c# winform窗体 调用js脚本语言,然后输出结果
- For JingJing