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