A discussion on the development of AOP the use of AOP and ASPECTJ

Source: Internet
Author: User

first, Why AOP technology is requiredAOP is a very mature technology. If the project has method a, method b, method C ... Wait for multiple methods, if the project needs to be method a, method b, method C ... This batch of methods increases the crosscutting treatment with a general nature. It is possible to visualize the idea of crosscutting treatment with a general nature: in the old tradition, the practice was

1, first define a advice method, the method to achieve this general nature of the crosscutting processing.
2. Open method a, method b, method C ... Source code modification, Make method a, method b, method C ... To call the advice method. Customer Phone: adds a log for each method. Customer Phone: adds permission control before each method. Customer Phone: for each method add .... if you use aop, you can do this without modifying method a, method b, method C ..., But you can add a call to method a, method b, and method C to the advice method.   aspect-oriented Programming (AOP) is a complement to object-oriented programming (OOP):The AOP framework has the following two Characteristics: 1. Good isolation between the steps.
2, Source Code Independence. second, the function of AOPEnsure that programmers do not modify method a, method b, method C ... Can be used for method a, method b, method C ... Increased General Processing. The essence of aop: still going to "modify" method a, method b, method C......--only This modification is done by the AOP framework, the programmer does not need to change. AOP requires to be modified, exactly how to modify the method a, method b, Method ... There are two ways to implement Aop:
    1. The AOP framework modifies the target class during the compilation phase, and the obtained class file is already Modified. Generate a static AOP proxy class (the generated *.class file has been changed and needs to use a specific compiler). Represented by Aspectj-static AOP Framework.
    2. At runtime, the AOP framework dynamically generates an AOP proxy (which dynamically generates an AOP proxy class in Memory) for the purpose of enhancing the target Object. It does not require a special compiler. is represented by spring Aop. -dynamic AOP Framework.
Which of the above two types is better? It is obvious that the static AOP framework is Better. Below we go into Aspectj's study third, actual combat aspectjAspectJ is a java-based AOP framework that provides powerful AOP capabilities, and many other AOP frameworks draw on or adopt some of these Ideas.1. Download and install ASPECTJ1) run, Download the resulting installation jar Package.

Start the downloaded JAR file in the command line Window: Java-jar aspectj-1.6.10.jar, the installation screen will allow you to select Java and select the Java directory you have Installed. Install the software (absolute green) into the specified directory (the author installs on the C drive).

2) The system should also add the C:\Java\aspectj1.6\bin path to the PATH environment variable,

Add C:\Java\aspectj1.6\lib\aspectjrt.jar and Aspectjtools.jar to the CLASSPATH environment Variable.

After successfully installing aspectj, you will see the following file structure under the E:\Java\AOP\aspectj1.6 path (ASPECTJ installation path):
    • Bin: This path holds the aj, aj5, ajc, ajdoc, ajbrowser commands, where the AJC command is most commonly used, it acts like a javac, and is used to compile-time enhancements to ordinary Java Classes.
    • Docs: This path contains ASPECTJ instructions, reference manuals, API documentation, and More.
    • Lib: the 4 jar files under this path are the core class libraries of ASPECTJ.
    • The relevant authorization File.
Open the DOS window, enter the AJC command, you can see all the information of the output AJC command, to know the installation and environment variable configuration success:

2. Using ASPECTJNext we simulate a common program: userservice:

PackageCom.Mybry.Aop.Service;
Public Class UserService{
Public IntAddUser(){
system.. Println ( "simulates adding a user's method. " return 20 }
public Validatelogin () {
systemout. Println ( "verify user Login. " }
} /span>

Bookservce:

PackageCom.Mybry.Aop.Service;
Public Class Bookservice{
public int addbook< Span class= "pun" > (string Name,int Price) {
system.. Println ( "adding books, title is:" +name ++price return 100 }
} /span>

Compile run Result: These two classes are exactly equivalent to our method a, method B ..... What if a customer now asks to add permission checks before each method? Here we are using ASPECTJ to achieve this Function. Now let's add this AOP functionality based on this simulation: let's write a AspectJ class that checks for Permissions:

Example 1,authaspect:

PackageCom.Mybry.Aop.Aspectj;
PublicAspectAuthaspect{
//Advice
//execution (* Com.mybry.aop.service.*.* (..) Any method parameter that executes any of the classes under any of the returned values is not limited to
before (): execution (* Com. Mybry. Aop. Service.*.* (..)) {
//the Original method was modified, Enhanced.
system.. Println ( "----------impersonation execution permission check----------" }
} /span>

Note that this kind of color declaration type: aspect, yes, This is the type that write aspect must declare, only the ASPECTJ compiler can recognize. Then compile the execution with the duni>ajc–d *.java command: so happy, it's done! What if at this point the customer asks to increase the processing of things in each method? good, here is the thing processing class: example 1,txaspect:

PackageCom.Mybry.Aop.Aspectj;
PublicAspectTxaspect{
Around means to add to the front and back of the method
ObjectAround():Execution(*Com.Mybry.Aop.Service.*.*(..)) {
//the Original method was modified, Enhanced.
system.. Println "= = = Analog Open transaction = =" object rvtval = proceed ();
system.. Println "= = = = =" return Rvtval }
} /span>

ok, let's compile and run it: ok! our aspect AOP program ends here!

A discussion on the development of AOP the use of AOP and ASPECTJ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.