浅谈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

相关文章

MySQL查看表的历史SQL的几种实现方法

《MySQL查看表的历史SQL的几种实现方法》:本文主要介绍多种查看MySQL表历史SQL的方法,包括通用查询日志、慢查询日志、performance_schema、binlog、第三方工具等,并... 目录mysql 查看某张表的历史SQL1.查看MySQL通用查询日志(需提前开启)2.查看慢查询日志3.

MySQL底层文件的查看和修改方法

《MySQL底层文件的查看和修改方法》MySQL底层文件分为文本类(可安全查看/修改)和二进制类(禁止手动操作),以下按「查看方法、修改方法、风险管控三部分详细说明,所有操作均以Linux环境为例,需... 目录引言一、mysql 底层文件的查看方法1. 先定位核心文件路径(基础前提)2. 文本类文件(可直

Java实现字符串大小写转换的常用方法

《Java实现字符串大小写转换的常用方法》在Java中,字符串大小写转换是文本处理的核心操作之一,Java提供了多种灵活的方式来实现大小写转换,适用于不同场景和需求,本文将全面解析大小写转换的各种方法... 目录前言核心转换方法1.String类的基础方法2. 考虑区域设置的转换3. 字符级别的转换高级转换

使用Python将PDF表格自动提取并写入Word文档表格

《使用Python将PDF表格自动提取并写入Word文档表格》在实际办公与数据处理场景中,PDF文件里的表格往往无法直接复制到Word中,本文将介绍如何使用Python从PDF文件中提取表格数据,并将... 目录引言1. 加载 PDF 文件并准备 Word 文档2. 提取 PDF 表格并创建 Word 表格

使用Python实现局域网远程监控电脑屏幕的方法

《使用Python实现局域网远程监控电脑屏幕的方法》文章介绍了两种使用Python在局域网内实现远程监控电脑屏幕的方法,方法一使用mss和socket,方法二使用PyAutoGUI和Flask,每种方... 目录方法一:使用mss和socket实现屏幕共享服务端(被监控端)客户端(监控端)方法二:使用PyA

Python使用Matplotlib和Seaborn绘制常用图表的技巧

《Python使用Matplotlib和Seaborn绘制常用图表的技巧》Python作为数据科学领域的明星语言,拥有强大且丰富的可视化库,其中最著名的莫过于Matplotlib和Seaborn,本篇... 目录1. 引言:数据可视化的力量2. 前置知识与环境准备2.1. 必备知识2.2. 安装所需库2.3

SpringBoot简单整合ElasticSearch实践

《SpringBoot简单整合ElasticSearch实践》Elasticsearch支持结构化和非结构化数据检索,通过索引创建和倒排索引文档,提高搜索效率,它基于Lucene封装,分为索引库、类型... 目录一:ElasticSearch支持对结构化和非结构化的数据进行检索二:ES的核心概念Index:

Python数据验证神器Pydantic库的使用和实践中的避坑指南

《Python数据验证神器Pydantic库的使用和实践中的避坑指南》Pydantic是一个用于数据验证和设置的库,可以显著简化API接口开发,文章通过一个实际案例,展示了Pydantic如何在生产环... 目录1️⃣ 崩溃时刻:当你的API接口又双叒崩了!2️⃣ 神兵天降:3行代码解决验证难题3️⃣ 深度

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时

检查 Nginx 是否启动的几种方法

《检查Nginx是否启动的几种方法》本文主要介绍了检查Nginx是否启动的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1. 使用 systemctl 命令(推荐)2. 使用 service 命令3. 检查进程是否存在4