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

1.

spring.xml:

 
      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">
      
      
      
     
     
      
     
     
     
     
     
     

     

      
     



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横切对象、切面参数传递_第1张图片

你可能感兴趣的:(spring aop横切对象、切面参数传递)