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

Problems encountered in using & lt;context:load-time-weaver/> to implement static Agent in spring

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article focuses on "the problems encountered in the implementation of static agents in spring". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the problems encountered in using spring to implement static agents".

Step one:

Create the class that you want to implement static, and the Advice enhanced class implementation, as follows:

Classes that require static proxies:

Public interface IITestBean {void test ();} public class TestBean implements IITestBean {@ Override public void test () {System.out.println ("test");}}

Advice Enhancement Class:

@ Aspectpublic class AspectTest {@ Pointcut ("execution (* * .test (..)") Public void test () {System.out.println ("I cut in");} @ Before ("test ()") public void beforeTest () {System.out.println ("beforeTest ()");} @ After ("test ()") public void afterTest () {System.out.println ("afterTest ()") } @ Around ("test ()") public Object aroundTest (ProceedingJoinPoint p) {System.out.println ("before1"); Object o = null; try {o = p.proceed ();} catch (Throwable throwable) {throwable.printStackTrace ();} System.out.println ("after1"); return o;}}

Step 2:

Create an aop.xml under the META-INF (create otherwise) folder under the class directory, as follows

Step 3:

Write the configuration spring-aspect.xml for spring, as follows:

Step 4:

Write a test class as follows:

Public class AspectTest {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("spring-aspect.xml"); TestBean bean = (TestBean) context.getBean ("test"); bean.test ();}}

Step 5:

When testing, you need to download and import the org.springframework.instrument.jar file, and configure it in idea as follows:

Step 6:

Problems encountered in operation

Problem 1: a java.lang.VerifyError: Expecting a stackmap frame at branch target 7 error occurred

Solution: VM option in idea, add-XX:-UseSplitVerifier

Problem 2:circular advice precedence error

Solution:

For the Advice enhancer AspectTest, you must write code according to @ Before- > @ Around- > @ After, and the above code can be adjusted smoothly. But in spring dynamic proxies do not have the wrong order and do not throw exceptions.

Step 7:

We can see that the normal static class proxy results are as follows:

At this point, I believe you have a deeper understanding of the "problems encountered in the use of spring to implement static agents". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report