Spring AOP简单的标签配置和xml配置
标签:
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; @Aspect //what 切面类 @Component public class MyAspect { @Pointcut("execution(* add(..))") //where 在哪个地方增强 所有的add方法 private void pointcut() {} @Before("com.bw.springaop.MyAspect.pointcut()") private void before(JoinPoint joinPoint) { System.out.println(joinPoint.getTarget() + "----------------------Before---------------------"); } @Around("bean(userService)") // when 什么时候增强 private void after(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println(joinPoint.getTarget() + "----------------------环绕增强开始---------------------"); joinPoint.proceed(); System.out.println(joinPoint.getTarget() + "----------------------环绕增强结束---------------------"); } }
xml配置:
<bean class="com.bw.springaop.MyAspect" id="myAspect"/><aop:config> <!--what--> <aop:aspect ref="myAspect"> <aop:pointcut id="pointcut" expression="bean(userService)"></aop:pointcut><!--where--> <aop:around method="after" pointcut-ref="pointcut"></aop:around><!--when--> </aop:aspect> </aop:config>
事物的配置: