spring 多个切面
Posted ooo0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring 多个切面相关的知识,希望对你有一定的参考价值。
Spring也能支持多个切面。当有多个切面时,它不会存在任何顺序,这些顺序代码会随机生成,但是有时候我们希望它按照指定的顺序运行。
在此之前要先定义一个切点方法,为此新建一个接口——MultiBean
代码清单:定义多个切面的切点方法
package com.ssm.chapter11.multi.bean; public interface MultiBean public void testMulti();
代码清单:实现MultiBean接口
package com.ssm.chapter11.multi.bean.impl; import com.ssm.chapter11.multi.bean.MultiBean; import org.springframework.stereotype.Component; /****************imports****************/ @Component public class MultiBeanImpl implements MultiBean // @Override public void testMulti() System.out.println("test multi aspects!!");
代码清单:3个切面
package com.ssm.chapter11.multi.aspect; import org.aspectj.lang.annotation.*; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; /**************** imports ****************/ @Aspect // @Order(1) public class Aspect1 implements Ordered // @Override public int getOrder() return 1; @Pointcut("execution(* com.ssm.chapter11.multi.bean.impl.MultiBeanImpl.testMulti(..))") public void print() @Before("print()") public void before() System.out.println("before 1 ......"); @After("print()") public void after() System.out.println("after 1 ......"); @AfterThrowing("print()") public void afterThrowing() System.out.println("afterThrowing 1 ......"); @AfterReturning("print()") public void afterReturning() System.out.println("afterReturning 1 ......");
package com.ssm.chapter11.multi.aspect; import org.aspectj.lang.annotation.*; import org.springframework.core.annotation.Order; /**************** imports ****************/ @Aspect @Order(2) public class Aspect2 @Pointcut("execution(* com.ssm.chapter11.multi.bean.impl.MultiBeanImpl.testMulti(..))") public void print() @Before("print()") public void before() System.out.println("before 2 ......"); @After("print()") public void after() System.out.println("after 2 ......"); @AfterThrowing("print()") public void afterThrowing() System.out.println("afterThrowing 2 ......"); @AfterReturning("print()") public void afterReturning() System.out.println("afterReturning 2 ......");
package com.ssm.chapter11.multi.aspect; import org.aspectj.lang.annotation.*; import org.springframework.core.annotation.Order; /**************** imports ****************/ @Aspect @Order(3) public class Aspect3 @Pointcut("execution(* com.ssm.chapter11.multi.bean.impl.MultiBeanImpl.testMulti(..))") public void print() @Before("print()") public void before() System.out.println("before 3 ......"); @After("print()") public void after() System.out.println("after 3 ......"); @AfterThrowing("print()") public void afterThrowing() System.out.println("afterThrowing 3 ......"); @AfterReturning("print()") public void afterReturning() System.out.println("afterReturning 3 ......");
代码清单:多切面测试Java配置
package com.ssm.chapter11.multi.config; import com.ssm.chapter11.multi.aspect.Aspect1; import com.ssm.chapter11.multi.aspect.Aspect2; import com.ssm.chapter11.multi.aspect.Aspect3; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; /****************imports****************/ @Configuration @EnableAspectJAutoProxy @ComponentScan("com.ssm.chapter11.multi") public class MultiConfig @Bean public Aspect1 getAspect1() return new Aspect1(); @Bean public Aspect2 getAspect2() return new Aspect2(); @Bean public Aspect3 getAspect3() return new Aspect3();
代码清单:测试
public static void main(String[] args) ApplicationContext ctx = new AnnotationConfigApplicationContext(MultiConfig.class); MultiBean multiBeanService = ctx.getBean(MultiBean.class); multiBeanService.testMulti();
如何让它有序执行:
[email protected](1),@Order(2),@Order(3) 注解
2.实现 implements Ordered
3.<aop:aspect ref="aspect1" order="1">
以上是关于spring 多个切面的主要内容,如果未能解决你的问题,请参考以下文章