spring__AOP方式三:使用注解

xml 
<!--注册bean--> <bean id="userService" class="com.x.service.UserServiceImpl"/> <bean id="log" class="com.x.log.Log"/> <bean id="afterLog" class="com.x.log.AfterLog"/> <!-- 方式三:使用注解--> <bean id="annotationPointCut" class="com.x.diy.AnnotationPointCut"/> <!-- 开启注解支持--> <aop:aspectj-autoproxy/>
AnnotationPointCut.java


@Aspect //标注这个类是个切面 public class AnnotationPointCut { @Before("execution(* com.x.service.UserServiceImpl.*(..))") public void before(){ System.out.println("===方法执行前==="); } @After("execution(* com.x.service.UserServiceImpl.*(..))") public void after(){ System.out.println("===方法执行后==="); } // 在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点; @Around("execution(* com.x.service.UserServiceImpl.*(..))") public void around(ProceedingJoinPoint jp) throws Throwable { System.out.println("环绕前"); Object proceed = jp.proceed();//执行方法 System.out.println("环绕后"); } }
测试类
public
class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 动态代理所代理的是接口 UserService service = (UserService)context.getBean("userService"); service.add(); } }
输出结果

环绕前
===方法执行前=== 增加用户 ===方法执行后=== 环绕后

 

posted @ 2021-04-25 16:58  琴湖copy王  阅读(54)  评论(0)    收藏  举报