xml方式实现:
<!--切点-->
<bean id="target" class="com.it.aop.Target"/>
<!--切面对象-->
<bean id="myAspect" class="com.it.aop.MyAspect"/>
<!--配置织入:哪些方法需要哪些增强-->
<aop:config>
<!--声明切面-->
<aop:aspect ref="myAspect">
<aop:pointcut id="myPointcut" expression="execution(* com.it.aop.*.*(..))"/>
<!--切面:切点+通知-->
<aop:before method="before" pointcut-ref="myPointcut"/>
<aop:around method="around" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
注解实现:
切面:
@Component("myAspect")
@Aspect
public class MyAspect {
@Pointcut("execution(* com.it.aop.*.*(..))") //*为通配符 (..)表示任意参数
public void pointcut(){};
@Before("pointcut()")
public void before(){
System.out.println("before strength");
}
@Around("MyAspect.pointcut()")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕前");
proceedingJoinPoint.proceed();
System.out.println("环绕后");
}
}
需要在xml中配置注解驱动
<aop:aspectj-autoproxy/>