88import org .springframework .beans .BeansException ;
99import org .springframework .beans .factory .DisposableBean ;
1010import org .springframework .beans .factory .SmartInitializingSingleton ;
11+ import org .springframework .beans .factory .config .BeanDefinition ;
1112import org .springframework .context .ApplicationContext ;
1213import org .springframework .context .ApplicationContextAware ;
13- import org .springframework .context .annotation . Lazy ;
14+ import org .springframework .context .support . GenericApplicationContext ;
1415import org .springframework .core .MethodIntrospector ;
1516import org .springframework .core .annotation .AnnotatedElementUtils ;
1617
@@ -33,9 +34,6 @@ public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationC
3334 @ Override
3435 public void afterSingletonsInstantiated () {
3536
36- // init JobHandler Repository
37- /*initJobHandlerRepository(applicationContext);*/
38-
3937 // init JobHandler Repository (for method)
4038 initJobHandlerMethodRepository (applicationContext );
4139
@@ -56,29 +54,11 @@ public void destroy() {
5654 super .destroy ();
5755 }
5856
59-
60- /*private void initJobHandlerRepository(ApplicationContext applicationContext) {
61- if (applicationContext == null) {
62- return;
63- }
64-
65- // init job handler action
66- Map<String, Object> serviceBeanMap = applicationContext.getBeansWithAnnotation(JobHandler.class);
67-
68- if (serviceBeanMap != null && serviceBeanMap.size() > 0) {
69- for (Object serviceBean : serviceBeanMap.values()) {
70- if (serviceBean instanceof IJobHandler) {
71- String name = serviceBean.getClass().getAnnotation(JobHandler.class).value();
72- IJobHandler handler = (IJobHandler) serviceBean;
73- if (loadJobHandler(name) != null) {
74- throw new RuntimeException("xxl-job jobhandler[" + name + "] naming conflicts.");
75- }
76- registJobHandler(name, handler);
77- }
78- }
79- }
80- }*/
81-
57+ /**
58+ * init job handler from method
59+ *
60+ * @param applicationContext applicationContext
61+ */
8262 private void initJobHandlerMethodRepository (ApplicationContext applicationContext ) {
8363 if (applicationContext == null ) {
8464 return ;
@@ -87,12 +67,28 @@ private void initJobHandlerMethodRepository(ApplicationContext applicationContex
8767 String [] beanDefinitionNames = applicationContext .getBeanNamesForType (Object .class , false , false ); // allowEagerInit=false, avoid early initialization
8868 for (String beanDefinitionName : beanDefinitionNames ) {
8969
90- // filter system bean
70+ // skip system bean
9171 if (isSystemBean (beanDefinitionName )) {
9272 continue ;
9373 }
9474
95- // get bean
75+ // skip lazy bean
76+ if (applicationContext instanceof GenericApplicationContext genericApplicationContext ) {
77+ if (!genericApplicationContext .containsBeanDefinition (beanDefinitionName )) {
78+ continue ;
79+ }
80+ // valid lazy bean
81+ BeanDefinition beanDefinition = genericApplicationContext .getBeanDefinition (beanDefinitionName );
82+ if (beanDefinition .isLazyInit ()) {
83+ logger .debug ("xxl-job bean-definition scan, skip lazy-init bean:{}" , beanDefinitionName );
84+ continue ;
85+ }
86+ }
87+
88+ // load bean
89+ Object bean = applicationContext .getBean (beanDefinitionName );
90+ /*
91+ skip lazy bean2
9692 Object bean = null;
9793 Lazy onBean = applicationContext.findAnnotationOnBean(beanDefinitionName, Lazy.class);
9894 if (onBean!=null){
@@ -101,6 +97,7 @@ private void initJobHandlerMethodRepository(ApplicationContext applicationContex
10197 }else {
10298 bean = applicationContext.getBean(beanDefinitionName);
10399 }
100+ */
104101
105102 // filter method
106103 Map <Method , XxlJob > annotatedMethods = null ; // referred to :org.springframework.context.event.EventListenerMethodProcessor.processBean
@@ -149,13 +146,4 @@ public static ApplicationContext getApplicationContext() {
149146 return applicationContext ;
150147 }
151148
152- /*
153- BeanDefinitionRegistryPostProcessor
154- registry.getBeanDefine()
155- @Override
156- public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
157- this.registry = registry;
158- }
159- * */
160-
161149}
0 commit comments