AOP XML-based configuration

First, the XML-based configuration AOP

       1. Suppose you create a need to enhance AccountService (wherein each method will perform a method of adding logging), and then create a log of the class that implements the method of logging;

public class AccountServiceImpl implements IAccountService {

    public void saveAccount() {
        System.out.println ( "a save" );
    }

    public void updateAccount(int i) {
        System.out.println ( "perform an Update" + I);
    }

    public int deleteAccount() {
        System.out.println ( "delete" );
         return 0 ;
    }
public class Logger {
    /**
     * Pre-notification
     */
    public void beforePrintLog(){
        System.out.println ( "pre-notification method beforePrintLog Logger class begins logging ..." );
    }

    /**
     * After returning advice
     */
    public void afterReturningPrintLog(){
        System.out.println ( "post-notification method afterReturningPrintLog Logger class begins logging ..." );
    }

    /**
     * Exception notification
     */
    public void afterThrowingPrintLog(){
        System.out.println ( "abnormality notification method afterThrowingPrintLog Logger class begins logging ..." );
    }

    /**
     * Final notice
     */

    public void afterPrintLog(){
        System.out.println ( "final method notification afterPrintLog Logger class begins logging ..." );
    }
    /**
     * Around advice
     *problem:
     * When we configured around advice, the entry point method is not performed, but the implementation of the notification method
     *analysis:
     * Around advice dynamic proxies have a clear entry point method call, and we did not
     *solve:
     * Spring provides an interface: ProceedingJoinPoint, proceed to the interface () method is equivalent to an explicit call to the entry point method
     * The interface may surround notified as a parameter, during program execution, spring provided in the framework interface implementation class for us to use for our
     * Sprig of around advice
     * Can be enhanced when the manual control method performed in the code
     */
    public Object aroundPrintLog(ProceedingJoinPoint pjp){
        Object rtValue=null;
        try {
            Object[] args=pjp.getArgs();
            System.out.println ( "Method aroundPrintLog Logger class begins logging front ..." );
            rtValue = pjp.proceed (args); // explicitly invoke business tier method (Method pointcut) 
            System.out.println ( "Method aroundPrintLog Logger class ... rear start logging" );
             return rtValue;
        } catch (Throwable t) {
            System.out.println ( "Method aroundPrintLog Logger class begins logging exception ..." );
             the throw  new new a RuntimeException (T);
        }finally {
            System.out.println ( "Method aroundPrintLog Logger class begins final logging ..." );
        }
    }
}
 
 

 

 
 
 

 

 

 

       The first step : introducing a dependency:              

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <!--解析切入点表达式-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>

    </dependencies>

       Step Two : Profile

         The spring configuration IOC, introduced into the introduction configuration object;

                Specific configuration: https://www.cnblogs.com/cqyp/p/12498972.html

          Configuring spring-based AOP configuration:

 

<-! Spring AOP xml-based configuration steps of
        1. notification Bean managed to spring
        2. Use aop: config configuration label indicates the beginning of AOP
        3. Use aop: aspect label indicates that the configuration section
                id: section is to give a unique identification
                ref: refers to the notice of the bean class Id
        4. aop: aspect internal label used to configure the type of notification corresponding tag
               Our current example is to printLog performed before you execute the entry point method: it is a pre-notification
               aop: before: shows an arrangement before advice
                    method: Logger class is used to specify which of the pre-notification method
                    pointcut: pointcut expression specifies the meaning of the entry point of service is a method of expression enhancement layer
                        Pointcut expressions wording:
                             Keywords: execution (expression)
                             expression:
                                  Access modifier return value package name. Package name. Package name ... the class name. Method name (parameter list)
                             Standard written expression:
                                   public void com.li.service.impl.AccountServiceImpl.saveAccount()
                             Access modifier can be omitted
                                  void com.li.service.impl.AccountServiceImpl.saveAccount()
                              The return value can be used to represent any return value wildcards
                                  * com.li.service.impl.AccountServiceImpl.saveAccount()
                              Package name can use wildcards, represents any package, the package has a few levels, you need to write a few *, can also be used .. represent the current package and its subpackages
                                  * *..AccountServiceImpl.saveAccount()
                              Class and method names can be achieved using the * wildcard
                                  * *..*.*()
                               Parameter List: use a wildcard to represent any type of parameters, but the parameters must have
                               It can be used with or without parameters can represent ..
                             Full wildcard wording
                             * *..*.*(..)
                      The actual wording of the entry point for the development of the expression:
                           All cutting method in the business layer implementation class
                               * com.li.service.impl.*.*(..)
    -> 
    <! - Configuration Logger class -> 
    < the bean ID = "Logger" class = "com.li.utils.Logger" > </ the bean > 
    <-! Opening AOP configuration -> 
    < AOP: config > 
! <- that uniquely identifies an entry point to configure expressions, id attribute is used to specify an expression, expression: expression content
      This label is written in the aop: aspect internal label can only use current section
      He can write aop: aspect outside, then it becomes common to all facets
  -->
<aop:pointcut id="pt1" expression="execution(* com.li.service.impl.*.*(..))"/>


        <! - Configuration cut -> 
        < : Aspect AOP ID = "logAdvice" REF = "Logger" > 
            <! - associated configure the notification type, and establishing the notification method and the starting point of the method -> 
<! - before home notification, executed before the entry point method

<aop:before method="beforePrintLog" pointcut-ref="pt1"/>
-> 
<! - after advice

<aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"/>
-> 
<! - exception notification

<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"/>
-> 
<! - Final Notice

<aop:after method="afterPrintLog" pointcut-ref="pt1"/>
-->
<!--配置环绕通知-->
<aop:around method="aroundPrintLog" pointcut-ref="pt1"></aop:around>
</aop:aspect> </aop:config>
 

 

            

 

Guess you like

Origin www.cnblogs.com/cqyp/p/12512854.html