Copy from: http://hi.baidu.com/guxing820/blog/item/ae44da2f65c5db3e1e308974.html
Thanks for author!
This is an online tutorial on Spring AOP programming. After reading this article, Spring AOP is no longer difficult to understand. Therefore, I have translated it into Chinese and recommended it to Spring AOP beginners. This is the link of the translation.
AOP is becoming the next Holy Grail of software development. With AOP, You can inject the code that processes aspect into the main 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.