Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the aspect-oriented programming of Java AOP

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to realize Java aspect-oriented programming AOP". In daily operation, I believe that many people have doubts about how to realize Java aspect-oriented programming AOP. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize Java aspect-oriented programming AOP". Next, please follow the editor to study!

One: background

Spring's AOP exists for decoupling. AOP allows a set of classes to share the same behavior. In OOP, the coupling of code can only be enhanced by inheriting classes and implementing interfaces, and class inheritance can only be single inheritance, which prevents more behaviors from being added to a group of classes. AOP makes up for the shortcomings of OPP.

2: an overview of Spring's annotations that support AspectJ, aspect programming

1. Declare an aspect using @ Aspect.

two。 Using @ After,@Before,@Around to define advice (advice), you can directly take the interception rule (pointcut) as a parameter.

3. The interception rule of @ After,@Before,@Around parameter is PointCut. In order to reuse tangent points, you can use @ PointCut to define the interception rule, and then call it in the parameter @ After,@Before,@Around.

4. Each intercepted place that meets the condition is the JoinPoint.

Three: code examples

1.pom.xml

Click (here) to collapse or open

Org.springframework

Spring-core

Org.springframework

Spring-beans

Org.springframework

Spring-context

Org.springframework

Spring-aop

Org.aspectj

Aspectjrt

Org.aspectj

Aspectjweaver

two。 Notes on interception rules

Click (here) to collapse or open

@ Target (ElementType.METHOD)

@ Retention (RetentionPolicy.RUNTIME)

@ Documented

Public @ interface Action {

String name ()

}

two。 Annotated intercepted class

Click (here) to collapse or open

@ Service

Public class DemoAnnotationService {

@ Action (name = "annotated intercepted add operation")

Public void add () {

System.out.println ("= DemoAnnotationService method add () =")

}

}

3. Method rule intercepted class

Click (here) to collapse or open

@ Service

Public class DemoMethodService {

Public String add () throws Exception {

System.out.println ("= DemoMethodService method add () =")

Int iTunes 100 Universe 0

Return "SUCCESS"

}

}

4. Write a section

Click (here) to collapse or open

@ Aspect

@ Component

Public class LogAspect {

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

Public void annotationPointCut () {

}

@ After ("annotationPointCut ()")

Public void after (JoinPoint joinPoint) {

MethodSignature signature = (MethodSignature) joinPoint.getSignature ()

Method method = signature.getMethod ()

Action action = method.getAnnotation (Action.class)

System.out.println ("annotated intercept" + action.name ())

}

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

Public void methodBefore (JoinPoint joinPoint) {

MethodSignature signature = (MethodSignature) joinPoint.getSignature ()

Method method = signature.getMethod ()

System.out.println ("regular interception of before method" + method.getName ())

}

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

Public void methodAfter (JoinPoint joinPoint) {

MethodSignature signature = (MethodSignature) joinPoint.getSignature ()

Method method = signature.getMethod ()

System.out.println ("regular interception of after method" + method.getName ())

}

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

Public void methodAfterResult (JoinPoint joinPoint, Object result) {

MethodSignature signature = (MethodSignature) joinPoint.getSignature ()

Method method = signature.getMethod ()

System.out.println ("after result method regular interception" + method.getName () + "result=" + result)

}

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

Public void methodAfterException (JoinPoint joinPoint, Exception e) {

MethodSignature signature = (MethodSignature) joinPoint.getSignature ()

Method method = signature.getMethod ()

System.out.println ("after exception method regular interception" + method.getName () + "e =" + e.getMessage ())

}

}

5. Configuration class

Click (here) to collapse or open

@ Configuration

@ ComponentScan ("com.gemdale")

@ EnableAspectJAutoProxy

Public class AppliactionConfig {

}

6. Executive class

Click (here) to collapse or open

Public class Start {

Public static void main (String [] args) throws Exception {

AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext (

AppliactionConfig.class)

/ / UseFunctionService

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

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

DemoAnnotationService demoAnnotationService = configApplicationContext.getBean (DemoAnnotationService.class)

DemoMethodService demoMethodService = configApplicationContext.getBean (DemoMethodService.class)

DemoAnnotationService.add ()

DemoMethodService.add ()

ConfigApplicationContext.close ()

}

}

At this point, the study of "how to implement Java aspect-oriented programming AOP" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report