菜鸟学习Spring——60s利用JoinPoint获取参数的值和方法名称
一、概述
AOP的实现方法在上两篇博客中已经用了两种方法来实现现在的问题来了虽然我们利用AOP,那么客户端如何信息传递?利用JoinPoint接口来实现客户端给具体实现类的传递参数。
二、代码演示。
目录结构:
SecurityHandler.java
package com.tgb.spring; import org.aspectj.lang.JoinPoint; public class SecurityHandler{ private void checkSecurity(JoinPoint joinPoint){
for (int i = 0; i < joinPoint.getArgs().length; i++) {
System.out.println(joinPoint.getArgs()[i]);
}
System.out.println(joinPoint.getSignature().getName()); System.out.println("=====checkSecurity===="); } }Client.java
package com.tgb.spring; import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tgb.spring.UserManager; public class Client { public static void main(String[] args) {
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager=(UserManager) factory.getBean("userManager");
userManager.addUser("张三", "123");
//userManager.delUser(1); }
}UserManager.java
package com.tgb.spring; public interface UserManager { public void addUser(String username,String password); public void delUser(int userId); public String findUserById(int userId); public void modifyUser(int userId,String username,String password); }UserManagerImpl.java
package com.tgb.spring; public class UserManagerImpl implements UserManager { public void addUser(String username, String password) {
//checkSecurity();
System.out.println("===UserManager.addUser==="); } public void delUser(int userId) {
//checkSecurity();
System.out.println("===UserManager.delUser==="); } public String findUserById(int userId) {
//checkSecurity();
System.out.println("===UserManager.findUserById===");
return "张三";
} public void modifyUser(int userId, String username, String password) {
//checkSecurity();
System.out.println("===UserManager.modifyUser==="); } // private void checkSecurity(){
// System.out.println("checkSecurity");
//
// } }applicationContext.xml
<?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="userManager" class="com.tgb.spring.UserManagerImpl" />
<bean id="securityHandler" class="com.tgb.spring.SecurityHandler"/>
<aop:config>
<aop:aspect id="securityAspect" ref="securityHandler"> <aop:pointcut id="addAddMethod" expression="execution(* com.tgb.spring.*.*(..))" />
<aop:before method="checkSecurity" pointcut-ref="addAddMethod" />
</aop:aspect>
</aop:config>
</beans>效果图:
三、总结。
我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得。
菜鸟学习Spring——60s利用JoinPoint获取参数的值和方法名称的更多相关文章
- spring aop 利用JoinPoint获取参数的值和方法名称
AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点 ...
- 菜鸟学习Spring——60s利用JoinPoint获取參数的值和方法名称
一.概述 AOP的实现方法在上两篇博客中已经用了两种方法来实现如今的问题来了尽管我们利用AOP,那么client怎样信息传递?利用JoinPoint接口来实现client给详细实现类的传递參数. 二. ...
- 菜鸟学习Spring——60s配置XML方法实现简单AOP
一.概述. 上一篇博客讲述了用注解的形式实现AOP现在讲述另外一种AOP实现的方式利用XML来实现AOP. 二.代码演示. 准备工作参照上一篇博客<菜鸟学习Spring--60s使用annota ...
- Spring MVC 不能正常获取参数的值
最近在开发时遇到一个非常奇怪的问题,在tomcat8中使用Spring MVC框架,在Controller中的方法参数无法正常获取到相应的值,将tomcat版本换成7.0就解决了. 记录以下解决过程, ...
- 菜鸟学习Spring——60s使用annotation实现简单AOP
一.概述. AOP大家都知道切面编程,在Spring中annotation可以实现简单的AOP列子.下面还未大家介绍几个概念: Aspect 对横切性关注点的模块化. Advice 对横切性关注点的具 ...
- 菜鸟学习Spring——60s让你学会动态代理原理
一.为什么要使用动态代理 当一个对象或多个对象实现了N中方法的时候,由于业务需求需要把这个对象和多个对象的N个方法加入一个共同的方法,比如把所有对象的所有方法加入事务这个时候有三种方法 ...
- 菜鸟学习Spring——60s学会Spring与Hibernate的集成
一.概述. Spring与Hibernate的集成在企业应用中是很常用的做法通过Spring和Hibernate的结合能提高我们代码的灵活性和开发效率,下面我就一步一步的给大家讲述Spring如何和H ...
- python 分享一个通过 (key1.key2.key3) 形式获取嵌套字典值的方法
最近在做接口自动化测试,响应的内容大多数是多层嵌套的json数据,如果一层层的去剥,效率不高,脚本繁重,所以写了一个可以通过(key1.key2.key3)形式获取嵌套字典值的方法,如有不对或者需要优 ...
- 【ABAP系列】SAP ABAP获取域(domain)值的方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP获取域(doma ...
随机推荐
- linux启动init流程(转)
当系统启动时,首先启动内核,内核调用init来完成引导进程.init启动时,它会在/etc/inittab内查找默认的运行级别:如id:2:initdefault:运行/etc/rc.d/init.d ...
- Android Studio使用中的小常识
1.如何继承抽象类? 1.1新建一个类如下: public class PersonDBOpenHelper{ } 1.2手写extends 你继承的类名 public class PersonDBO ...
- 第三方框架之SDWebImage
1. 下载SDWebImage,导入工程.github托管地址https://github.com/rs/SDWebImage 2. 在需要的地方导入头文件#import "UIImageV ...
- 洛谷P1207 [USACO1.2]双重回文数 Dual Palindromes
P1207 [USACO1.2]双重回文数 Dual Palindromes 291通过 462提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 暂时没有讨论 ...
- typedef 与define 的区别
typedef和#define的用法与区别 typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译 ...
- [JFinal 2] JFinal 开发框架
导读:在这次和大家一起开发的今日开讲后台管理系统中,我们用的是JFinal框架.开始的时候,说是用SSH,心里一阵窃喜,刚刚做了网上商城的项目,对于这个框架还算是接触过了.JFinal却是个新货,心里 ...
- hbase 新增节点
关于Hbase的集群管理 http://www.linuxidc.com/Linux/2012-07/65909.htm 1.如果只增加集群的存储量,建议增加Hadoop datanode节点. 方法 ...
- 学习BFC
BFC全称是Block Formatting Context,即块格式化上下文.它是CSS2.1规范定义的,关于CSS渲染定位的一个概念.要明白BFC到底是什么,首先来看看什么是视觉格式化模型. 视觉 ...
- Linux日志
一.Linux 常見的日志文件 /var/log/cron:你的 crontab 排程有没有实际被进行? 进行过程有没有发生错误?你的 /etc/crontab 是否撰写正确?在这个登录档内查询看看. ...
- 二十一、contextMap中放的常用数据
二十一.contextMap中放的常用数据 request:请求范围的数据.即ServletRequest中的那个Map parameters:请求参数的数据.即request.getParamete ...