三、总结。

我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得。

三、总结。

我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得。

三、总结。

我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得。

一、概述

AOP的实现方法在上两篇博客中已经用了两种方法来实现现在的问题来了虽然我们利用AOP,那么客户端如何信息传递?利用JoinPoint接口来实现客户端给具体实现类的传递参数。

二、代码演示。

目录结构:

SecurityHandler.Java

  1. package com.tgb.spring;
  2. import org.aspectj.lang.JoinPoint;
  3. public class SecurityHandler{
  4. private void checkSecurity(JoinPoint joinPoint){
  5. for (int i = 0; i < joinPoint.getArgs().length; i++) {
  6. System.out.println(joinPoint.getArgs()[i]);
  7. }
  8. System.out.println(joinPoint.getSignature().getName());
  9. System.out.println("=====checkSecurity====");
  10. }
  11. }

Client.java

  1. package com.tgb.spring;
  2. import org.springframework.beans.factory.BeanFactory;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import com.tgb.spring.UserManager;
  5. public class Client {
  6. public static void main(String[] args) {
  7. BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
  8. UserManager userManager=(UserManager) factory.getBean("userManager");
  9. userManager.addUser("张三", "123");
  10. //userManager.delUser(1);
  11. }
  12. }

UserManager.java

  1. package com.tgb.spring;
  2. public interface UserManager {
  3. public void addUser(String username,String password);
  4. public void delUser(int userId);
  5. public String findUserById(int userId);
  6. public void modifyUser(int userId,String username,String password);
  7. }

UserManagerImpl.java

  1. package com.tgb.spring;
  2. public class UserManagerImpl implements UserManager {
  3. public void addUser(String username, String password) {
  4. //checkSecurity();
  5. System.out.println("===UserManager.addUser===");
  6. }
  7. public void delUser(int userId) {
  8. //checkSecurity();
  9. System.out.println("===UserManager.delUser===");
  10. }
  11. public String findUserById(int userId) {
  12. //checkSecurity();
  13. System.out.println("===UserManager.findUserById===");
  14. return  "张三";
  15. }
  16. public void modifyUser(int userId, String username, String password) {
  17. //checkSecurity();
  18. System.out.println("===UserManager.modifyUser===");
  19. }
  20. //  private void checkSecurity(){
  21. //      System.out.println("checkSecurity");
  22. //
  23. //  }
  24. }

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  9. <bean id="userManager" class="com.tgb.spring.UserManagerImpl" />
  10. <bean id="securityHandler" class="com.tgb.spring.SecurityHandler"/>
  11. <aop:config>
  12. <aop:aspect id="securityAspect" ref="securityHandler">
  13. <aop:pointcut id="addAddMethod" expression="execution(* com.tgb.spring.*.*(..))" />
  14. <aop:before method="checkSecurity" pointcut-ref="addAddMethod" />
  15. </aop:aspect>
  16. </aop:config>
  17. </beans>

效果图:

三、总结。

我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得。

本文转自http://blog.csdn.net/gwblue/article/details/40592117 感谢作者

spring aop通过joinpoint传递参数的更多相关文章

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

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

  2. Spring Aop——给Advice传递参数

    给Advice传递参数 Advice除了可以接收JoinPoint(非Around Advice)或ProceedingJoinPoint(Around Advice)参数外,还可以直接接收与切入点方 ...

  3. Spring AOP切面的时候参数的传递

    Spring AOP切面的时候参数的传递 Xml: <?xml version="1.0" encoding="UTF-8"?> <beans ...

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

    一.概述 AOP的实现方法在上两篇博客中已经用了两种方法来实现现在的问题来了虽然我们利用AOP,那么客户端如何信息传递?利用JoinPoint接口来实现客户端给具体实现类的传递参数. 二.代码演示. ...

  5. 使用Spring AOP预处理Controller的参数

    实际编程中,可能会有这样一种情况,前台传过来的参数,我们需要一定的处理才能使用,比如有这样一个Controller @Controller public class MatchOddsControll ...

  6. Spring AOP中JoinPoint的用法

    Spring JoinPoint的用法 JoinPoint 对象 JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的 ...

  7. Spring AOP获取方法的参数名称和参数值

    aop配置: <aop:aspectj-autoproxy expose-proxy="true" /> @Before(value = "execution ...

  8. Spring Aop 修改目标方法参数和返回值

    一.新建注解 @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Document ...

  9. spring aop 解决模糊查询参数 % - /等特殊符号问题

    import com.hsq.common.utils.StringUtil;import org.aspectj.lang.ProceedingJoinPoint;import org.aspect ...

随机推荐

  1. “~/Views/Home/Text.aspx”处的视图必须派生自 ViewPage、ViewPage<TModel>、ViewUserControl 或 ViewUserControl<TModel>。

    在MVC架构中使用aspx页面,需要在Text.aspx中开头加入如下代码: <%@ Page Language="C#" Inherits="System.Web ...

  2. php获取系统信息的方法

    php获取系统信息的方法. 用 getenv函数进行处理: <?php $root = getenv('DOCUMENT_ROOT'); ////服务器文档根目录 $port = getenv( ...

  3. 感知器算法PLA

    for batch&supervised binary classfication,g≈f <=> Eout(g)≥0 achieved through Eout(g)≈Ein(g ...

  4. 机器学习实战——k-邻近算法:约会网站

    1.kNN 算法 算法说明: set<X1,X2……Xn> 为已知类别数据集,预测 点Xt 的类别: (1)计算中的set中每一个点与Xt的距离 (2)按距离增序排列 (3)选择距离最小的 ...

  5. Ubuntu 14.04安装Chromium浏览器并添加Flash插件Pepper Flash Player

    安装方法Ubuntu 14.04及衍生版本用户命令: 因为默认库里面有Chromium和Pepper Flash Player,安装非常容易,打开终端,输入以下命令: sudo apt-get upd ...

  6. 101个MySQL的调节和优化的Tips

    MySQL 是一个功能强大的开源数据库.随着越来越多的数据库驱动的应用程序,人们一直在推动MySQL发展到它的极限.这里是101条调节和优化MySQL安装的技巧.一些技巧是针对特定的安装环境的,但这些 ...

  7. creating indexing for SQL tunning

    1. Not so long time ago, I got a report from customer. It's reported that they had a report getted v ...

  8. iOS 10 版本适配问题收集-b

    随着iOS10发布的临近,大家的App都需要适配iOS10,下面是我总结的一些关于iOS10适配方面的问题,如果有错误,欢迎指出. 1.系统判断方法失效: 在你的项目中,当需要判断系统版本的话,不要使 ...

  9. 百度地图API使用

    1.引用js脚本 <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&am ...

  10. c# 取 list前100条数据

    [问] List<KeyWord> sortedList = (from a in keyWordList orderby a.Total descending select a).ToL ...