Spring
自定义切面采用两种方式:一是使用xlm配置,二是使用
注释。在3.0以后使用spring注释时一定要注意,配置方式已经改变。
一,spring3.0以前注释使用方式如下:
class="java" name="code">@Aspect
public class CheckUser {
@Pointcut("execution(* org.service.*.find*(..))")
public void checkUser() {
System.out
.println("**************The System is Searching Information For You****************");
}
@Pointcut("execution(* org.service.*.add*(..))")
public void checkAdd() {
System.out.println("**************<< Add User >> Checking.....***************");
}
@Before("checkUser()")
public void beforeCheck() {
System.out.println(">>>>>>>> 准备搜查用户..........");
}
@After("checkUser()")
public void afterCheck() {
System.out.println(">>>>>>>> 搜查用户完毕..........");
}
@Before("checkAdd()")
public void beforeAdd() {
System.out.println(">>>>>>>> 增加用户--检查ing..........");
}
@After("checkAdd()")
public void afterAdd() {
System.out.println(">>>>>>>> 增加用户--检查完毕!未发现异常!..........");
}
}
一,spring3.0以后注释使用方式如下:
@Aspect
public class CheckUser {
/*
* @Pointcut("execution(* com.jushi.aop.*.find*(..))") public void
* checkUser() { System.out .println(
* "**************The System is Searching Information For You****************"
* ); }
*
* @Pointcut("execution(* com.jushi.aop.*.add*(..))") public void checkAdd()
* { System.out
* .println("**************<< Add User >> Checking.....***************"); }
*/
@Before("execution(* com.jushi.aop.*.add*(..))")
public void beforeCheck(JoinPoint jp) {
System.out.println(">>>>>>>> 准备搜查用户..........");
}
@After("execution(* com.jushi.aop.*.add*(..))")
public void afterCheck(JoinPoint jp) {
System.out.println(">>>>>>>> 搜查用户完毕..........");
}
@Before("execution(* com.jushi.aop.*.add*(..))")
public void beforeAdd(JoinPoint jp) {
System.out.println(">>>>>>>> 增加用户--检查ing..........");
}
@After("execution(* com.jushi.aop.*.add*(..))")
public void afterAdd(JoinPoint jp) {
System.out.println(">>>>>>>> 增加用户--检查完毕!未发现异常!..........");
}
}
如果使用不当会出现
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut checkAdd
Caused by: java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut checkAdd