Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the annotation mode in Java SpringAOP technology?

Shulou Source: shulou.com Published: 2022-06-01 09:53:11 10月03日 Update

This article mainly shows you "what is the annotation method in Java SpringAOP technology", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is the annotation method in Java SpringAOP technology" this article.

1. Configure xml scan Note 2. Configuration Note package com.qcby;import org.springframework.stereotype.Component;@Component (value = "user") public class User {/ / join Point / pointcut public void add () {System.out.println ("add.");}}

Add @ Aspect to the facet class, write enhanced methods, and declare using notification type annotations

@ Component@Aspect / / generate the proxy object public class UserProxy {} 3. Automatic proxy 4 is enabled in configuration file. Notification type comment

@ Before-- advance notification

@ AfterReturing-post notification

@ Around-- surround notification (the target object method is not executed by default and needs to be executed manually)

@ After-final notice

@ AfterThrowing-- exception throw notification

Package com.qcby;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.stereotype.Component;@Component@Aspect / / generate proxy object public class UserProxy {/ / Enhancement / Notification-"pre-notification @ Before (value =" execution (public void com.qcby.User.add ()) ") public void before () {System.out.println (" pre-notification. ") } / / surround notification @ Around (value = "execution (public void com.qcby.User.add ())") public void around (ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println ("surround front."); / / execute the enhanced method proceedingJoinPoint.proceed () System.out.println ("surround rear.");} / / final notification @ After (value = "execution (public void com.qcby.User.add ())") public void after () {System.out.println ("final notification.") } / / Post notice @ AfterReturning (value = "execution (public void com.qcby.User.add ())") public void afterReturning () {System.out.println ("Post notice.") } / / exception notification @ AfterThrowing (value = "execution (public void com.qcby.User.add ())") public void afterThrowing () {System.out.println ("error.");}} 5. Test class package com.qcby.test;import com.qcby.User;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class UserTest {@ Test public void aopTest1 () {ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("SpringConfig.xml"); User user = (User) applicationContext.getBean ("user"); user.add ();}} 6. Result

The above is all the content of this article "what is the Annotation method in Java SpringAOP Technology?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Annotations techniques methods content objects methods articles agents configurations types learning help generation pointcuts aspects manual files easy to understand more organized Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB vpn Docker Apple