spring spring's source code analysis notes how @Aspect works?

AOP (Aspect Orient Programming), aspect-oriented programming, object-oriented programming is a complement of OOP. Object-oriented programming is to consider the structure of the program from the static point of view, dynamic Oriented Programming from the viewpoint of the program is running.

AOP bottom, is achieved using the dynamic proxy mode. Using two agents: JDK dynamic proxy, with the dynamic proxy CGLIB. JDK dynamic proxy is an interface-oriented, CGLIB either implement an interface, and can not implement the interface. (I do not know that the introduction of dynamic proxies can look on dynamic proxies)

Oriented Programming, the business logic is encapsulated into the cross-section, using the function of the implant AOP container section to the main business logic. CROSS-called service logic means: general, with the main service logic independent code, such as a security check, the transaction log and the like.
spring spring's source code analysis notes how @Aspect works?
@Aspect function definitions may be used cut point tangent point, we can use logical operators to review the cut point tangent point arithmetic to obtain a composite, in order to reuse the tangent point section, we can name the cut-off point for other the place had cited the definition of the tangent point. When a connection point of a plurality of matching contact point, consider the problem of weaving sequence, in addition, one important issue is how to further enhance the connection point information access context.

1、@Aspect

In the definition xml: <aop: aspectj-autoproxy / >,
which is defined in http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

- <xsd:element name="aspectj-autoproxy">
- <xsd:annotation>
- <xsd:documentation source="java:org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator">
- <![CDATA[ 
    Enables the use of the @AspectJ style of Spring AOP.

  ]]> 
  </xsd:documentation>
  </xsd:annotation>
- <xsd:complexType>
- <xsd:sequence>
- <xsd:element name="include" type="includeType" minOccurs="0" maxOccurs="unbounded">
- <xsd:annotation>
- <xsd:documentation>
- <![CDATA[ 
    Indicates that only @AspectJ beans with names matched by the (regex)
    pattern will be considered as defining aspects to use for Spring autoproxying.

  ]]> 
  </xsd:documentation>
  </xsd:annotation>
  </xsd:element>
  </xsd:sequence>
- <xsd:attribute name="proxy-target-class" type="xsd:boolean" default="false">
- <xsd:annotation>
- <xsd:documentation>
- <![CDATA[ 
    Are class-based (CGLIB) proxies to be created? By default, standard
    Java interface-based proxies are created.

  ]]> 
  </xsd:documentation>
  </xsd:annotation>
  </xsd:attribute>
- <xsd:attribute name="expose-proxy" type="xsd:boolean" default="false">
- <xsd:annotation>
- <xsd:documentation>
- <![CDATA[ 
    Indicate that the proxy should be exposed by the AOP framework as a
    ThreadLocal for retrieval via the AopContext class. Off by default,
    i.e. no guarantees that AopContext access will work.

  ]]> 
  </xsd:documentation>
  </xsd:annotation>
  </xsd:attribute>
  </xsd:complexType>
  </xsd:element>

1.1 Registration

org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator
inheritance are as follows:
spring spring's source code analysis notes how @Aspect works?
1.2 resolution process

AspectJAutoProxyBeanDefinitionParser.java#parse()方法

    public BeanDefinition parse(Element element, ParserContext parserContext) {
        AopNamespaceUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(parserContext, element);
        extendBeanDefinition(element, parserContext);
        return null;
    }

The registration process:

 public static void registerAspectJAnnotationAutoProxyCreatorIfNecessary(
            ParserContext parserContext, Element sourceElement) {

        BeanDefinition beanDefinition = AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(
                parserContext.getRegistry(), parserContext.extractSource(sourceElement));
        useClassProxyingIfNecessary(parserContext.getRegistry(), sourceElement);
        registerComponentIfNecessary(beanDefinition, parserContext);
    }

Call implementation class:

    public static BeanDefinition registerAspectJAnnotationAutoProxyCreatorIfNecessary(BeanDefinitionRegistry registry, Object source) {
        return registerOrEscalateApcAsRequired(AnnotationAwareAspectJAutoProxyCreator.class, registry, source);
    }

1.3 specific implementation class: AbstractAutoProxyCreator the postProcessAfterInitialization () method
spring spring's source code analysis notes how @Aspect works?
DefaultAopProxyFactory # createAopProxy () method

@Override
    public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
        if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
            Class<?> targetClass = config.getTargetClass();
            if (targetClass == null) {
                throw new AopConfigException("TargetSource cannot determine target class: " +
                        "Either an interface or a target is required for proxy creation.");
            }
            if (targetClass.isInterface()) {
                return new JdkDynamicAopProxy(config);
            }
            return new ObjenesisCglibAopProxy(config);
        }
        else {
            return new JdkDynamicAopProxy(config);
        }
    }

Default jdk own agency, there is a cglib way.

Guess you like

Origin blog.51cto.com/14295088/2404513