一、概述

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获取参数的值和方法名称的更多相关文章

  1. spring aop 利用JoinPoint获取参数的值和方法名称

    AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点 ...

  2. 菜鸟学习Spring——60s利用JoinPoint获取參数的值和方法名称

    一.概述 AOP的实现方法在上两篇博客中已经用了两种方法来实现如今的问题来了尽管我们利用AOP,那么client怎样信息传递?利用JoinPoint接口来实现client给详细实现类的传递參数. 二. ...

  3. 菜鸟学习Spring——60s配置XML方法实现简单AOP

    一.概述. 上一篇博客讲述了用注解的形式实现AOP现在讲述另外一种AOP实现的方式利用XML来实现AOP. 二.代码演示. 准备工作参照上一篇博客<菜鸟学习Spring--60s使用annota ...

  4. Spring MVC 不能正常获取参数的值

    最近在开发时遇到一个非常奇怪的问题,在tomcat8中使用Spring MVC框架,在Controller中的方法参数无法正常获取到相应的值,将tomcat版本换成7.0就解决了. 记录以下解决过程, ...

  5. 菜鸟学习Spring——60s使用annotation实现简单AOP

    一.概述. AOP大家都知道切面编程,在Spring中annotation可以实现简单的AOP列子.下面还未大家介绍几个概念: Aspect 对横切性关注点的模块化. Advice 对横切性关注点的具 ...

  6. 菜鸟学习Spring——60s让你学会动态代理原理

    一.为什么要使用动态代理         当一个对象或多个对象实现了N中方法的时候,由于业务需求需要把这个对象和多个对象的N个方法加入一个共同的方法,比如把所有对象的所有方法加入事务这个时候有三种方法 ...

  7. 菜鸟学习Spring——60s学会Spring与Hibernate的集成

    一.概述. Spring与Hibernate的集成在企业应用中是很常用的做法通过Spring和Hibernate的结合能提高我们代码的灵活性和开发效率,下面我就一步一步的给大家讲述Spring如何和H ...

  8. python 分享一个通过 (key1.key2.key3) 形式获取嵌套字典值的方法

    最近在做接口自动化测试,响应的内容大多数是多层嵌套的json数据,如果一层层的去剥,效率不高,脚本繁重,所以写了一个可以通过(key1.key2.key3)形式获取嵌套字典值的方法,如有不对或者需要优 ...

  9. 【ABAP系列】SAP ABAP获取域(domain)值的方法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP获取域(doma ...

随机推荐

  1. MSP430F149学习之路——SPI

    代码一: //****************************************************************************** //Description: ...

  2. 使用HTTP协下载文件

    通过发送HTTP请求,下载文件 头文件: #ifndef __HTTP__ #define __HTTP__ #include <stdio.h> #include <stdlib. ...

  3. C#之泛型

    泛型是C# 2.0版本才有的语言特性,是具有参数类型占位符的类.结构.接口和方法.这些占位符是类.结构.接口和方法所存储或使用的一个或多个占位符.简单来说,就是变量类型的参数化. 以下是详细demo: ...

  4. 复杂的sql 一对多查询

    SELECT distinct       u.Name as UserName,       a.OrderId,       (       select  d.SignCity from T_O ...

  5. Atom markdown .md 编写格式技巧

    使用Atom预览markdown 1.打开任意.md文件(markdown源文件) 菜单栏File->Open file...(ctrl+o)打开文件: 2.windows下使用快捷键 ctrl ...

  6. hadoop-集群管理(2)——内存设置

    http://www.cnblogs.com/yuechaotian/archive/2013/03/08/2949607.html 1. 内存 hadoop为各个守护进程(namenode,seco ...

  7. NTP服务搭建

    1:实验环境 前提条件:虚拟机环境,windows 客户端,关闭防火墙:linux 服务器关闭防火墙,关闭selinux. Cent OS :NTP服务器 IP: 192.168.80.134  /2 ...

  8. UIButton设置imgae图片自适应button的大小且不变形

    在某些情况下,我们使用的UIButton的背景图片不一定就是标准的尺寸,有时会偏大,那么怎么办? 这个比较简单直接设置 :    self.imageView.contentMode = UIView ...

  9. Ubuntu系统下安装python2.7

    第一步:下载python2.7相关版本源码(例如:Python-2.7.4.tgz) 第二步:安装 1) 解压 $tar zxvf Python-2.7.4.tar.tgz 2)进入文件夹: $cd ...

  10. Oracle VM VirtualBox 设置本机与虚拟机共享文件

    文章转载自http://wenku.baidu.com/link?url=5uZ1xWnGC55FGW2XUqzLVdttRcik2xCqwRKEdh6z-nZJ63UQn3j2750ES_q4Oro ...