AOP is becoming the next Holy Grail of software development. With AOP, You can process Code Injection master Program Generally, the main program does not aim to process these aspect. AOP can prevent code confusion.
To understand how AOP achieves this, consider logging. The log itself is unlikely to be the main task of the main program you developed. It would be nice to inject "invisible" and common Log Code into the main program. AOP can help you.
Spring framework is a promising AOP technology. As a non-invasive and lightweight AOP framework, you can use it in Java programs without using pre-compilers or other meta-labels. This means that only one person in the development team needs to deal with the AOP framework, and others are still programming as usual.
AOP is the root of many terms that are hard to comprehend by intuition. Fortunately, you only need to understand three concepts and write the AOP module. These three concepts are: advice, pointcut, and advisor. Advice is the code you want to inject into different places in other programs. Pointcut defines the location where advice needs to be injected. It is usually a public method of a specific class. Advisor is the assembler of pointcut and advice. It is the code that injects advice into pre-defined positions in the main program.
Now that we know that we need to use advisor to inject "invisible" advice into the main code, let's implement a Spring AOP example. In this example, we will implement a before advice, which means that the advice code is executed before the called public method starts. The following is the implementation code of this before advice:
Code:
|
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! (" + This. getclass (). getname () + ")"); } } |
The interface methodbeforeadvice must be implemented only by the before method, which defines the implementation of advice. The before method shares three parameters, which provide a wealth of information. The method M parameter is executed after advice starts. The method name can be used as a condition to determine whether the code is executed. Object [] ARGs is an array of parameters passed to the called public method. When logs are required, The args parameter and the name of the executed method are useful information. You can also change the parameter passed to m, but be careful to use this function. programmers who write the original main program do not know that the main program may conflict with the input parameter. Object target is a reference to the execution method M object.
In the following beanimpl class, advice is executed before each public method is called:
Code:
|
Package com. Company. springaop. test; Public class beanimpl implements Bean { Public void themethod (){ System. Out. println (this. getclass (). getname () + "." + New exception (). getstacktrace () [0]. getmethodname () + "()" + "Says hello! "); } } |
Beanimpl class implements the following interface Bean:
Code:
|
Package com. Company. springaop. test; Public interface bean { Public void themethod (); } |
Although it is not necessary to use interfaces, it is a good programming practice to use interfaces instead of implementing programming. Spring also encourages this.
Pointcut and advice are implemented through the configuration file. Therefore, you only need to write the Java code of the main method:
Code:
|
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 ){ // Read the 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 (); } } |
Starting from reading and processing the configuration file, we will create it immediately. This configuration file serves as the "glue" for different parts of the bonding program ". After reading and processing the configuration file, we will get a create factory CTX. Any spring-managed object must be created through this factory. The object can be used normally after being created through the factory.
The configuration file can be used to assemble every part of the program.
Code:
|
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd"> <Beans> <! -- Config --> <Bean id = "Bean" class = "org. springframework. AOP. Framework. proxyfactorybean"> <Property name = "proxyinterfaces"> <Value> com. Company. springaop. Test. Bean </value> </Property> <Property name = "target"> <Ref local = "beantarget"/> </Property> <Property name = "interceptornames"> <List> <Value> theadvisor </value> </List> </Property> </Bean> <! -- Class --> <Bean id = "beantarget" class = "com. Company. springaop. Test. beanimpl"/> <! -- Advisor --> <! -- Note: An Advisor assembles pointcut and advice --> <Bean id = "theadvisor" class = "org. springframework. AOP. Support. regexpmethodpointcutadvisor"> <Property name = "advice"> <Ref local = "thebeforeadvice"/> </Property> <Property name = "pattern"> <Value> com \. Company \. springaop \. Test \. Bean \. Themethod </value> </Property> </Bean> <! -- Advice --> <Bean id = "thebeforeadvice" class = "com. Company. springaop. Test. testbeforeadvice"/> </Beans> |
The order defined by the four beans is not important. Now we have an advice, an advisor that contains the regular expression pointcut, a main program class, And a configured interface. Through the factory CTX, this interface returns a reference implemented by itself.
Beanimpl and testbeforeadvice are both directly configured. We use a unique ID to create a bean element and specify an implementation class. This is all the work.
Advisor is implemented through a regexmethodpointcutadvisor class provided by Spring framework. We use an attribute of advisor to specify the desired advice-bean. The second attribute defines pointcut using a regular expression to ensure good performance and ease of use.
The final configuration is Bean, which can be created through a factory. Bean definitions seem more complex than they actually do. Bean is an implementation of proxyfactorybean, which is part of the Spring framework. The bean behavior is defined by the following three attributes:
- The property proxyinterface defines the interface class.
- The target attribute points to a locally configured bean, which returns an interface implementation.
- The attribute interceptornames is the only attribute that allows defining a Value List. This list contains all the advisor that needs to be executed on beantarget. Note that the order of the Advisor list is very important.
Spring Tool
Although you can manually modify the ant build script, using spring UI (now part of Spring framework and renamed it spring-ide) makes it easy to use Spring AOP, just a few clicks. You can install springui into a plug-in of Eclipse. Then, you just need to right-click your project and select "add spring project nature ". In the project attribute, you can add a spring configuration file under "spring Project. Add the following class libraries to project: aopalliance. jar, commons-logging.jar, jakarta-oro-2.0.7.jar, and spring. jar before compilation. When running 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!
Advantages and disadvantages
Spring is more advantageous than other frameworks, because it provides more functions than AOP. As a Lightweight Framework, it can play a role in different parts of J2EE. Therefore, even if you do not want to use Spring AOP, you may still want to use spring. Another advantage is that spring does not require all developers to use it. Spring learning should start from the first page of spring reference. After reading this article, you should be able to better understand spring reference. The only drawback of spring is the lack of more documents, but its mailing list is a good supplement, and more documents will appear continuously.
Source:
http://spring.jactiongroup.net/viewtopic.php? T = 478