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");
}
}



打印结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP 是一个用于实现面向切面编程的框架。它可以通过配置来实现横切关注点的模块化,并将其应用到程序的不同部分。Spring AOP 使用 AspectJ 切入点指示符来定义切入点表达式,用于匹配方法执行连接点。Spring AOP 支持以下 AspectJ 切入点指示符: 1. execution:用于匹配方法执行连接点。这是使用 Spring AOP 时要使用的主要切入点指示符。 2. within:限制匹配以连接某些类型中的点(使用 Spring AOP 时在匹配类型中声明的方法的执行)。 3. this:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中 Bean 引用(Spring AOP 代理)是给定类型的实例。 4. target:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中目标对象(正在代理的应用程序对象)是给定类型的实例。 5. args:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中参数是给定类型的实例。 6. @target:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中执行对象的类具有给定类型的注释。 7. @args:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中传递的实际参数的运行时类型具有给定类型的注释。 8. @within:限制匹配以连接具有给定注释的类型中的点(使用 Spring AOP 时在具有给定注释的类型中声明的方法的执行)。 9. @annotation:限制匹配到连接点的主题(在 Spring AOP 中运行的方法)具有给定注释的连接点。 在使用 Spring AOP 时,需要引入 Spring AOPSpring Context 相关的包,并在配置文件中进行相应的配置。可以通过 Maven 或其他构建工具来引入相关依赖。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值