---
title: "基于注解方式声明切面"
author: "yant"
date: "2009-12-13T16:44:15Z"
tags: []
categories: ["企业架构"]
slug: "untitled"
draft: false
lastmod: "2009-12-13T16:48:22Z"
id: 547494
url: "https://yant.iteye.com/blog/547494"
---

@Aspect //注明此类事切面类 
public class PrintLog(){ 
	@Pointcut("execution(* com.yant.service..*.*(..))")//第一个..表示service下的子包；第二个..表示任意参数（可以没有 
	private void anyMoth(){}//声明一个切点 

	@Before("anyMethed() && args(userNmae)")//定义前置通知,其中：args(userName)表示：此前置通知只拦截* com.yant.service..*.*(..)),并把参数赋给下面的方法参数， 
	public void doAccessCheck(String userNme){ 
	} 

	@AfterReturning(pointcut="anyMethod()",return="result")//定义后置通知，其中return表示此后置通知只拦截返回值为String的方法，并把此返回结果赋给下面方法的参数result，两者参数名必须相同 
	public void doAfterReturning(String result){ 
	} 

	@After("anyMethod()")//定义最终通知 
	public void doAfter(){ 
	} 

	@AfterThrowing(pointcut="anyMethod()",throwing="e")//定义异常通知（当所拦截的方法出现异常时） 
	public void doAfterThrowing(Exception e){ 
	} 

	@Around("anyMethod()")//定义环绕通知 
	public Object doBasicProfiing(ProceddingJoinPoint pjp) throws Throwable{ 
	//if(){//判断用户是否有权限 
		System.out.print("进入方法")； 
		Object result = pjp.proceed(); //此方法是必须
		System.out.println("退出方法"); 
	//} 
	return result； 
	} 
}