As the first part of the Aspect-Oriented Programming, AOP in the Spring Framework, this article describes the basics that enables you to quickly develop aspect-oriented properties in Spring. Use tracking and recording aspects (facing HelloWorld) as an example, this article shows how to declare entry points and notifications in the use of the Spring framework for application. The second part of this series will more deepen how to use all notifications and entry points in Spring to achieve more practical aspects and aspect design patterns.
The purpose of this article is not to introduce all important elements constituting the modular J2EE system - the Spring Framework - we will only put the attention on the AOP functionality provided by Spring. Due to Spring modular design methods, we can use only the AOP elements of the frame without doing too much consideration of other modules constituting the Spring framework.
Spring provides Spring aspects in AOP?
"Its goals are not to provide the most complete AOP implementation (although Spring AOP is very powerful); but to provide AOP implementation with Spring Io's close integration to help solve common problems in enterprise applications."
Spring Framework Reference Document
In order to achieve this goal, the Spring Framework currently supports a set of AOP concepts from entry points to notifications. This article will show how to use the following AOP concept in the Spring framework:
Notification: How to make a Before notification, AfterReturNing notification, and AfThrowing notifications are declared as beans.
PointCut: How to declare static entry point logic to link all the contents in the XML Spring Bean Configuration file.
Advisor: The way the associated entry point definition is notified in the way.
Setting scenario: a simple example application
"In general, Spring is not pre described. Although it is very easy to use, it avoids enforcing a specific method."
Spring Framework Reference Document
To try the AOP function of the Spring framework, first we have to create a simple Java application. The IbusinessLogic interface and the BusinessLogic class provide the BEAN in the Spring Frame. Although the interface is not required for our simple application logic, it is a good practice recommended by the Spring framework.
Public interface ibusinessLogic {public void foo ();} public class businesslogic imports iblic curigic {public void foo () {system.out.println ("INSIDE BusinessLogic.foo ()");}}
You can write the MainApplication class, thereby practicing the public method of BusinessLogic Bean.
import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class MainApplication {public static void main (String [] args) {// Read the configuration file ApplicationContext ctx = new FileSystemXmlApplicationContext ( "springconfig. xml "); // Instantiate an object IBusinessLogic testObject = (IBusinessLogic) ctx.getBean (" businesslogicbean "); // Execute the public // method of the bean testObject.foo ();}} class and its associated interfaces in BusinessLogic There is nothing to pay attention to in what you need. However, the way the mainapplication class is initialized by the businessLogic object. By using CTX.getBean ("BusinessLogicbean") call, MainApplication will load and manage the task of the BEAN instance of the BusinessLogic class to the Spring framework.
Allow Spring to control the initialization of BusinessLogic Beans, which makes Spring runtime, which has the opportunity to execute all the Bean-related management tasks required to perform the J2EE system before the BEAN is returned to the application. The Spring runtime configuration can then determine which tasks and modules are applied to the bean. This configuration information is provided by an XML file similar to the following:
XML Version = "1.0" encoding = "UTF-8"?>
This configuration file, SpringConfig.xml, specifies the bean that you want to load with IbusinessLogic. This bean is then associated with the businesslogic implementation class. It seems that it costs a lot of effort to load a simple bean and call a method, but you have to know that this profile is just a manifestation of the Spring framework to apply a number of features of its components to the application. Figure 1 shows the basic sequence of sequence: MainApplication is carried out, no application.
Figure 1. There is no sequence diagram for the application of the businessLogic bean
Application Method Tracking (Method Tracing) may be the most basic aspects. This may be the easiest way you have found, so it is a good starting point for researching new AOP implementations.
Method Tracking Aspects captures the calls of the tracking method in a target application, and the return value of the method, and displays this information in some way. In AOP, the BEFORE and AFTER types notified are used to capture these types of joint points because both notices can trigger before or after the method calls. Using the Spring Framework, method tracking Before notification is declared in the TracingBeforeadvice class.
import java.lang.reflect.Method; import org.springframework.aop MethodBeforeAdvice;. public class TracingBeforeAdvice implements MethodBeforeAdvice {public void before (Method m, Object [] args, Object target) throws Throwable {System.out.println ( "Hello World! (by " this.getClass (). getname () ") ");}}
Similarly, the After notification can be declared in the TracingAfteradvice class.
import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; public class TracingAfterAdvice implements AfterReturningAdvice {public void afterReturning (Object object, Method m, Object [] args, Object target) throws Throwable {System.out.println ("Hello World! (By" this.getClass (). Getname () ")");}}
Both classes indicate specific notifications by implementing the appropriate notification interface of the Spring framework. Each type of notification specifies the implementation of Before (..) or afterReturning (..) method to make Spring runs when you tell where the appropriate joint point will appear. It is worth noting that TracingAfteradvice is actually extension from AfterReturnangAdvice, indicating that only the notification is run when the joint point is not abnormal.
In order to associate with the appropriate coupling points in the application, some modifications must be made to SpringConfig.xml.
XML Version = "1.0" encoding = "UTF-8"?>
Because this example uses org.springframeth.aop.support.RegexpMethodPointCutAdvisor entry point Advisor, the entry point logic is specified using the regular expression. Regular expressions are used to identify the joint points of the public interface to the IbusinessLogici interface. Here are some regular expressions that can be used to specify different joint points collections on the IbusinessLogic interface:
The final bean declaration in the SpringConfig.xml file specifies the class that implements the notification bean.
Since the right configuration of tracking has been specified, the next time the mainApplication is executed, these aspects will be woven in the initialization process, and all the methods in the BusinessLogic BEAN will be tracked, as shown in Figure 2.
Figure 2. Method Tracking Sequence diagram after application to BusinessLogic Bean (click on the image to view larger image)
Method Tracking Aspects and Example The source code for the application can be downloaded in the reference information section at the end of this article.
The reuse of the aspect can be extended to method tracking, providing a slightly complex record (Logging). The record provides a very good reuse example because many of the features required for recording are already included in method tracking.
In this case, the recording is extended to display the information related to the additional abnormality caused by the additional (during the execution of the application).
To fully use records, you need to make some changes to the application. The BusinessLogicException exception class provides an exception that can be raised by the new VOID BAR () method that can be achieved by the IbusinessLogicinterface interface and BusinessLogic. public class BusinessLogicException extends Exception {} public interface IBusinessLogic {public void foo (); public void bar () throws BusinessLogicException;} public class BusinessLogic implements IBusinessLogic {public void foo () {System.out.println ( "Inside BusinessLogic.foo ( ) ")");} Public void bar () throws businesslogicException {system.out.println ("INSIDE BusinessLogic.bar ()"); throw new businessxception ();}}
The MainApplication class will now make additional calls for the Void Bar () method, and process the exceptions that may be raised by this method.
import org.springframeworkcontext.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class MainApplication {public static void main (String [] args) {// Read the configuration file ApplicationContext ctx = new FileSystemXmlApplicationContext ( "springconfig.xml" ); // Instantiate an object IBusinessLogic testObject = (IBusinessLogic) ctx.getBean ( "businesslogicbean"); // Execute the public methods of the bean testObject.foo (); try {testObject.bar ();} catch (BusinessLogicException ble ) {System.out.println ("caught businessxcext");}}}
TracingBeforeadVice and TracingAfteradvice notifications from method tracking can be used as an overall reuse. The LoggingThrowsAdvice class provides a notification for new exception records.
import org.springframework.aop.ThrowsAdvice; import java.lang.reflect.Method; public class LoggingThrowsAdvice implements ThrowsAdvice {public void afterThrowing (Method method, Object [] args, Object target, Throwable subclass) {System.out.println ( " Logging That a " Subclass " Exception Was thrown. ");}}
The final step in the application record is to modify the SpringConfig.xml configuration file to include the newly added LoggingThrowSAdvice notification. Figure 3 shows the UML sequence diagram for running the mainApplication and uses the Spring framework.
Figure 3. Recording application to the sequential diagram after the businessLogic bean (click on the image) This record clearly shows how to reuse the existing aspects and how to use notifications in the Spring framework. By rewriting existing methods for rewriting existing method tracking for Before and After notifications, more complex recording frames can be recorded, such as log4j. For the source code for recording aspects and examples, see the reference information section at the end of this article.
Conclude
This article shows some simple aspects of the basic AOP structure in the Spring framework. In this series of next article, we will introduce some more practical aspects, explore the life cycle, use the Around Notification of the Spring Frame, and use Spring to apply AOP mode.