菜鸟学习Spring——60s利用JoinPoint获取參数的值和方法名称
一、概述
AOP的实现方法在上两篇博客中已经用了两种方法来实现如今的问题来了尽管我们利用AOP,那么client怎样信息传递?利用JoinPoint接口来实现client给详细实现类的传递參数。
二、代码演示。
文件夹结构:
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>效果图:
![]()
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3dibHVl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" align="middle" alt="">
三、总结。
我们能够通过Advice中加入一个JoinPoint參数,这个值会由spring自己主动传入。从JoinPoint中能够取得。
菜鸟学习Spring——60s利用JoinPoint获取參数的值和方法名称的更多相关文章
- 菜鸟学习Spring——60s利用JoinPoint获取参数的值和方法名称
一.概述 AOP的实现方法在上两篇博客中已经用了两种方法来实现现在的问题来了虽然我们利用AOP,那么客户端如何信息传递?利用JoinPoint接口来实现客户端给具体实现类的传递参数. 二.代码演示. ...
- 菜鸟学习Spring——60s配置XML方法实现简单AOP
一.概述. 上一篇博客讲述了用注解的形式实现AOP现在讲述另外一种AOP实现的方式利用XML来实现AOP. 二.代码演示. 准备工作参照上一篇博客<菜鸟学习Spring--60s使用annota ...
- spring aop 利用JoinPoint获取参数的值和方法名称
AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点 ...
- 菜鸟学习Spring——60s使用annotation实现简单AOP
一.概述. AOP大家都知道切面编程,在Spring中annotation可以实现简单的AOP列子.下面还未大家介绍几个概念: Aspect 对横切性关注点的模块化. Advice 对横切性关注点的具 ...
- 菜鸟学习Spring——60s让你学会动态代理原理
一.为什么要使用动态代理 当一个对象或多个对象实现了N中方法的时候,由于业务需求需要把这个对象和多个对象的N个方法加入一个共同的方法,比如把所有对象的所有方法加入事务这个时候有三种方法 ...
- 菜鸟学习Spring——60s学会Spring与Hibernate的集成
一.概述. Spring与Hibernate的集成在企业应用中是很常用的做法通过Spring和Hibernate的结合能提高我们代码的灵活性和开发效率,下面我就一步一步的给大家讲述Spring如何和H ...
- springMVC --@RequestParam注解(后台控制器获取參数)
在SpringMVC后台控制层获取參数的方式主要有两种,一种是request.getParameter("name"),第二种是用注解@RequestParam直接获取. 1.获取 ...
- 【web开发学习笔记】Structs2 Result学习笔记(三)带參数的结果集
Result学习笔记(三)带參数的结果集 第一部分:代码 //前端 <head> <meta http-equiv="Content-Type" content= ...
- php 获取数组第一个值的方法分享
以下是对使用php实现获取数组第一个值的方法进行了详细的分析介绍,需要的朋友可以过来参考下 reset (PHP 3, PHP 4, PHP 5)reset -- 将数组的内部指针指向第一个单元 说明 ...
随机推荐
- ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第五篇:MVC整合Ajax
摘要 本文将从完成“输入数据验证”这个功能出发,逐渐展开ASP.NET MVC与Ajax结合的方法.首先,本文将使用ASP.NET MVC提供的同步方式完成数据验证.而后,将分别结合ASP. ...
- DataTable +chart控件
//这是仿你的DataTable //-----开始--------- DataTable dataTable1 = new System.Data.DataTable(); dataTable1.C ...
- 01011_怎么打开任务管理器?win7打开任务管理器方法
以下几种方法可以打开任务管理器 1.在系统的任务栏点击鼠标右键,然后在弹出的右键菜单中选择“启动任务管理器”: 2.同时按下这三个按钮:Ctrl + Shift + Esc: 3.同时按下键盘的Ctr ...
- [Angular] Update FormArray with patchValue
Currently, patchValue doesn't support update FormArray. The workarround is you need to empty the for ...
- amazeui学习笔记--css(常用组件3)--按钮组Button-group
amazeui学习笔记--css(常用组件3)--按钮组Button-group 一.总结 1.按钮组用法:把一系列要使用的 .am-btn 按钮放入 .am-btn-group . 2.按钮工具栏: ...
- 就目前市面上的面试整理来说,最全的BAT大厂面试题整理在这
原标题:就目前市面上的面试整理来说,最全的BAT大厂面试题整理在这 又到了面试求职高峰期,最近有很多网友都在求大厂面试题.正好我之前电脑里面有这方面的整理,于是就发上来分享给大家. 这些题目是网友去百 ...
- GridView与ArrayAdapter的结合
activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- Hadoop读书笔记(一)Hadoop介绍
1.Hadoop是什么? 适合大数据的分布式与计算平台. HDFS:Hadoop Distributed File System 分布式文件系统 MapReduce:并行计算框架 2.HDFS的架构 ...
- 《机器学习实战》---第二章 k近邻算法 kNN
下面的代码是在python3中运行, # -*- coding: utf-8 -*- """ Created on Tue Jul 3 17:29:27 2018 @au ...
- 8.2 Android灯光系统_led_class驱动
android-5.0.2\hardware\libhardware\include\hardware\lights.h //系统一些宏定义 android源码只带的灯光驱动在linux内核的dri ...