浅谈Spring静态切入点使用方法

2023-12-27 21:18

本文主要是介绍浅谈Spring静态切入点使用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

所谓spring静态切入点,相对于动态切入点来说,具有良好的性能,因为静态切入点只在代理创建时候执行一次,而不是在运行期间,每次目标方法执行前都进行执行,下面,以实例说明如何定义静态切入点

 

看我我前一篇blog的朋友都知道,如果不定义切入点,通知方法是会对整个目标类的所有方法均进行切入的
但实际需求中,我们可能对其中的几个方法执行A通知,对其他的方法执行B通知,这时候,就需要通过定义不同的切入点来进行区分

目标接口:

 

package  StaticAdvisorTest;

public   interface  Shopping  {
  
public String buySomething(String type);
  
public String buyAnything(String type);
  
public String sellSomething(String type);
  
public String sellAnything(String type);

}

 javabean:

 

package  StaticAdvisorTest;

public   class  Customer  {
  
private String name;
  
private String age;
  
public Customer(){
      
  }

  
public Customer(String name,String age){
      
this.name=name;
      
this.age=age;
  }

public String getAge() {
    
return age;
}

public void setAge(String age) {
    
this.age = age;
}

public String getName() {
    
return name;
}

public void setName(String name) {
    
this.name = name;
}

}

 

业务目标实现类:

 

package  StaticAdvisorTest;

public   class  ShoppingImpl  implements  Shopping  {
    
private Customer customer;
    
public Customer getCustomer() {
        
return customer;
    }

    
public void setCustomer(Customer customer) {
        
this.customer = customer;
    }

    
public String buySomething(String type) {
        System.out.println(
this.getCustomer().getName()+" bye "+type+" success");
        
return null;
    }

    
    
public String buyAnything(String type) {
       System.out.println(
this.getCustomer().getName()+" bye "+type+" success");
       
return null;

     }

    
public String sellAnything(String type) {
        System.out.println(
this.getCustomer().getName()+" sell "+type+" success");
        
return null;
    }

    
public String sellSomething(String type) {
         System.out.println(
this.getCustomer().getName()+" sell "+type+" success");
           
return null;
    }


}

 

通知(切面)方法:

 

package  StaticAdvisorTest;

import  java.lang.reflect.Method;

import  org.springframework.aop.MethodBeforeAdvice;
// 前置通知
public   class  WelcomeAdvice  implements  MethodBeforeAdvice  {

    
public void before(Method method, Object[] args, Object obj)
            
throws Throwable {
        String type
=(String)args[0];
        System.out.println(
"Hello welcome to buy "+type);

    }


}

 

下面是重点,我们想对所有的buy方法进行通知处理,也就是在所有的buy方法上定义切面

spring为我们创建了静态切入点的父类 StaticMethodMatcherPointCut ,如果我们想实现自制的静态切入点,只要继承这个类就可以了,不过一般情况下,我们使用spring提供的静态切入点NameMatchMethodPointCut就足够了

配置文件如下:

 

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"  >
< beans >
 
< bean  id ="customer"  class ="StaticAdvisorTest.Customer" >
   
< constructor-arg  index ="0" >
     
< value > gaoxiang </ value >
   
</ constructor-arg >
    
< constructor-arg  index ="1" >
     
< value > 26 </ value >
   
</ constructor-arg >
 
</ bean >
 
< bean  id ="shoppingImpl"  class ="StaticAdvisorTest.ShoppingImpl" >
   
< property  name ="customer" >
     
< ref  local ="customer" />
   
</ property >
 
</ bean >
<!--  定义通知  -->
< bean  id ="shoppingAdvise"  class ="StaticAdvisorTest.WelcomeAdvice" ></ bean >
<!--  定义切入点  -->
< bean  id ="shoppingPointCutAdvisor"  class ="org.springframework.aop.support.NameMatchMethodPointcutAdvisor" >
  
< property  name ="mappedName" >
    
< value > sell* </ value >
  
</ property >
  
< property  name ="advice" >
    
< ref  bean ="shoppingAdvise" />
  
</ property >
</ bean >
<!--  定义代理  -->
< bean  id ="StaticAdvisorTest"  class ="org.springframework.aop.framework.ProxyFactoryBean" >
  
< property  name ="proxyInterfaces" >
    
< value > StaticAdvisorTest.Shopping </ value >
  
</ property >
  
< property  name ="interceptorNames" >
    
< list >
      
< value > shoppingPointCutAdvisor </ value >
    
</ list >
  
</ property >
  
< property  name ="target" >
    
< ref  bean ="shoppingImpl" />
  
</ property >
</ bean >

</ beans >

 <!-- 如果不使用通配符,则用以下表达 
  <property name="mappedNames">
    <list>
       <value>sellSomething</value>
       <value>sellAnything</value>
    </list>
  </property>
  -->

测试程序:

 

package  StaticAdvisorTest;

import  java.io.File;

import  org.springframework.beans.factory.BeanFactory;
import  org.springframework.beans.factory.xml.XmlBeanFactory;
import  org.springframework.core.io.FileSystemResource;



public   class  TestAdvisor  {

    
public static void main(String[] args) {

        String filePath
=System.getProperty("user.dir")+File.separator+"StaticAdvisorTest"+File.separator+"hello.xml";
        
        BeanFactory factory
=new XmlBeanFactory(new FileSystemResource(filePath));
        
        Shopping shopping
=null;

        shopping
=(Shopping)factory.getBean("StaticAdvisorTest");
        shopping.buySomething(
"something");
        shopping.buyAnything(
"anything");
        shopping.sellAnything(
"anything");
        shopping.sellSomething(
"something");
        
    

    }

}

 

运行结果:

gaoxiang bye something success
gaoxiang bye anything success
Hello welcome to buy anything
gaoxiang sell anything success
Hello welcome to buy something
gaoxiang sell something success

可以看到,为所有的sell开头的方法都进行了切面处理,而sell方法没有任何影响


转载地址:http://blog.csdn.net/daryl715/article/details/1618292

这篇关于浅谈Spring静态切入点使用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/544302

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关