spring3: 切面及通知实例 Aspectj的aop

时间:2022-08-30 16:55:03

1.前置通知

接口:

package chapter1.server;

public interface IHelloService {

public void sayAdvisorBefore(String param) ;
}

  实现

package chapter1.service.impl;

import chapter1.server.IHelloService;

public class HelloService implements IHelloService {

public void sayAdvisorBefore(String param) {
// TODO Auto-generated method stub
System.out.println("============say " + param);
}

}

  配置:

<!-- 启动对Aspectj的支持 -->					
<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService" />
<bean id="aspect" class="chapter1.aop.HelloAspect"/>

  aop:

package chapter1.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class HelloAspect {

//定义切入点
@Pointcut(value="execution(* chapter1..*.sayAdvisorBefore(java.lang.String)) && args(param)", argNames = "param")
public void beforePointcut(String param) {}

//定义通知
@Before(value = "beforePointcut(param)", argNames = "param")
public void beforeAdvice(String param) {
System.out.println("===========before advice param:" + param);
}
}

  测试程序:

//前置通知
public void testAspectj()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj.xml");
IHelloService hello = context.getBean("helloService", IHelloService.class);
hello.sayAdvisorBefore("before");
}

  

 

2.后置返回通知

接口

package chapter1.server;

public interface IHelloService2 {
public int sayAfterReturning(String param);
}

  实现

package chapter1.service.impl;

import chapter1.server.IHelloService2;

public class HelloService2 implements IHelloService2 {

public int sayAfterReturning(String param) {
// TODO Auto-generated method stub
System.out.println("============ say after returning:" + param);
return 1;
}

}

  配置:

<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService2" />
<bean id="aspect" class="chapter1.aop.HelloAspect2"/>

  aop:

package chapter1.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.AfterReturning;

@Aspect
public class HelloAspect2 {



//方法一
//通知
@AfterReturning(
//value="execution(* chapter1..*.sayAdvisorBefore(java.lang.String)) and args(param)",
value="execution(* chapter1..*.sayAfterReturning(..))",
argNames="retVal",
returning="retVal")
public void afterReturningAdvice(Object retVal)
{
System.out.println("================= return after advice : " + retVal);
}


//方法二
//定义切入点
@Pointcut(value="execution(* chapter1..*.sayAfterReturning(java.lang.String) and args(param))", argNames="param")
public void returnPointcut(String param) {}

public void afterReturningAdvice2(Object retVal)
{

}

}

  测试程序:

//后置返回通知
public void testAspectAfterReturning()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj2.xml");
IHelloService2 hello = context.getBean("helloService", IHelloService2.class);
hello.sayAfterReturning("hahah");
}

  

 

 

3.后置错误通知

接口

package chapter1.server;

public interface IHelloService3 {
public boolean sayAfterThrow(String param);
}

  实现:

package chapter1.service.impl;

import chapter1.server.IHelloService3;

public class HelloService3 implements IHelloService3 {

public boolean sayAfterThrow(String param) {
// TODO Auto-generated method stub
System.out.println("=========say after throw:" + param);
throw new RuntimeException();
}

}

  配置:

<!-- 开启对Aspectj的支持 -->	
<aop:aspectj-autoproxy />
<bean id="helloService" class="chapter1.service.impl.HelloService3" />
<bean id="aspect" class="chapter1.aop.HelloAspect3" />

  aop:

package chapter1.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterThrowing;

@Aspect
public class HelloAspect3 {


@AfterThrowing(value="execution(* chapter1..*.sayAfterThrow(java.lang.String))", argNames="exception", throwing="exception")
public void afterThrowAdvice(Exception exception)
{
System.out.println("=========after throwing advice : " + exception);
}
}

  测试程序:

//后置错误通知	
public void testAfterThrow()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj3.xml");
IHelloService3 hello = context.getBean("helloService", IHelloService3.class);
hello.sayAfterThrow("error");
}

  

 

 

4.环绕通知

接口:

package chapter1.server;

public interface IHelloService4 {
public void sayAround(String param);
}

  实现:

package chapter1.service.impl;

import chapter1.server.IHelloService4;

public class HelloService4 implements IHelloService4 {

public void sayAround(String param) {
// TODO Auto-generated method stub
System.out.println("============= say around: " + param);
}

}

  配置:

<!-- 开启对Aspectj的支持 -->		
<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService4" />
<bean id="aspect" class="chapter1.aop.HelloAspect4"/>

  aop:

package chapter1.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;

@Aspect
public class HelloAspect4 {


@Around(value="execution(* chapter1..*.sayAround(java.lang.String))", argNames="param")
public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable
{
System.out.println("========= around before advice");
Object retVal = pjp.proceed(new Object[] {"我被替换了呀"});
System.out.println("========= around before advice");
return retVal;

}
}

  测试程序:

//环绕通知	
public void testAround()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj4.xml");
IHelloService4 hello = context.getBean("helloService", IHelloService4.class);
hello.sayAround("gaga ya xia ba");
}

  

 

 

5.引入(结合chatper1.service.IHelloService程序来试验)

接口:

package chapter1.server;

public interface IHelloService5 {
public void sayDeclare();
}

  实现:

package chapter1.service.impl;

import chapter1.server.IHelloService5;

public class HelloService5 implements IHelloService5 {

public void sayDeclare() {
// TODO Auto-generated method stub
System.out.println("=========== say declare " );
}

}

  配置:

<!-- 开启对Aspect的支持 -->
<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService"/>
<bean id="aspect" class="chapter1.aop.HelloAspect5"/>

  aop:

package chapter1.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;

import chapter1.server.IHelloService5;
import chapter1.service.impl.HelloService5;

@Aspect
public class HelloAspect5 {

@DeclareParents(
value="chapter1..*.HelloService+",
defaultImpl=HelloService5.class)
public IHelloService5 ihelloService5;

}

  测试程序:

//引入
@Test
public void testDeclare()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj5.xml");
IHelloService5 hello = context.getBean("helloService", IHelloService5.class);
hello.sayDeclare();
}