Spring (five)

AspectJ

       AspectJ is a java-based AOP framework predicted, it provides a powerful AOP capabilities Spring2.0 SpringAOP after the introduction of support for AspectJ

       AspectJ AOP achieved in two ways:

  1. Xml-based declarative AspectJ
    1. Configuration section

Spring in the configuration file, the configuration of the matter cut using <aop: aspect> element, which will be converted into a defined section SpringBean Bean, so to define a first common SpringBean in the configuration file, the <aop: ref aspect> element can be introduced

<Aop: aspect> element common attributes:

id is used to define the cut surface is a group identifier name

ref: used to refer to the ordinary SpringBean

    1. Configuration entry point

Spring in the configuration file, entry point by <aop: pointcut> element class definition. When <aop: pointcut> element as <aop: config> child element to define elements, indicating that the entry point is a global entry point, it can be shared by a plurality of facets. When <aop: pointcut> element as <aop: adpect> child element of the element, the current section indicates that the active pointcut detachment

<Aop: pointcut> common attributes

A unique identifying name for the specified entry points: id

expression: specifies the entry point for the associated pointcut expression

    1. To configure notifications

Graphic:

Example code:

       Build turnkey class

       Section categories:

       package com.bdqn.cn.aspect;

 

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

 

 

// cut class 

public class MyAspect {

      

       // Pre-notification

       public void myBefore(JoinPoint joinPoint){

              System.out.println ( " Before advice: Make a check action ...");

              System.out.println ( " target class:" + joinPoint.getTarget ());

              System.out.println ( " implanted target method:" + joinPoint.getSignature () getName ( ).);

       }

      

       // post notice

       public void myAfterReturning(JoinPoint joinPoint){

              System.out.println ( " Rear notice: Analog Logging ...");

              System.out.println ( " implanted target method:" + joinPoint.getSignature () getName ( ).);

       }

      

       /*

        *   Around advice

        * ProceedingJoinPoint is sub-interface JoinPoint, showing the target method can be performed

        * 1 , must return type is Object

        * 2 , must receive a parameter of type ProceedingJoinPoint

        *          3、必须throws Throwable

        *

        */

       public Object myArround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{

              // start

              System.out.println ( " Surround begin: Before performing the target method to simulate open affairs ...");

              // execution of the current target method

              Object obj = proceedingJoinPoint.proceed();

              // End

              System.out.println ( " around the end: After executing the target method to simulate close the transaction ...");

             

              return obj;

       }

      

       // exception notification

       public void myAfterThrowing(JoinPoint joinPoint,Throwable e){

              System.out.println ( " exception notification: wrong" + e.getMessage ());

       }

      

       // final notice

       public void myAfter(){

              System.out.println ( " Final Notice: the release of resources after the end of the simulation method ...");

       }

      

      

}

Write configuration file:

       <! - the target class ->

       <bean id="userDao" class="com.bdqn.cn.dao.UserDaoImpl" />

       <! - defined aspects Bean ->

       <bean id="myAspect" class="com.bdqn.cn.aspect.MyAspect" />

       <aop:config>

              <! - 1 disposed cut ->

              <aop:aspect id="aspect" ref="myAspect">

                     <- 2! Disposed pointcut pointcut expression: execution (* * .. * * (..) -.>

                     <aop:pointcut expression="execution(* *..*.*(..))" id="mypointcut"/>

                     <-! 3 , configure notifications ->

                     <! - Configure pre-notification ->

                     <aop:before method="myBefore" pointcut-ref="mypointcut"/>

                     <!--

                            Configuring rear notification, performed after the method returns, the return value can be obtained,

                            returning property: after advice for setting the second parameter is the name of the Object Type

                     -->

                     <aop:after-returning method="myAfterReturning" pointcut-ref="mypointcut" returning="returnVal"/>

                     <! - arranged around advice ->

                     <aop:around method="myArround" pointcut-ref="mypointcut"/>

                     <! - Configure the exception notification ->

                     <aop:after-throwing method="myAfterThrowing" pointcut-ref="mypointcut" throwing="e"/>

                     <! - Configure a final notice ->

                     <aop:after method="myAfter" pointcut-ref="mypointcut"/>

              </aop:aspect>

       </aop:config>

By viewing the configuration file, we know that in <aop> programming there is no written content about the target class, then enhance how the code is implanted into the target class in it?

       <aop: config> element and its subelements

<AOP: config> : top developed AspectJ configuration elements, at <beans> configuration file may contain a plurality of the element

<AOP: Aspect> : a configuration section, <aop: config> child element and attribute definitions section specifies ref

<AOP: the pointcut> : Configuration entry point, <aop: aspect> child element and attribute specifies the notification enhanced expression those  

Pointcut expression:

       <aop: pointcut> common attributes

              the above mentioned id : used to specify the name that uniquely identifies an entry point

              expression The : pointcut expression is used to specify the associated  

Universal expression:. Execution (* * .. * * (..)

              <aop:pointcut expression=" execution(* com.bdqn.cn.dao.*.*(..))" id="唯一标识符"/>

              The pointcut expression refers to any method in a packet matches com.bdqn.cn.dao

              * The first: that the return type, use * to represent all types

              com.bdqn.cn.dao : expressed the need to intercept the package name

              * Indicates a second behind the class name

              The third representation name *

              Parameter later (..) is a method

              ".." : denotes any parameter

<AOP: before> : pre-configured notification, <aop: aspect> child element, the method of pre-notification attribute specified method, pointcut-ref attribute associated with the specified entry point

<AOP: After-returning> : Returns the notification opposite configuration, <aop: aspect> child elements, the method attribute specifies rear notification method, pointcut-ref attribute associated with the specified entry point

<AOP: around> : configuration around advice, <aop: aspect> child element, the method indicates that the specified attribute surround notification method specified pointcut-ref attribute associated with a pointcut

<AOP: After the throwing-> : Configuration abnormality notification, <aop: aspect> child element, the method indicates that the specified attribute abnormality notification method specified pointcut-ref attribute associated with an entry point, when no abnormality occurs, will not be executed

<AOP: After> : Configure a final notification, <aop: aspect> child elements, the method specified final method notification attribute, the attribute associated with the specified pointcut-ref pointcut

 

AspectJ need to import the package:

 

2 , annotated version of AspectJ use

       @Aspect used to define a section

       @Pointcut used to define the entry point. When using the method needs to define a parameter that contains the name and signature of any name to represent the entry point, in fact, this method signature on an empty common method returns a value of void, and the method is

       @Before used to configure pre-notification, in use, the need to develop a value property value, the property value is used to specify a pointcut expression

       @AfterReturning : used to define the post-notification

       @Around for configuring notifications surround

       @AfterThrowing used to configure the exception notification

       @After used to configure the final notice

Example code:

       package com.bdqn.cn.aspect;

 

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

import org.springframework.stereotype.Component;

 

 

// cut class 

@Aspect

@Component

public class MyAspect {

      

       // define the starting point

       @Pointcut("execution(* com.bdqn.cn.dao.UserDaoImpl.*(..))")

       private void myPointcut(){}

      

       // Pre-notification

       @Before("myPointcut()")

       public void myBefore(JoinPoint joinPoint){

              System.out.println ( " Before advice: Make a check action ...");

              System.out.println ( " target class:" + joinPoint.getTarget ());

              System.out.println ( " implanted target method:" + joinPoint.getSignature () getName ( ).);

       }

      

       // post notice

       @AfterReturning("myPointcut()")

       public void myAfterReturning(JoinPoint joinPoint){

              System.out.println ( " Rear notice: Analog Logging ...");

              System.out.println ( " implanted target method:" + joinPoint.getSignature () getName ( ).);

       }

      

       /*

        *   Around advice

        * ProceedingJoinPoint is sub-interface JoinPoint, showing the target method can be performed

        * 1 , must return type is Object

        * 2 , must receive a parameter of type ProceedingJoinPoint

        *          3、必须throws Throwable

        *

        */

       @Around("myPointcut()")

       public Object myArround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{

              // start

              System.out.println ( " Surround begin: Before performing the target method to simulate open affairs ...");

              // execution of the current target method

              Object obj = proceedingJoinPoint.proceed();

              // End

              System.out.println ( " around the end: After executing the target method to simulate close the transaction ...");

             

              return obj;

       }

      

       // exception notification

       @AfterThrowing(value="myPointcut()",throwing="e")

       public void myAfterThrowing(JoinPoint joinPoint,Throwable e){

              System.out.println ( " exception notification: wrong" + e.getMessage ());

       }

      

       // final notice

       @After("myPointcut()")

       public void myAfter(){

              System.out.println ( " Final Notice: the release of resources after the end of the simulation method ...");

       }

      

      

}

 

Profiles

                     Implementing Classes:

                            package com.bdqn.cn.dao;

 

import org.springframework.stereotype.Repository;

 

@Repository("userDao")

public class UserDaoImpl implements UserDao {

 

       @Override

       public void addUser() {

              // TODO Auto-generated method stub

              System.out.println ( "Add User");

       }

 

       @Override

       public void deleteUser() {

              // TODO Auto-generated method stub

              System.out.println ( "delete user");

       }

 

}

Test categories:

                     @Test

       public void method(){

             

              ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");

              UserDao ud = (UserDao)app.getBean("userDao");

              ud.addUser ();

              ud.deleteUser ();

      

       }

Published 40 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/sj_1993/article/details/105250004
Recommended