Springboot Actuator The 12: actuator aop

Preface
spring aop is a core concept in how spring boot is to automate the configuration? Now we have to analyze

Analytical
spring boot automation configuration is configured to read the read value org.springframework.boot.autoconfigure.EnableAutoConfiguration /META-INF/spring.factories of which only about a aop, i.e. org.springframework.boot.autoconfigure.aop .AopAutoConfiguration. This class is the key to open the chest

AopAutoConfiguration declares the following comment:

@Configuration
@ConditionalOnClass ({EnableAspectJAutoProxy.class, Aspect.class, Advice.class})
@ConditionalOnProperty (prefix = "Spring.Aop", name = "Auto", havingValue = "to true", to true matchIfMissing =)
. 1
2
. 3
@ configuration -> configuration class
@ConditionalOnClass ({EnableAspectJAutoProxy.class, Aspect.class, Advice.class }) -> EnableAspectJAutoProxy.class presence in the current class path, Aspect.class, when this arrangement was only resolved Advice.class
@ConditionalOnProperty (prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true) -> take effect when configured with spring.aop.auto = true If not configured, it defaults to take effect.
AopAutoConfiguration only two internal classes:

JdkDynamicAutoProxyConfiguration, code is as follows:

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = false)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false", matchIfMissing = true)
public static class JdkDynamicAutoProxyConfiguration {

}
. 1
2
. 3
. 4
. 5
. 6
@Configuration -> configuration class
@EnableAspectJAutoProxy (proxyTargetClass = false) -> Open aop annotation, the annotation on, followed by parsing.
@ConditionalOnProperty (prefix = "Spring.Aop", name = "PROXY target-class ", havingValue =" false ", matchIfMissing = true) -> commencement when disposed spring.aop.proxy-target-class = false, if not configured by default to take effect.
CglibAutoProxyConfiguration, code is as follows:

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true", matchIfMissing = false)
public static class CglibAutoProxyConfiguration {

}
. 1
2
. 3
. 4
. 5
. 6
@Configuration -> configuration class
@EnableAspectJAutoProxy (proxyTargetClass = false) -> Open aop annotation
@ConditionalOnProperty (prefix = "spring.aop", name = "proxy-target-class", havingValue = "true ", matchIfMissing = false) -> If the current configuration spring.aop.proxy-target-class = true entry into force, if not configured, the default is not effective.
Therefore, we can know, aop default JdkDynamicAutoProxyConfiguration the entry into force of this on line with us. cognitive spring aop default JDK is used, if the proxy class for use, configure spring.aop.proxy-target-class = true, a proxy is used cglib

@EnableAspectJAutoProxy code is as follows:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {

// indicate whether based proxies, false -> using a proxy (java dynamic proxy) for the interface .true -> use CGLIB
Boolean the proxyTargetClass () to false default;

// indicates whether exposure to the agent (by way of ThreadLocal), this attribute is set to true self-resolves target inside the object can not call the enhanced embodiment cut in question.
Boolean the exposeProxy () default to false;
}
. 1
2
. 3
. 4
. 5
. 6
7
8
9
10
11
12
risk solutions calls itself self when calling on target inside the object will not be implemented enhanced section in question can watch Spring transaction processing and some implementations of this blog

Here by @Import (AspectJAutoProxyRegistrar.class), introduced AspectJAutoProxyRegistrar configuration.

Or seen from previous articles, this time will be called ConfigurationClassParser # processImports. Since AspectJAutoProxyRegistrar realized ImportBeanDefinitionRegistrar interface, it will be added to ConfigurationClass of importBeanDefinitionRegistrars JdkDynamicAutoProxyConfiguration the corresponding.

Finally, in order to call its methods registerBeanDefinitions ConfigurationClassBeanDefinitionReader # loadBeanDefinitionsFromRegistrars will importBeanDefinitionRegistrars.

AspectJAutoProxyRegistrar # registerBeanDefinitions code is as follows:

public void registerBeanDefinitions(
AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);

AnnotationAttributes enableAspectJAutoProxy =
AnnotationConfigUtils.attributesFor(importingClassMetadata, EnableAspectJAutoProxy.class);
if (enableAspectJAutoProxy.getBoolean("proxyTargetClass")) {
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
}
if (enableAspectJAutoProxy.getBoolean("exposeProxy")) {
AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Registration id for the org.springframework.aop.config.internalAutoProxyCreator, class is AnnotationAwareAspectJAutoProxyCreator here through the layers of bean called final call AopConfigUtils # registerOrEscalateApcAsRequired method, the code is as follows:

static BeanDefinition registerOrEscalateApcAsRequired Private (<?> Class CLS, BeanDefinitionRegistry Registry, Object Source) {
Assert.notNull (Registry, "BeanDefinitionRegistry not the MUST BE null");
// If you already have an automatic proxy creator and the presence of auto proxy creator Situation so inconsistent with the needs in the end is determined according to priority which requires the use of
IF (registry.containsBeanDefinition (AUTO_PROXY_CREATOR_BEAN_NAME)) {
the BeanDefinition apcDefinition = registry.getBeanDefinition (AUTO_PROXY_CREATOR_BEAN_NAME);
!. IF (cls.getName () the equals (apcDefinition.getBeanClassName ())) {
int = currentPriority findPriorityForClass (apcDefinition.getBeanClassName ());
int = requiredPriority findPriorityForClass (CLS);
IF (currentPriority <requiredPriority) {
// the most important change is to change the bean bean corresponding ClassName
apcDefinition.setBeanClassName (cls.getName ());
}
}
// If there is already an automatic proxy creator and consistent to be created, you need to create once again
return null;
}
RootBeanDefinition BeanDefinition = new new RootBeanDefinition (CLS);
beanDefinition.setSource ( Source);
. beanDefinition.getPropertyValues () the Add ( "Order", Ordered.HIGHEST_PRECEDENCE);
beanDefinition.setRole (BeanDefinition.ROLE_INFRASTRUCTURE);
registry.registerBeanDefinition (AUTO_PROXY_CREATOR_BEAN_NAME, BeanDefinition);
return BeanDefinition;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18
19
20
21
22
23
where incoming cls to AnnotationAwareAspectJAutoProxyCreator.class
If you already have an automatic proxy creator and the auto proxy creator is inconsistent with the status quo existing in the end you need to determine which one to use based on priority -> If this high when an incoming priority than AnnotationAwareAspectJAutoProxyCreator registered priority is replaced AnnotationAwareAspectJAutoProxyCreator. then direct return
if there is already an automatic proxy creator and consistent with that will be created (AnnotationAwareAspectJAutoProxyCreator), then the need to create re-
register, id is org.springframework.aop.config.internalAutoProxyCreator, class is AnnotationAwareAspectJAutoProxyCreator, priority Integer.MIN_VALUE. roles for internal use
if configured proxyTargetClass equal to true, the id of org.springframework.aop.config.internalAutoProxyCreator to add proxyTargetClass of the BeanDefinition attribute value of true code is as follows:

static void forceAutoProxyCreatorToUseClassProxying public (the BeanDefinitionRegistry Registry) {
IF (registry.containsBeanDefinition (AUTO_PROXY_CREATOR_BEAN_NAME)) {
the BeanDefinition Definition = registry.getBeanDefinition (AUTO_PROXY_CREATOR_BEAN_NAME);
. definition.getPropertyValues () the Add ( "the proxyTargetClass", of Boolean.TRUE);
}
}
. 1
2
. 3
. 4
. 5
. 6
If configured exposeProxy equal to true, the id is on the org.springframework.aop.config.internalAutoProxyCreator BeanDefinition added exposeProxy attribute value is true code is as follows:

static void forceAutoProxyCreatorToExposeProxy public (the BeanDefinitionRegistry Registry) {
IF (registry.containsBeanDefinition (AUTO_PROXY_CREATOR_BEAN_NAME)) {
the BeanDefinition Definition = registry.getBeanDefinition (AUTO_PROXY_CREATOR_BEAN_NAME);
. definition.getPropertyValues () the Add ( "the exposeProxy", of Boolean.TRUE);
}
}
. 1
2
3
4
5
6
about the source aop aspect, prepared one by one in the spring in depth Secret, aop automated configuration on spring boot in to resolve here.
--------------- ------
Disclaimer: "a farm yard effort" to CSDN article blogger's original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_26000415/article/details/79010971

Guess you like

Origin www.cnblogs.com/duanxz/p/11328712.html