Integrating ASPECTJ in Spring 2.0

Source: Internet
Author: User
Tags aop requires

In the Java language, from the way of weaving, there are three ways of weaving: compile-time weaving, class-loading period weaving and running period weaving. Compile-time weaving refers to the use of special compilers in the Java compilation period, weaving slices into Java classes, and class load-time weaving refers to the weaving of slices through a special class loader when the class bytecode is loaded into the JVM, and the run-time weaving is through the cglib tool or the JDK dynamic proxy.

The ASPECTJ is a language-level AOP implementation that provides complete AOP support by weaving the facets in the form of compile-time weaving and class-loading periods. It defines the slice in the AspectJ language, weaving the slices into the Java class at compile time or class load time.

In a lower version of Spring, you can only define slices through interfaces, and in spring 2.0 you define pointcuts through the ASPECTJ pointcut expression syntax, and Spring 2.0 uses the ASPECTJ parsing packet to parse the tangent point to weave the cut. But that's not what we're going to talk about in this article. In this article, we want to integrate spring and ASPECTJ from a higher level, directly using the ASPECTJ, and let the spring IOC container manage the slice instance.

Spring AOP provides limited AOP support that, in general, has been able to meet our development requirements, but if there are higher requirements for AOP (such as materialized slices, attribute access facets, etc.), then you need to use ASPECTJ support. AspectJ can also take advantage of the spring IOC's dependency injection capabilities, which complement each other and harps together.

How to use AspectJ LTW

As we mentioned earlier, ASPECTJ provides two ways to weave, the first through a special compiler, in the compilation period, the AspectJ language to write the slice class into the Java class, you can do this through an ant or Maven task, the second way is the class load period weaving, Also referred to as LTW (Load time weaving). Here, we only introduce the weaving of LTW, compile the period of weaving see: http://www.eclipse.org/aspectj/doc/released/devguide/antTasks.html.

There are two main steps to use the AspectJ LTW, first, to set the LTW's loom class package through the JVM's-javaagent parameters to proxy the JVM default classloader; second, The LTW loom requires a aop.xml file in which to specify the slice class and the target class that needs to be woven into the slice. Now, let's take a look at the specific approach:

1. Under normal circumstances, we will not directly in the DOS window, through the Java command to start the application or test. This requires that we set the parameters of the JVM in an IDE environment, or in an application deployment environment. We take eclipse and Tomcat, respectively, to describe how to set the-javaapent JVM parameters in the IDE and the Web application server.

Settings under Eclipse

In eclipse, if we want to change JVM parameters, you can select a-> in the project class navigation tree to right-click->run As->run ..., you can set the run properties of the class in the Run Settings window that pops up, and switch to arguments tab page, in VM arguments, specify the AspectJ loom class package by-javaagent, as shown in the following illustration:

Here, we set it to:-javaagent:d:\masterspring\resources\aspectj-1.5.3\lib\aspectjweaver.jar

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.