spring aop通过joinpoint传递参数
三、总结。
我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得。
三、总结。
我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得。
三、总结。
我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从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中可以取得。
本文转自http://blog.csdn.net/gwblue/article/details/40592117 感谢作者
spring aop通过joinpoint传递参数的更多相关文章
- spring aop 利用JoinPoint获取参数的值和方法名称
AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点 ...
- Spring Aop——给Advice传递参数
给Advice传递参数 Advice除了可以接收JoinPoint(非Around Advice)或ProceedingJoinPoint(Around Advice)参数外,还可以直接接收与切入点方 ...
- Spring AOP切面的时候参数的传递
Spring AOP切面的时候参数的传递 Xml: <?xml version="1.0" encoding="UTF-8"?> <beans ...
- 菜鸟学习Spring——60s利用JoinPoint获取参数的值和方法名称
一.概述 AOP的实现方法在上两篇博客中已经用了两种方法来实现现在的问题来了虽然我们利用AOP,那么客户端如何信息传递?利用JoinPoint接口来实现客户端给具体实现类的传递参数. 二.代码演示. ...
- 使用Spring AOP预处理Controller的参数
实际编程中,可能会有这样一种情况,前台传过来的参数,我们需要一定的处理才能使用,比如有这样一个Controller @Controller public class MatchOddsControll ...
- Spring AOP中JoinPoint的用法
Spring JoinPoint的用法 JoinPoint 对象 JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的 ...
- Spring AOP获取方法的参数名称和参数值
aop配置: <aop:aspectj-autoproxy expose-proxy="true" /> @Before(value = "execution ...
- Spring Aop 修改目标方法参数和返回值
一.新建注解 @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Document ...
- spring aop 解决模糊查询参数 % - /等特殊符号问题
import com.hsq.common.utils.StringUtil;import org.aspectj.lang.ProceedingJoinPoint;import org.aspect ...
随机推荐
- php数组声明、遍历、数组全局变量使用小结
数组的本质:管理和操作一组变量,成批处理,下面为大家介绍下数组的分类.数组的分类及使用说明,感兴趣的朋友可以了解下哈 php教程:数组声明,遍历,数组全局变量 <? /* * 一.数组的概 ...
- mysql rand随机查询记录效率
一直以为mysql随机查询几条数据,就用 SELECT * FROM `table` ORDER BY RAND() LIMIT 5 就可以了. 但是真正测试一下才发现这样效率非常低.一个15万余条的 ...
- hdu 5451 Best Solver 矩阵循环群+矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=5451 题意:给定x 求解 思路: 由斐波那契数列的两种表示方法, 之后可以转化为 线性表示 F[n] = ...
- js 前加分号和感叹号是什么意思?
;!function(){}(); ;!有什么用? 从语法上来开,Javascript中分号表示语句结束,在开头加上,可能是为了压缩的时候和别的方法分割一下,表示一个新的语句开始.所以,如果在一个单 ...
- layoutSubviews -- setNeedsLayout -- layoutIfNeeded -- 区别
-layoutSubviews方法:这个方法,默认没有做任何事情,需要子类进行重写-setNeedsLayout方法: 标记为需要重新布局,异步调用layoutIfNeeded刷新布局,不立即刷新,但 ...
- C#WinForm中播放背景音乐(亲测可用)
using System.Runtime.InteropServices; public static uint SND_ASYNC = 0x0001; public static uint SND_ ...
- OpenCV之mixChannels()函数使用说明
step 1: 函数功能说明 mixChannels主要就是把输入的矩阵(或矩阵数组)的某些通道拆分复制给对应的输出矩阵(或矩阵数组)的某些通道中,其中的对应关系就由fromTo参数制定. step ...
- sqlserver 动态表名 动态字段名 执行 动态sql
动态语句基本语法: 1 :普通SQL语句可以用exec执行 Select * from tableName exec('select * from tableName') exec sp_execut ...
- C# 延迟初始化
一个对象的延迟初始化意味着该对象的创建将会延迟至第一次使用该对象时.(在本主题中,术语“延迟初始化”和“延迟实例化”是同义词.)延迟初始化主要用于提高性能,避免浪费计算,并减少程序内存要求. 以下是最 ...
- linux下php多版本的并存实现
其实最简单的方法,就是通过nginx,生成多个php使用不同的端口,这实在简单,我写了两个版本,一个是apche服务,一个是nginx服务,使用一两个不同的版本,爽!