This is a tutorial discovered online. After reading this article, Spring AOP is no longer difficult to understand, so I translated it into Chinese, recommend to beginners of Spring AOP. This is the link to the translation. 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:
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! (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:
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! ");}} The class beanimpl implements the following interface bean:
Code: 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:
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 ();
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.
Code: XML Version = "1.0" encoding = "UTF-8"?>
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 defines three properties: Properties proxyinterface defines the 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 (Transplement: 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. You will see the following information when running the program: ... (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 Framework, because in addition to AOP, it provides more features. 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.