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 @ Aspect weaving does not work and does not perform pre-enhanced weaving @ Before in SpringAop

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

Share

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

This article is about how to achieve @ Aspect weaving that does not take effect and does not perform pre-enhanced weaving @ Before in SpringAop. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

SpringAop @ Aspect weaving does not work, do not perform pre-enhanced weaving @ Before want to write an AOP, there are two main purposes

The first intention is to do back-end token validation to prevent form repeated submissions.

The second intention is to unify the verification results of the background JSR303 Validator, and do not want to distribute the processing of the verification results in each controller method.

@ ResponseBody @ RequestMapping (value = "add", method = RequestMethod.POST) public ResponseModel add (@ Valid User user, BindingResult br, HttpServletResponse response) {if (br.hasErrors ()) {return ResponseModel.validFail (getErrorsSplitNewLine (br));} accountService.addUser (user) Return ResponseModel.success ("Save user success");}

As in the above method, br.hasErrors () exists in each form submission method, and you want to extract it separately and use AOP unified processing.

So write an AOP, as follows:

@ Aspect@Componentpublic class ParamValidAspect {@ Before ("@ annotation (com.hebao.tech.adm.framework.annotation.ParamValid)") public void paramValid (JoinPoint point) {System.out.println ("Parameter checking cut-in method was called."); / / omitted}}

Since the main purpose of this article is to record the reasons why AOP does not take effect, I will not write about the specific implementation here.

The above defines an Aop weave on the annotated Controller method with the annotation @ ParamValid.

Import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target; @ Target (ElementType.METHOD) @ Retention (RetentionPolicy.RUNTIME) public @ interface ParamValid {}

The content of this ParamValid is just an iconic comment, declared as a method-level comment, and a run-time comment.

Finally, add the AOP dynamic proxy setting to application.xml.

If the spring configuration file has not introduced the configuration of aop, you also need to add the xml declaration

Finished, tested, found a little tragic, the basic weaving does not take effect, does not report an error, Leng is not to execute the relevant weaving code.

Finally, I searched the Internet and found that Spring and SpringMVC are two different parent-child containers. @ Aspect is loaded by the spring container, while the instantiation and injection of these classes annotated by @ Controller is done by SpringMVC. If @ Aspect is loaded by the spring container, the Spring MVC container may not have been initialized and the Controller class has not been initialized, so it cannot be woven properly.

So the adjustment is as follows:

@ Aspectpublic class ParamValidAspect {@ Before ("@ annotation (com.hebao.tech.adm.framework.annotation.ParamValid)") public void paramValid (JoinPoint point) {System.out.println ("Parameter checking cut-in method was called."); / / omitted}}

Remove the @ Component annotation, then move aop:aspectj-autoproxy to the springmvc configuration file, and define bean, as follows:

In this way, we are done.

Using @ Aspect,@Before is not called @ Aspect@Componentpublic class LogAspect {@ Before ("pointcut ()") public void before () {System.out.println ("before");} @ Pointcut ("@ annotation (com.demo.annotation.Log)") public void pointcut () {} @ Around ("pointcut ()") public void around () {System.out.println ("arount") @ After ("pointcut ()") public void after () {System.out.println ("after");}}

Call the method to return the result:

Arount

After

@ Aspect@Componentpublic class LogAspect {@ Before ("pointcut ()") public void before () {System.out.println ("before");} @ Pointcut ("@ annotation (com.mxy.annotation.Log)") public void pointcut () {} @ Around ("pointcut ()") public void around (ProceedingJoinPoint point) {System.out.println ("arount before") Try {point.proceed ();} catch (Throwable throwable) {throwable.printStackTrace ();} System.out.println ("arount after");} @ After ("pointcut ()") public void after () {System.out.println ("after");}}

The call returns the result:

Arount before

Before

Arount after

After

Thank you for reading! On "how to achieve @ Aspect weaving does not take effect in SpringAop, do not implement pre-enhanced weaving @ Before" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it for more people to see it!

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