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

What is the annotation mode in Java SpringAOP technology?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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!

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