Spring代理处理参数和方法

本文介绍如何使用 Spring AOP 在 UserManager 接口的方法调用前进行参数和方法名的检查,确保安全性。通过配置 aspect 和 pointcut,实现对 addUser 方法的前置通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring代理处理参数和方法:

 

  1. package com.rx.spring.dynamicmock;
  2. public interface UserManager {
  3.     public void addUser(String username, String password);
  4.     
  5.     public void deleteUser(int id);
  6.     
  7. }

 

  1. package com.rx.spring.dynamicmock;
  2. public class UserManagerImpl implements UserManager {
  3.     public void addUser(String username, String password) {
  4.         System.out.println("-------UserManagerImpl.addUser()----------");
  5.     }
  6.     public void deleteUser(int id) {
  7.         System.out.println("-------UserManagerImpl.deleteUser()----------");
  8.     }
  9. }

 

  1. package com.rx.spring.dynamicmock;
  2. import org.aspectj.lang.JoinPoint;
  3. public class SecurityHandler {
  4.     
  5.     /***
  6.      * 处理连接点
  7.      * @param joinPoint 
  8.      */
  9.     private void checkSecurity(JoinPoint joinPoint) {
  10.         
  11.         Object[] args = joinPoint.getArgs();
  12.         for (int i=0; i<args.length; i++) {
  13.             System.out.println(args[i]);
  14.         }
  15.         
  16.         System.out.println(joinPoint.getSignature().getName());
  17.         
  18.         System.out.println("----------checkSecurity()---------------");
  19.     }
  20.     
  21. }

 

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

 

  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.     
  10.     <bean id="securityHandler" class="com.rx.spring.dynamicmock.SecurityHandler"/>           
  11.     
  12.     <bean id="userManager" class="com.rx.spring.dynamicmock.UserManagerImpl"/>
  13.     
  14.     <aop:config>
  15.         <aop:aspect id="security" ref="securityHandler">
  16.             <aop:pointcut id="allAddMethod" expression="execution(* com.rx.spring.dynamicmock.UserManagerImpl.add*(..))"/>
  17.             <aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>
  18.         </aop:aspect>
  19.     </aop:config>   
  20. </beans>

运行结果:

  1. abc
  2. 123
  3. addUser
  4. ----------checkSecurity()---------------
  5. -------UserManagerImpl.addUser()----------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值