spring3.0-aop-A small example based on @aspectj facets

Source: Internet
Author: User

Conditions:

The 1.JDK version is 5.0 or above, otherwise the annotation technology cannot be used

2.jar Package:

Aspectjweaver-1.7.4.jar

Aspectjrt-1.7.4.jar

Spring-framework-bom-4.0.5.release-sources.jar

Start writing a simple @aspectj-based slice

// Proxy Object  Public class Testweaver {    publicvoid  hello () {        System.out.println ("Hello,world" );    }}

@Aspect//marked as a tangent by means of annotations Public classTestannotationaspect {//defining Pointcut @before and enhanced types@Before ("Execution (* Hello (..))"))     Public voidDobefore () {System.out.println ("Pre-notification"); }    //defining Pointcut @after and enhanced types@After ("Execution (* Hello (..))"))     Public voidDoafter () {System.out.println ("Final Notice"); }}

 Public classtest{ Public Static voidMain (string[] args) {//Creating ObjectsAspectjproxyfactory A =Newaspectjproxyfactory (); Testweaver T=NewTestweaver (); //set the target objectA.settarget (t); //Set the Slice classA.addaspect (Testannotationaspect.class); //Create a fabric-in proxy objectTestweaver t2 =A.getproxy ();    T2.hello (); }}

The entire process:

In this example, the Testweaver class and the Testannotationaspect class are just a simple class, and the Testannotationaspect class labels the @aspectj annotations,

This makes it possible for a subsequent third-party program to detect whether the class that owns the @aspect is a slice class.

Second, the Dobefore () in the Testannotationaspect class is labeled @before, which means pre-enhancement. The following execution () uses a method of the Pointcut function to match the pattern string to indicate the member value.

The member value is a @aspectj pointcut expression, which means that on the target class's Hello () texture enhancement, you can bring in arbitrary arguments and any return value.

Finally, a slice agent is generated for the Testweaver class through Org.springframework.aop.aspectj.annotation.AspectJProxyFactory.

spring3.0-aop-A small example based on @aspectj facets

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.