Reduce the encoding time and repetition by facing features. The most popular new concepts currently programming is an Aspect-Oriented Programming (AOP), ASPECT-Oriented Programming, AOP. AOP has been used primarily in academic and R & D institutions, and now begins in the mainstream development field. Like OOP, the AOP is an innovative software development method for improvement on the basis of an object-oriented programming (OOP) method. OOP introduces concepts such as packages, inheritance, and polymorphisms to establish an object hierarchy to simulate a collection of public behavior. However, OOP does not meet the requirements in public behavior in processing scope to some unrelated objects. That is, OOP allows you to define the relationship from top to bottom, but it is not suitable for definition from left to right. For example, look at the log function. The log code is often spread horizontally in all object levels, and the core function of the object it spread is not related to the core functions of the object it. This is true for other types of code, such as security, abnormal processing, and transparent persistence. This unrelated code that spreads throughout is called cross-cutting code, which is why the AOP encoding method is generated.
AOP provides a method of extracting cross-cutting code, which across the individual object hierarchies, but there is no correlation with the object code it spans it. AOP is not embedded in the class, but allows you to extract cross-cutting code into a separate module, then dynamically apply the code when needed, this separate module is called a "feature code" ("aspect" "And also translated" tags "). Dynamic application cross-cutting code is implemented by defining a specific location-entry point (PointCut) to demonstrate a specific location-cut point (PointCut) to apply cross-cutting code in your object model. When running or compiling, based on your AOP framework, the cross-cut code is inserted into the specified entry point. In essence, AOP allows you to introduce new features in the object, and objects do not need to understand the imported features. This is a very useful concept.
To further understand how the AOP works, see a typical AOP log example. Code Listing 1 shows two simple objects containing log code: Objecta and ObjectB. By standard object-oriented programming, you must write log code in the corresponding object every time you need. In the example of the code list 1, use System.out.Println () call to record the log. Another way to replace System.out.Println () call is to use log frame frames such as log4j, but this will bring extra overhead and bring unnecessary mess to use it. Whether using System.out.Println () call or log framework to implement logging, log code has no correlation with the class that is embedded. With AOP, you can dynamically insert the log code into a class that requires the log function. In this way, the object can focus on its core responsibility. Java objects focusing on their core responsibilities are often referred to as Pojos (Plain Old Java Objects): ordinary old-fashioned Java objects. You can use AOP to add logs and some other types of public capabilities without embedding unnecessary independent features. This article will introduce and explain AOP terminology to explain the difference between different AOP frames, then step by step, step by step, and an example of using AOP is used to cache. AOP Terminology AOP introduces several new terms to describe their basic concepts. These terms must be practiced to understand the AOP. The terms and descriptions of AOP introduction are listed below: 1. Recommendations - is the code that applies to or crosss you existing object models. The recommended code is the code to modify the behavior or attribute of an object. It is recommended to be also referred to as an IntroductionS or Mix-INS. 2. Entry Point - Defined in your model to apply the suggested location point. For example, the entry point definitions should be introduced to a class or which method is intercepted before execution. The entry point is also known as a JoinPoints. 3. Features - It encapsulates the suggestions and entry points into the functional unit, and its packaging mode is basically the same as the OOP uses classes to encapsulate fields and methods to the cohesive unit. For example, you may have a log feature that contains the recommendations and entry points that apply log code to all settings for objects (Setter) and getter. Selecting a framework must select an AOP framework for you before starting using AOP. Java and any other mainstream object-oriented programming language does not provide built-in support for AOP. However, there are some AOP frameworks that support Java, most of which have the same core function, but there is a difference in the way AOP connection to the object model. Some AOP frameworks use by NPC to connect to the object model, others use the agent-based system to connect. Modify the source code or modify the bytecode before compiling by the zoning method. Both of these methods can effectively result in the same result: the modified byte code embeds the AOP to the original code. The agent-based framework uses a proxy system, AOP frameworks This intercepts all methods calls that have a feature code object, and then proxy performs method calls to the objects you want. This proxy function is transparent, and it can always keep the bytecode. Another core difference between each AOP framework is the way the feature code is defined and applied. In some frameworks, through the code to define and apply features, while others require the XML configuration file to define and apply the feature code. Better, some frames simultaneously support the code and XML configuration file definitions and application feature code. Because most AOP frames have overlap, which framework uses often depends on a unique feature of a particular framework. The AOP sample application in this article is developed with a new AOP framework Dynaop.
However, the basic principles of this example application are common, applicable to all AOP frames. The Dynaop framework supports code definitions and application feature codes, which also provides a unique solution for defining and applying feature codes through the Beanshell script framework. Beanshell scripts allow you to easily write Java object scripts, such as the sample application. Cache the results of the method using a simple cache feature using AOP using AOP, indicating some of the basic concepts of AOP. The first time to call the method has a practical cache feature code, which calls the method and caches the result of its execution. Subsequent calls to this method will return the call results of the method from the cache. This basic cache feature code is very useful for the method of running time, such as a method of processing a large amount of data or a method of issuing multiple queries for a data source. If the log example in Code List 1, embedded cache functions in each required class will result in a large number of repetitive code. Moreover, the cache is not a core function for most objects, and it is very suitable for unified and reused using AOP. This cache application includes four files: CACHINTERCEPTOR.JAVA- contains AOP code that caches method call results. These codes are called suggestions. User.java - a basic class containing the "Name" field to get "getter" and "setter". The CachingInterceptor AOP code will be used in this class. Cachetest.java - an example application that describes the use of CACHINTERCEPTOR AOP code usage. Dynaop.BSH-specifies where to apply the beanshell script for CACHINTERCEPTOR Advice. These provisions are called Pointcuts. The following sections will explain each file in detail. CachingInterceptor.java file. The CachingInterceptor class will be applied to any class cache code in a standard, generic manner. This class caches the results of the method for initial call. The CACHINGINTERCEPTOR class then intercepts subsequent calls for the method that have been cached, and returns these results directly from the cache to provide a fast response for methods for running time. Code Listing 2 shows the CACHINGINTERCEPTOR class. The CachingInterceptor class contains Intercept (), CalculateCachecode () and GetFullMethodName (). The intercept () method implements the Dynaop.Iterceptor interface and contains the core suggested code of this feature. The method is called before being called by a method with a feature code, and then it is responsible for calling this method with a feature code. Importantly, the intercept () method is a proxy for inserting the functionality executed before the method is proxy. In addition, the intercept () method controls whether the agent method is truly called. The intercept () method of the CachingInterceptor class calls the calculatecachecode () method to calculate a cache key value to the method called. Then use this key value to be called method (the getName () method in the User class in this example is defined in the back section) Find cache, see if its result has been cached. If the result has been cached, then the result is returned, and the user.getname () method is not called. If the result is not cached, the user.getname () method is called, and the result can be cached for subsequent calls. This is a basic cache implementation, regardless of whether the time limit data items in the cache are long. User.java file. The User class is a simple class, with only one field and the acquisition and setting method for this field. The CachingInterceptor feature code is used for the getName () method of this class.
Code Listing 3 shows the User class. Note that each method in this class has a System.out.Println () call indicating that the method is called. These calls are used to illustrate what results will occur when the sample is run. Next Learn More AOP Information AOSD.Neteclipse.org/aspectJ Beanshell Download Dynaop This application reads more James Holmes Articles Java Art Struts: Complete Reference Manual Cachetest.java file. The Cachetest class contains the code for how the CACHINGINTERCEPTOR feature code is used. This application simply instantizes a User object, and calls its GetName () method multiple times, showing the method results in the continuous call cache and direct access to the method. Code Listing 4 shows the Cachetest class. Cachetest contains a main () method, so it can run as a separate application. The Dynaop framework uses a runtime-based knitting mechanism to insert the AOP code into the object, which directly instantiate the User object, use this new operator, rather than returns an object with a feature code. In order to use the CachingInterceptor feature code in the sample application, the Cachetest class instantiates the User object through the Dynaop ProxyFactory class. Once you via proxyFactory.getInstance () .Extend () instantiates this object, you can use the User object as used as any other object. From this point, the execution of the AOP code is transparent. Dynaop.bsh file. The Dynaop.BSH file is a Beanshell script that specifies the entry point for applying the CACHINTERCEPTOR recommendation. The following is the content of the Dynaop.bsh file: // Apply Interceptor to all // getter methods.interceptor (user.class, get_methods, new cachinginterceptor (); this simple script specifies that the CACHINGINTERCEPTOR recommendation will be applied to all acquisition methods of the User class . GET_METHODS is one of several constants for Dynaop to easily specify a set of cut points. You can also specify each method for some cleansing points if necessary. Compile and run this app, compile and run this application. Suppose you put the four files of this application in the Dynaop you install, such as C: / Java / Dynaop, the following command line will compile this application: javac -classpath ./bsh-2.0b1.jar; ./cglib-asm-1.0.jar ;./dynaop 1.0-beta.jar; ./jakarta-oro-2.0.7.jar * .java After compiling, run it: java -classpath. /; ./bsh-2.0b1.jar ;./cglib-asm-1.0.jar ;./dynaop-1.0-beta.jar ;/jakarta-oro-2.0.7.jar Cachetest Figure 1 shows this sample application operation result. This application calls the setName () method of the User class first, and then call the getName () method. Both calls are enabled in the User class, and subsequent calls to getName () are found in the cache and no longer enabled in the User class.