0
点赞
收藏
分享

微信扫一扫

AspectJ 基于注解的实现各种通知

接口

package com.hk.spring.annotation;

public interface ISomeService {
public void doFirst();
public void doSecond();
public String doThird();

}

实现接口

package com.hk.spring.annotation;

public class ISomeServiceImpl implements ISomeService {

@Override
public void doFirst() {
System.out.println("主业务逻辑doFirst执行");
}

@Override
public void doSecond() {
System.out.println("主业务逻辑doSecond执行");

}

@Override
public String doThird() {
System.out.println("主业务逻辑doThird执行");
return "ABC";

}

}

切面类

package com.hk.spring.annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
* 定义一个简单的POJO类作为切面类
* @author 浪丶荡
* @param <throwing>
*
*/
@Aspect //表示当前类为切面类
public class MyAspect<throwing> {

/**
* 前置通知
*/
@Before("execution(* *..ISomeService.doFirst(..))") //表示前置通知的切入点表达式
public void before(){
System.out.println("前置方法");
}
/**
* 前置通知2
*/
@Before("execution(* *..ISomeService.doThird(..))")
public void before(JoinPoint jp){//jp为切入点表达式值
System.out.println("前置方法执行 "+jp);
}
/**
* 后置通知
*/
@AfterReturning("execution(* *..ISomeService.doSecond(..))")
public void afterReturing(JoinPoint jp){//jp为切入点表达式值
System.out.println("后置方法执行 "+jp);
}
/**
* 后置通知2
*/
@AfterReturning(value="execution(* *..ISomeService.doThird(..))",returning="result")
public void afterReturing(String result){
System.out.println("后置方法执行 result="+result);
}
/**
* 环绕通知
*/
@Around(value="execution(* *..ISomeService.doThird(..))")
public String around(ProceedingJoinPoint p){
Object result = null;
System.out.println("目标方法执行前");
try {
//执行目标方法
result = p.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("目标方法执行后");

return ((String) result).toLowerCase();

}
/**
* 异常通知
*/
@AfterThrowing(value="execution(* *..ISomeService.doSecond(..))",throwing="ex")
public void afterThrowing(Exception ex){
System.out.println("afterThrowing执行,异常是:"+ex.getMessage());
}
/**
* 最终通知
*/
@After("execution(* *..ISomeService.doSecond(..))")
public void after(){
System.out.println("最终通知方法");
}
}

配置文件

<?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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册目标对象 -->
<bean id="someService" class="com.hk.spring.annotation.ISomeServiceImpl"></bean>
<!-- 注册切面 -->
<bean id="myAspect" class="com.hk.spring.annotation.MyAspect"></bean>
<!-- 注册Aspect的自动代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

测试

package com.hk.spring.annotation;


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

public class Mytest {

@Test
public void test1(){
String resoure = "com/hk/spring/annotation/applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(resoure);
ISomeService service = (ISomeService) ac.getBean("someService");
service.doFirst();
service.doSecond();
String result = service.doThird();
System.out.println(result);

}
}


举报

相关推荐

0 条评论