您的位置:首页 > 编程语言 > Java开发

spring Aop 配置文件方式+JoinPoint获取参数

2019-01-29 14:34 621 查看
               

 

<?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">
 <!-- <aop:aspectj-autoproxy/> -->不需要这个
 
 <bean id="securityHandler" class="annotationSecurityHandler.SecurityHandler" />
 <bean id="securityHandler1" class="annotationSecurityHandler.SecurityHandler1" />
 <bean id="userManager" class="managerImpl.UserManagerImpl" />
 <!-- 配置aspect -->
 <aop:config>
 <!-- 定义切面 -->
  <aop:aspect id="security" ref="securityHandler1">
  <!-- 定义pointcut,并写表达式 -->
   <aop:pointcut id="allMethod" expression="execution(* managerImpl.UserManagerImpl.add*(..))|| execution(* managerImpl.UserManagerImpl.del*(..))"/>
   <!-- 定义advice,织入pointcut -->
   <aop:before method="checkSecurity" pointcut-ref="allMethod"/>
  </aop:aspect>
 </aop:config>
 </beans>

======================SecurityHandler1 .java==============================

package annotationSecurityHandler;

import org.aspectj.lang.JoinPoint;

public class SecurityHandler1 {

 private void checkSecurity(JoinPoint j){//取得代理类传递的参数
  Object obj[] = j.getArgs();
  for(Object o :obj){
   System.out.println(o);
  }
  System.out.println("========checkSecurity=="+j.getSignature().getName());//这个是获得方法名
 }
}

===============test.java==================================

public static void main(String[] args) {
  BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
  UserManager userManager = (UserManager) beanFactory.getBean("userManager");
  userManager.addUser("123", "aaa");
  userManager.deleteUserById(1);
 }

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: