spring aop横切对象、切面参数传递

2023-10-20 04:10

本文主要是介绍spring aop横切对象、切面参数传递,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.

spring.xml:

<?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"  
      xsi:schemaLocation="http://www.springframework.org/schema/beans   
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      http://www.springframework.org/schema/aop   
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
      
      
      
      <bean id="testaspect" class="com.spring.TestAspect"></bean>
      <bean id="testbiz" class="com.spring.TestBiz"></bean>
      
      <aop:config>
      <aop:pointcut expression="execution(* com.spring.TestBiz.biz(String,String)) and args(name,password)" id="testpoint"/>
      <aop:aspect ref="testaspect">
      <aop:before method="save" pointcut-ref="testpoint" arg-names="name,password"/>
      <!--
      <aop:after method="del" pointcut-ref="testpoint"/>
      <aop:around method="total" pointcut-ref="testpoint"/>
      -->
      </aop:aspect>
      </aop:config>
      
      </beans>



2.业务类(横切对象)

package com.spring;


public class TestBiz {
private String name;
private String password;

public void biz(String name,String password){
this.name=name;
this.password=password;
}

public String aa(){
//System.out.println(this.name);
return this.name;
}
}


3.切面

package com.spring;


import org.aspectj.lang.ProceedingJoinPoint;


public class TestAspect {
private String name;
private String password;

public void save(String name,String password){
System.out.println("guo="+name);
System.out.println("password="+password);
this.name=name;
this.password=password;
System.out.println(this.name);

}

public String aa(){
// System.out.println(this.name);
return this.name;
}



public String del(){
String name2="name2";
System.out.println("aspect del....");
return name2;
}

//环绕通知需要携带ProceedingJoinPoint类型的参数
public void total(ProceedingJoinPoint p){
String className=p.getTarget().getClass().getName();
String method=p.getSignature().getName();
try {
long start=System.currentTimeMillis();
System.out.println("start="+start);
p.proceed();
long end=System.currentTimeMillis();
System.out.println("end="+end);
System.out.println(className+"类的"+method+"方法"+"执行了"+(end-start)+"长时间");
} catch (Throwable e) {
e.printStackTrace();
}
}



}



4.测试类

package com.spring;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Test {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
TestBiz biz=(TestBiz)context.getBean("testbiz");
biz.biz("guooo","123456");
}
}



打印结果:


这篇关于spring aop横切对象、切面参数传递的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Java中的工具类命名方法

《Java中的工具类命名方法》:本文主要介绍Java中的工具类究竟如何命名,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java中的工具类究竟如何命名?先来几个例子几种命名方式的比较到底如何命名 ?总结Java中的工具类究竟如何命名?先来几个例子JD

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

SpringBoot整合OpenFeign的完整指南

《SpringBoot整合OpenFeign的完整指南》OpenFeign是由Netflix开发的一个声明式Web服务客户端,它使得编写HTTP客户端变得更加简单,本文为大家介绍了SpringBoot... 目录什么是OpenFeign环境准备创建 Spring Boot 项目添加依赖启用 OpenFeig

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖