千家信息网

Java面向切面编程AOP怎么实现

发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,这篇文章主要介绍"Java面向切面编程AOP怎么实现",在日常操作中,相信很多人在Java面向切面编程AOP怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Jav
千家信息网最后更新 2025年02月05日Java面向切面编程AOP怎么实现

这篇文章主要介绍"Java面向切面编程AOP怎么实现",在日常操作中,相信很多人在Java面向切面编程AOP怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Java面向切面编程AOP怎么实现"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一:背景
Spring的AOP的存在目的是为了解耦。AOP可以让一组类共享相同的行为。在OOP中只能通过继承类和实现接口,来是代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类上,AOP弥补了OPP的不足。

二:概述 Spring支持AspectJ的注解方式切面编程

1.使用@Aspect 声明一个切面。

2.使用@After,@Before,@Around 定义建言(advice),可直接将拦截规则(切点)作为参数。

3.其中@After,@Before,@Around参数的拦截规则为切点(PointCut) ,为了使切点复用,可使用@PointCut 专门定义拦截规则,然后在@After,@Before,@Around的参数中调用。

4.其中符合条件的每一个被拦截处为连接点(JoinPoint)

三:代码实例

1.pom.xml


点击(此处)折叠或打开


  1. org.springframework

  2. spring-core



  3. org.springframework

  4. spring-beans




  5. org.springframework

  6. spring-context




  7. org.springframework

  8. spring-aop




  9. org.aspectj

  10. aspectjrt




  11. org.aspectj

  12. aspectjweaver

2.拦截规则的注解


点击(此处)折叠或打开

  1. @Target(ElementType.METHOD)

  2. @Retention(RetentionPolicy.RUNTIME)

  3. @Documented

  4. public @interface Action {

  5. String name();

  6. }

2.注解的被拦截类


点击(此处)折叠或打开

  1. @Service

  2. public class DemoAnnotationService {


  3. @Action(name = "注解式拦截的add操作")

  4. public void add() {

  5. System.out.println("======DemoAnnotationService方法add()=========");

  6. }

  7. }

3.方法规则被拦截类


点击(此处)折叠或打开

  1. @Service

  2. public class DemoMethodService {

  3. public String add() throws Exception{

  4. System.out.println("======DemoMethodService方法add()=========");

  5. int i=100/0;

  6. return "SUCCESS";

  7. }

  8. }


4.编写切面


点击(此处)折叠或打开

  1. @Aspect

  2. @Component

  3. public class LogAspect {


  4. @Pointcut("@annotation(com.gemdale.gmap.spring.boot.demo.Action)")

  5. public void annotationPointCut() {

  6. }


  7. @After("annotationPointCut()")

  8. public void after(JoinPoint joinPoint) {

  9. MethodSignature signature = (MethodSignature) joinPoint.getSignature();

  10. Method method = signature.getMethod();

  11. Action action = method.getAnnotation(Action.class);


  12. System.out.println("注解式拦截 " + action.name());

  13. }


  14. @Before("execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))")

  15. public void methodBefore(JoinPoint joinPoint) {

  16. MethodSignature signature = (MethodSignature) joinPoint.getSignature();

  17. Method method = signature.getMethod();

  18. System.out.println("before方法规则式拦截 " + method.getName());

  19. }


  20. @After("execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))")

  21. public void methodAfter(JoinPoint joinPoint) {

  22. MethodSignature signature = (MethodSignature) joinPoint.getSignature();

  23. Method method = signature.getMethod();

  24. System.out.println("after方法规则式拦截 " + method.getName());

  25. }


  26. @AfterReturning(pointcut = "execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))", returning = "result")

  27. public void methodAfterResult(JoinPoint joinPoint, Object result) {

  28. MethodSignature signature = (MethodSignature) joinPoint.getSignature();

  29. Method method = signature.getMethod();

  30. System.out.println("after result方法规则式拦截 " + method.getName() + "result=" + result);

  31. }


  32. @AfterThrowing(pointcut = "execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))", throwing = "e")

  33. public void methodAfterException(JoinPoint joinPoint, Exception e) {

  34. MethodSignature signature = (MethodSignature) joinPoint.getSignature();

  35. Method method = signature.getMethod();

  36. System.out.println("after exception方法规则式拦截 " + method.getName() + " e=" + e.getMessage());

  37. }


  38. }

5.配置类


点击(此处)折叠或打开

  1. @Configuration

  2. @ComponentScan("com.gemdale")

  3. @EnableAspectJAutoProxy

  4. public class AppliactionConfig {


  5. }


6.执行类


点击(此处)折叠或打开

  1. public class Start {

  2. public static void main(String[] args) throws Exception{


  3. AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(

  4. AppliactionConfig.class);

  5. // UseFunctionService

  6. // useFunctionService=configApplicationContext.getBean(UseFunctionService.class);

  7. // System.out.println(useFunctionService.sayHello("Gengchong"));

  8. DemoAnnotationService demoAnnotationService = configApplicationContext.getBean(DemoAnnotationService.class);

  9. DemoMethodService demoMethodService = configApplicationContext.getBean(DemoMethodService.class);


  10. demoAnnotationService.add();


  11. demoMethodService.add();


  12. configApplicationContext.close();

  13. }

  14. }

到此,关于"Java面向切面编程AOP怎么实现"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0