AOP configuration based on xml file

Keywords: Java Attribute Spring xml encoding

AOP configuration based on xml file

  • The notification class object is delivered to Spring for management, that is, the notification class object is configured

    <! -- 1. Configure the Logger object (here is the notification class) -- >
    <bean id="logger" class="com.mypro.utils.Logger"></bean>
  • Use the aop:config tab to start the AOP configuration

    <aop:config></aop:config>
  • Using the aop:aspect tag inside the aop:config tag indicates the configuration aspect

    • id attribute: provides a unique id for the facet

    • ref attribute: Specifies the id of the Bean of the notification class

    <aop:config>
        <aop:aspect id="loggerAdvice" ref="logger"></aop:aspect>
    </aop:config>
  • Use the corresponding label inside the aop:aspect label to configure the notification type

    • Pre notice label: aop:before

    • Post notification label: AOP: after returning

    • Exception notification label: AOP: after throwing

    • Final notice label: aop:after

    • Common properties of the above notice labels

      • Method property: the method used to specify the notification class

      • Pointcut ref property: id used to specify the unique identity of the configured pointcut expression

      • Pointcut property: used to specify a pointcut expression, which means to enhance methods in the business layer

        • The standard format of pointcut expression: execution (access modifier return value type package name. Package name.. class name. Method name (parameter list)) is only for one method

        • All in one notation:

          * *..*.*(..)
        • The evolution from the standard format to the all-in-one collocation

          • Access modifiers can be omitted

          • The return value can be replaced by the wildcard * to indicate any return value*

          • The package name can also be replaced by wildcard characters. If there are several packages, there will be several **

          • The package name can also be simplified:... Indicates the current package and all subpackages *

          • The class name can also be replaced by the wildcard * to represent any class **

          • Method names can also be replaced by wildcards to represent any method *.. **

          • parameter list

            • Can be a basic data type example int

            • It can be reference type to write package name. Example of class name java.lang.String

            • You can use wildcard. To represent any data type, but you must have the parameter *.. * (.)

            • You can add another wildcard. It indicates whether there is a parameter *.. *. * (..)

          • You can add wildcards as needed

    • Configure pointcut expressions

      • Label: aop:pointcut

      • Attribute: id unique identification, expression configured expression

      • classification

        • Place the aop:pointcut label anywhere inside the aop:aspect label, only for the current section

        • The aop:pointcut label can only be placed above the outside of the first aop:aspect label. Any aspect can be used

      <aop:pointcut id="pt1" expression="execution(* com.mypro.service.impl.*.*(..))"></aop:point>

      Full AOP profile

      <bean id="logger" class="com.mypro.service.impl.WordsServiceImpl"></bean>
      <aop:config>
          <aop:aspect id="loggerAdvice" ref="logger">
              <aop:pointcut id="pt1" expression="execution(* com.mypro.service.impl.*.*(..))"></aop:point>
              <aop:before method="beforeInfo" pointcut-ref="pt1"></aop:before>
              <aop:after-returning method="afterReturningInfo" pointcut-ref="pt1"></aop:after-returning>
              <aop:after-throwing method="afterThrowingInfo" pointcut-ref="pt1"></aop:after-throwing>
              <aop:after method="afterInfo" pointcut-ref="pt1"></aop:after>
          </aop:aspect>
      </aop:config>
    • Orbit notification label: aop:around. Properties are the same as the properties of other notification labels, and the function of surround notification is to complete the notification labels in the above-mentioned 4 by encoding. You only need to configure this surround notification label in the xml file, and the encoding is implemented in the notification class

      <bean id="logger" class="com.mypro.service.impl.WordsServiceImpl"></bean>
      <aop:config>
          <aop:aspect id="loggerAdvice" ref="logger">
              <aop:pointcut id="pt1" expression="execution(* com.mypro.service.impl.*.*(..))"></aop:pointcut>
              <aop:around method="aroundInfo" poinitcut-ref="pt1"></aop:around>
          </aop:aspect>
      </aop:config>

      Then, in the notification class (in this case, the Logger class), the aroundInfo method writes functions similar to the above four notifications.

      Analysis: through the surround notification code in the dynamic agent, it is found that the surround notification of the dynamic agent has a clear pointcut method call, but our method does not

      Solution: the Spring framework provides us with an interface, proceed ingjoinpoint, which has a procedure method, which is equivalent to explicitly calling the pointcut method. The interface can be used as a method parameter around the notification. When the program is executed, the Spring framework will provide us with the implementation class call of the interface

      import org.aspectj.lang.ProceedingJoinPoint;
      public class Logger{
          public Object aroundInfo(){
              Object rtValue = null;
              try{
                  System.out.println("Before advice");
                  // Get the parameters needed for method execution
                  Object[] args = pjp.getArgs();
                  // Explicitly call business layer methods (pointcut methods)
                  rtValue = pjp.proceed(args);
                  System.out.println("Post notification");
                  return rtValue;
              }catch(Throwable t){      // It must be used. Throwable Catching anomalies
                  System.out.println("Exception notification");
                  throw new RuntimeException(t);
              }finally{
                  System.out.println("Final notice");
              }
          }
      }

       

Posted by Griffin on Fri, 17 Apr 2020 22:43:49 -0700