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

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3