Skip to content

Commit

Permalink
【spring】注解扫描优化
Browse files Browse the repository at this point in the history
  • Loading branch information
yong.teng committed Nov 16, 2023
1 parent 94cb9ec commit afdd070
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 45 deletions.
4 changes: 2 additions & 2 deletions buession-canal-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<!-- start canal -->
<dependency>
<groupId>com.alibaba.otter</groupId>
<artifactId>canal.client</artifactId>
<artifactId>canal.protocol</artifactId>
<version>${alibaba.canal.version}</version>
<exclusions>
<exclusion>
Expand All @@ -89,7 +89,7 @@
</dependency>
<dependency>
<groupId>com.alibaba.otter</groupId>
<artifactId>canal.protocol</artifactId>
<artifactId>canal.client</artifactId>
<version>${alibaba.canal.version}</version>
<exclusions>
<exclusion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@

import com.buession.canal.annotation.CanalBinding;
import com.buession.core.validator.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.scope.ScopedProxyFactoryBean;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
Expand All @@ -47,7 +44,9 @@
import org.springframework.lang.NonNull;
import org.springframework.util.ClassUtils;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

/**
* {@link CanalBinding} 扫描器
Expand All @@ -62,7 +61,7 @@ class CanalBindingClassPathMapperScanner extends ClassPathBeanDefinitionScanner
*/
private boolean lazyInitialization;

private final static Logger logger = LoggerFactory.getLogger(CanalBindingClassPathMapperScanner.class);
private final Set<String> destinations = new HashSet<>();

/**
* 构造函数
Expand Down Expand Up @@ -97,76 +96,50 @@ protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
return annotationMetadata.isIndependent();
}

@Override
protected boolean checkCandidate(@NonNull String beanName, @NonNull BeanDefinition beanDefinition) {
if(super.checkCandidate(beanName, beanDefinition)){
return true;
}else{
if(logger.isDebugEnabled()){
logger.warn(
"Skipping CanalBindingFactoryBean with name '{}' and '{}' bindingType. Bean already defined with the same name!",
beanName, beanDefinition.getBeanClassName());
}
return false;
}
}

@Override
protected void registerBeanDefinition(@NonNull BeanDefinitionHolder definitionHolder,
@NonNull BeanDefinitionRegistry registry) {
super.registerBeanDefinition(definitionHolder, registry);

AbstractBeanDefinition beanDefinition = (AbstractBeanDefinition) definitionHolder.getBeanDefinition();

boolean scopedProxy = false;

if(ScopedProxyFactoryBean.class.getName().equals(beanDefinition.getBeanClassName())){
beanDefinition = (AbstractBeanDefinition) Optional
.ofNullable(((RootBeanDefinition) beanDefinition).getDecoratedDefinition())
.map(BeanDefinitionHolder::getBeanDefinition).orElseThrow(()->new IllegalStateException(
"The target bean definition of scoped proxy bean not found. Root bean definition[" +
definitionHolder + ']'));
scopedProxy = true;
}

processBeanDefinition(beanDefinition);

if(scopedProxy){
return;
}

if(beanDefinition.isSingleton() == false){
registerProxyBeanDefinitionHolder(definitionHolder, registry);
}
}

private void processBeanDefinition(final AbstractBeanDefinition beanDefinition) {
String beanClassName = beanDefinition.getBeanClassName();

CanalBinding canalBinding = AnnotationUtils.findAnnotation(ClassUtils.resolveClassName(beanClassName, null),
CanalBinding.class);
if(canalBinding == null){
return;
}

if(Validate.isBlank(canalBinding.destination())){
throw new IllegalStateException(
"Either 'destination' must be required in @CanalBinding for: " + beanClassName);
}

if(destinations.contains(canalBinding.destination())){
throw new IllegalStateException(
"The destination: " + canalBinding.destination() + " already exists in @CanalBinding for: " +
beanClassName);
}

beanDefinition.setLazyInit(lazyInitialization);
beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
beanDefinition.setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, beanClassName);
}

private void registerProxyBeanDefinitionHolder(final BeanDefinitionHolder definitionHolder,
final BeanDefinitionRegistry registry) {
final BeanDefinitionHolder proxyBeanDefinitionHolder = ScopedProxyUtils.createScopedProxy(definitionHolder,
registry, true);

if(registry.containsBeanDefinition(proxyBeanDefinitionHolder.getBeanName())){
registry.removeBeanDefinition(proxyBeanDefinitionHolder.getBeanName());
}
registry.registerBeanDefinition(proxyBeanDefinitionHolder.getBeanName(),
proxyBeanDefinitionHolder.getBeanDefinition());
destinations.add(canalBinding.destination());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void setBeanFactory(@NonNull BeanFactory beanFactory) throws BeansExcepti
@Override
public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException {
CanalBinding canalBinding = AnnotationUtils.findAnnotation(bean.getClass(), CanalBinding.class);

if(canalBinding != null){
Dispatcher dispatcher = beanFactory.getBean(Dispatcher.class);
detectBindingMethods(bean, bean.getClass(), (AbstractDispatcher) dispatcher, canalBinding.destination());
Expand All @@ -76,8 +76,8 @@ protected void detectBindingMethods(final Object bean, final Class<?> beanType,
return;
}

String listenerName = EventListenerUtils.buildEventListenerName(destination, canalEventListener.schema(),
canalEventListener.table(), canalEventListener.eventType());
String listenerName = EventListenerUtils.buildEventListenerName(destination,
canalEventListener.schema(), canalEventListener.table(), canalEventListener.eventType());
dispatcher.getEventListenerRegistry().register(listenerName, bean, invocableMethod);
});
}
Expand Down

0 comments on commit afdd070

Please sign in to comment.