Spring AOP programming

xiaoxiao2021-03-29  230

This is an article I found online about the use of Spring AOP, explaining the unknown Spring AOP simple and clear. Original article

Here.

AOP is becoming the next holy cup of software development. Using AOP, you can inject the code of the ASPECT into the main program, usually the main purpose of the main program is not to process these aspect. AOP can prevent code confusion.

In order to understand how the AOP does this, consider the job of the log. The log itself is unlikely to be the main task of the main program you develop. If you can inject the "invisible", the general log code is injected into the main program, then how is it. AOP can help you do it.

Spring framework is a very promising AOP technology. As a non-intrusive, light AOP Framework, you can use it in a Java program without using a pre-compiler or other meta tag. This means that only one person in the development team must deal with AOP Framework, others are still like the usual programming.

AOP is the root cause of a lot of intuition imperceptible terms. Fortunately, you can write a AOP module as long as you understand the three concepts. These three concepts are: Advice, Pointcut and Advisor.

Advice is the code you want to inject to different places within another program.

PointCut defines a location that needs to be injected into the Advice, which is usually a PUBLIC method for a particular class.

Advisor is the code of PointCut and Advice, which is the code that injects Advice to predefined positions in the main program.

Since we know that you need to use Advisor to inject "unacceptable" Advice to the main code, let us implement an example of Spring AOP. In this example, we will implement a Before Advice, which means that the Advice's code is executed before the called public method is called. The following is the implementation code of this Before Advice:

Package com.Company.springaop.test;

Import java.lang.reflect.method;

Import org.springframework.aop.methodbeforeadvice;

Public Class TestBeforeadvice Implements MethodbeforeAdvice {

Public void before (Method M, Object [] args, object target

Throws throwable {

System.out.println ("Hello World! (BY"

this.getClass (). getname ()

")");

}

}

Interface MethodbeforeAdvice has only one method Before needed, it defines the implementation of Advice. The Before approximates three parameters that provide considerable information. The parameter method m is the method of starting after the start of Advice. The method name can be used as a condition that determines whether or not the code is executed. Object [] args is the array of parameters to pass to the called Public method. When the log is required, the parameter args and the name of the executed method are very useful information. You can also change the parameters transmitted to M, but carefully use this feature; programmers who write the original main program do not know that the main program may conflict with the incoming parameters. Object target is a reference to performing a method M object.

In the beanimpl class below, Advice is executed before each public method calls:

Package com.Company.springaop.test;

Public class beanimpl imports bean {

Public void thethod () {

System.out.println (this.getClass (). GetName () "." New exception (). GetStackTrace () [0] .getMethodName ()

"()"

"Says Hello!");

}

}

The class beanimpl implements the following interface bean:

Package com.Company.springaop.test;

Public interface bean {

Public void thethod ();

}

Although it is not necessary to use an interface, it is also good programming practice for interface instead of implementation programming, Spring encourages this.

PointCut and Advice are implemented by profiles, so you only need to write Java code for the main method:

Package com.Company.springaop.test;

Import org.springframework.context.ApplicationContext;

Import org.springframework.context.support.filesystemxmlapplicationContext;

Public class main {

Public static void main (String [] args) {

// r r r Configuration File

ApplicationContext CTX

= New FileSystemXmlapplicationContext ("SpringConfig.xml");

// instantiate an Object

Bean x = (bean) CTX.getBean ("bean");

// Execute the public method of the bean (the test)

X.Themethod ();

}

}

We start with reading and processing profiles, then you will create it immediately. This configuration file will be "glue" as different parts of the bonder. After reading and handling the configuration file, we get a Create Plant CTX. Any Spring management object must be created through this plant. Objects can be used normally after being created by the factory.

Each portion of the program can be assembled with the configuration file.

com.company.springaop.test.bean

Theadvisor

com / .company / .springaop / .test / .bean / .themethod

The order of four beans defined is not important. We now have an Advice, an Advisor that contains regular expressions Pointcut, a main program class, and a configuration interface, through the factory CTX, this interface returns a reference to your own implementation.

BEANIMPL and TESTBEFOREADVICE are all direct configuration. We create a bean element with a unique ID and specify an implementation class. This is all work.

Advisor is implemented by a RegexMethodPointCutAdvisor class provided by Spring Framework. We use the Advisor's attribute to specify the Advice-Bean it needs. The second attribute defines PointCut with regular expressions to ensure good performance and readability.

Finally configured Bean, it can be created through a factory. Bean's definition seems to be complicated than actually. Bean is an implementation of ProxyFactoryBean, which is part of Spring Framework. This bean behavior is defined by three attributes:

Property ProxyInterface defines an interface class. Attribute Target points to a bean that is locally configured, this bean returns an implementation of an interface. Properties InterceptorNames is the only properties that allows you to define a list of values. This list contains all the Advisor that needs to be performed on the Beantarget. Note that the order of the Advisor list is very important.

Spring tool

Although you can manually modify the Ant build script, but use Springui (the Springui is now part of the Spring Framework, and is renamed Spring-IDE), using Spring AOP to become very simple, just point the mouse. You can install Springui into a plug-in in Eclipse. Then, you only need to right to right on your Project and select "Add Spring Project Nature". In the Project property, you can add a spring profile under Spring Project. Add the following class library to Project: aopalliance.jar before compiling: AopAlliance.jar, Jakarta-ORO-2.0.7.jar and Spring.jar. When you run the program, you will see the following information: ... (logging information)

Hello World! (By com.company.springaop.test.testbeforeadvice)

com.company.springaop.test.beanimpl.themethod () Says Hello!

pros and cons

Spring is more advantageous than other Framework, because in addition to AOP, it provides more functions. As a light Framework, it can play a role in the J2EE different parts. Therefore, even if you don't want to use Spring AOP, you may still want to use Spring. Another advantage is that Spring does not require development of all teams to use it. Learning Spring should start from the first page of Spring Reference. After reading this article, you should be able to better understand the Spring Reference. Spring The only disadvantage is that there is a lack of more documents, but its mailing list is a good supplement, and it will continue to have more documents.

转载请注明原文地址:https://www.9cbs.com/read-130479.html

New Post(0)