现在的位置: 首页 > 综合 > 正文

AspectJ实例

2017年12月26日 ⁄ 综合 ⁄ 共 1031字 ⁄ 字号 评论关闭

AspectJ是java Aop编译时增强的代表。

几个专业名词:

aspect 切面  解释:service包下面的所有类的所有get**()方法,算是一个切面

joinpoint 连接点 解释:在方法的开始,或方法的结束,或方法的开始和结束可以插入增强处理(只是说可以插入);

advice 增强处理 解释:在方法的开始,或方法的结束, 或方法的开始和结束插入的代码叫做增强处理;

pointcut 切入点 解释:在方法的开始,或方法的结束,或方法的开始和结束插入了增强处理的连接点被称为切入点,就是连接点插入了增强处理就变成了切入点;

aop代理方法 包括 目标方法和增强处理

增强处理类

public aspect TxAdvice  
{
	before():execution(* com.sharp.service.*.*(..))//切入点
	{
		System.out.println("author check");//增强处理
	}
	Object around():execution(* com.sharp.service.*.*(..))//切入点
	{
		System.out.println("tx is open ");//增强处理
		Object obj = proceed();//回调目标方法
		System.out.println("tx is close ");//增强处理
		return obj;//返回目标方法的返回值
	}
	after():execution(* com.sharp.service.*.*(..))//切入点
	{
		System.out.println(" tx advice log is saving.1..");//增强处理
	}
	after():execution(* com.sharp.service.*.*(..))//切入点
	{
		System.out.println(" tx advice log is saving.2..");//增强处理
	}
}

目标类:

package com.sharp.service;

public class Hello
{
	public void say(){
		//连接点(这里可以插入增强处理)
		System.out.println("hello is saying~~~~~~");//目标方法
		//连接点(这里可以插入增强处理)
	}
	public void cry(){
		//连接点(这里可以插入增强处理)
		System.out.println("hello is crying~~~~~~");//目标方法
		//连接点(这里可以插入增强处理)
	}
}

抱歉!评论已关闭.