-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yong.teng
committed
Nov 12, 2023
1 parent
523064a
commit 4c2215d
Showing
6 changed files
with
172 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,64 @@ | |
* | Author: Yong.Teng <[email protected]> | | ||
* | Copyright @ 2013-2023 Buession.com Inc. | | ||
* +-------------------------------------------------------------------------------------------------------+ | ||
*/package com.buession.canal.spring.annotation.factory;/** | ||
* | ||
* | ||
*/ | ||
package com.buession.canal.spring.annotation.factory; | ||
|
||
import com.buession.canal.annotation.CanalBinding; | ||
import com.buession.canal.annotation.CanalEventListener; | ||
import com.buession.canal.client.dispatcher.AbstractDispatcher; | ||
import com.buession.canal.client.dispatcher.Dispatcher; | ||
import com.buession.canal.core.listener.utils.EventListenerUtils; | ||
import org.springframework.aop.support.AopUtils; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.beans.factory.BeanFactoryAware; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.core.annotation.AnnotationUtils; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* @author Yong.Teng | ||
* @since 0.0.1 | ||
*/public class CanalBindingBeanPostProcessor { | ||
*/ | ||
public class CanalBindingBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware { | ||
|
||
private BeanFactory beanFactory; | ||
|
||
@Override | ||
public void setBeanFactory(@NonNull BeanFactory beanFactory) throws BeansException { | ||
this.beanFactory = beanFactory; | ||
} | ||
|
||
@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()); | ||
} | ||
|
||
return bean; | ||
} | ||
|
||
protected void detectBindingMethods(final Object bean, final Class<?> beanType, final AbstractDispatcher dispatcher, | ||
final String destination) { | ||
ReflectionUtils.doWithMethods(beanType, (method)->{ | ||
Method invocableMethod = AopUtils.selectInvocableMethod(method, beanType); | ||
CanalEventListener canalEventListener = AnnotationUtils.findAnnotation(invocableMethod, | ||
CanalEventListener.class); | ||
|
||
if(canalEventListener == null){ | ||
return; | ||
} | ||
|
||
String listenerName = EventListenerUtils.buildEventListenerName(destination, canalEventListener.schema(), | ||
canalEventListener.table(), canalEventListener.eventType()); | ||
dispatcher.getEventListenerRegistry().register(listenerName, bean, invocableMethod); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.