Hello World

吞风吻雨葬落日 欺山赶海踏雪径

0%

Spring配置记录

spring配置记录

bean中配置map和list属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<bean id="myService" class="com.xxx.myServiceImpl" init-method="init">
<property name="processorRouter">
<map>
<entry key="scene">
<list>
<ref bean="sceneProcessor" />
</list>
</entry>
<entry key="market">
<list>
<ref bean="sceneProcessor" />
<ref bean="marketProcessor" />
</list>
</entry>
<entry key="shop">
<list>
<ref bean="sceneProcessor" />
<ref bean="marketProcessor" />
<ref bean="shopProcessor" />
</list>
</entry>
</map>
</property>
</bean>

在bean中定义

1
private Map<String, List<Processor>> processorRouter;

spring装配非spring管理的类

可以使用AutowireCapableBeanFactory
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/AutowireCapableBeanFactory.html

通过ApplicationContext.getAutowireCapableBeanFactory()方法获取。

使用例子

在dts代码中保证job实现类能够使用spring bean的代码如下
com.alibaba.dts.client.executor.job.factory.JobProcessorFactory#initSpringJobProcessor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/** 填充字段 */
Field[] fields = jobProcessor.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
fields[i].setAccessible(true);
String fieldName = fields[i].getName();
Object object = null;
try {
object = applicationContext.getBean(fieldName);
} catch (Throwable e) {
logger.warn("[JobProcessorFactory]: initSpringJobProcessor field not found"
+ ", jobProcessor:" + jobProcessorProperties[0].trim()
+ ", fieldName:" + fieldName);
}
if (object != null) {
try {
fields[i].set(jobProcessor, object);
} catch (Throwable e) {
logger.error("[JobProcessorFactory]: initSpringJobProcessor field set error"
+ ", jobProcessor:" + jobProcessorProperties[0].trim()
+ ", fieldName:" + fieldName
+ ", object:" + object, e);
continue;
}
logger.warn("[JobProcessorFactory]: initSpringJobProcessor set field"
+ ", jobProcessor:" + jobProcessorProperties[0].trim()
+ ", fieldName:" + fieldName);
}
}

可以先尝试使用AutowireCapableBeanFactory#autowireBean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
try {
/**
* 使用spring容器自动把依赖的bean注入到jobProcessor,多加了这句话
*/
applicationContext.getAutowireCapableBeanFactory().autowireBean(jobProcessor);
} catch (Exception ex) {
logger.warn("initing spring bean error");
/** 填充字段 */
Field[] fields = jobProcessor.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
fields[i].setAccessible(true);
String fieldName = fields[i].getName();
Object object = null;
try {
object = applicationContext.getBean(fieldName);
} catch (Throwable e) {
logger.warn("[JobProcessorFactory]: initSpringJobProcessor field not found"
+ ", jobProcessor:" + jobProcessorProperties[0].trim()
+ ", fieldName:" + fieldName);
}
if (object != null) {
try {
fields[i].set(jobProcessor, object);
} catch (Throwable e) {
logger.error("[JobProcessorFactory]: initSpringJobProcessor field set error"
+ ", jobProcessor:" + jobProcessorProperties[0].trim()
+ ", fieldName:" + fieldName
+ ", object:" + object, e);
continue;
}
logger.warn("[JobProcessorFactory]: initSpringJobProcessor set field"
+ ", jobProcessor:" + jobProcessorProperties[0].trim()
+ ", fieldName:" + fieldName);
}
}
}