Spring面向切面(AOP)声明方式的整理
时间:2021-12-14 作者:匿名
使用aop
首先需要 配置 aop头部
<beans xmlns="http://域名/schema/beans" xmlns:xsi="http://域名/2001/XMLSchema-instance" xmlns:aop="http://域名/schema/aop" ---- xsi:schemaLocation="http://域名/schema/beans http://域名/schema/beans/spring-beans-域名 http://域名/schema/aop http://域名/schema/aop/spring-aop-域名"> ---- </beans>
Spring提供了两种切面声明方式。
1.基于XML配置方式声明切面。
<bean id="orderservice" class="域名域名rServiceBean"/> <bean id="log" class="域名域名rint"/>
2.基于注解方式声明切面。
@Aspect public class LogPrint { @Pointcut("execution(* 域名ice..*.*(..))") private void anyMethod() {}//声明一个切入点 @Before("anyMethod() && args(userName)")//定义前置通知 public void doAccessCheck(String userName) { } @AfterReturning(pointcut="anyMethod()",returning="revalue")//定义后置通知 public void doReturnCheck(String revalue) { } @AfterThrowing(pointcut="anyMethod()", throwing="ex")//定义例外通知 public void doExceptionAction(Exception ex) { } @After("anyMethod()")//定义最终通知 public void doReleaseAction() { } @Around("anyMethod()")//环绕通知 public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { return 域名eed(); } }