Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

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

Shulou Source: shulou.com Published: 2022-06-02 03:38:13 09月25日 Update

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!

Tags: Problem static agent content method code file error learning testing configuration wrong practical deeper interest dynamic reason practical practical easy to operate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker macOS MariaDB Shulou Technology Redmi