瀏覽代碼

GLUE依赖注入逻辑优化,支持别名注入;from:shirokumacafe@github

xueli.xue 8 年之前
父節點
當前提交
4cf65ebdb2
共有 2 個文件被更改,包括 14 次插入2 次删除
  1. 1 0
      README.md
  2. 13 2
      xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java

+ 1 - 0
README.md 查看文件

@@ -709,6 +709,7 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
709 709
 - 5、升级数据库连接池c3p0版本;
710 710
 - 6、执行器log4j配置优化,去除无效属性;
711 711
 - 7、底层代码重构和逻辑优化以及CleanCode;
712
+- 8、GLUE依赖注入逻辑优化,支持别名注入;
712 713
 
713 714
 
714 715
 #### 规划中

+ 13 - 2
xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java 查看文件

@@ -8,6 +8,7 @@ import org.slf4j.Logger;
8 8
 import org.slf4j.LoggerFactory;
9 9
 import org.springframework.beans.BeansException;
10 10
 import org.springframework.beans.factory.annotation.Autowired;
11
+import org.springframework.beans.factory.annotation.Qualifier;
11 12
 import org.springframework.context.ApplicationContext;
12 13
 import org.springframework.context.ApplicationContextAware;
13 14
 import org.springframework.core.annotation.AnnotationUtils;
@@ -76,14 +77,24 @@ public class GlueFactory implements ApplicationContextAware {
76 77
 			// with bean-id, bean could be found by both @Resource and @Autowired, or bean could only be found by @Autowired
77 78
 			if (AnnotationUtils.getAnnotation(field, Resource.class) != null) {
78 79
 				try {
79
-					fieldBean = applicationContext.getBean(field.getName());
80
+					Resource resource = AnnotationUtils.getAnnotation(field, Resource.class);
81
+					if (resource.name()!=null && resource.name().length()>0){
82
+						fieldBean = applicationContext.getBean(resource.name());
83
+					} else {
84
+						fieldBean = applicationContext.getBean(field.getName());
85
+					}
80 86
 				} catch (Exception e) {
81 87
 				}
82 88
 				if (fieldBean==null ) {
83 89
 					fieldBean = applicationContext.getBean(field.getType());
84 90
 				}
85 91
 			} else if (AnnotationUtils.getAnnotation(field, Autowired.class) != null) {
86
-				fieldBean = applicationContext.getBean(field.getType());		
92
+				Qualifier qualifier = AnnotationUtils.getAnnotation(field, Qualifier.class);
93
+				if (qualifier!=null && qualifier.value()!=null && qualifier.value().length()>0) {
94
+					fieldBean = applicationContext.getBean(qualifier.value());
95
+				} else {
96
+					fieldBean = applicationContext.getBean(field.getType());
97
+				}
87 98
 			}
88 99
 			
89 100
 			if (fieldBean!=null) {