|
17 | 17 | package com.alibaba.dubbo.config.spring.beans.factory.annotation; |
18 | 18 |
|
19 | 19 | import com.alibaba.dubbo.common.utils.Assert; |
| 20 | +import com.alibaba.dubbo.config.AbstractConfig; |
20 | 21 | import com.alibaba.dubbo.config.spring.context.annotation.DubboConfigBindingRegistrar; |
21 | 22 | import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfigBinding; |
| 23 | +import com.alibaba.dubbo.config.spring.context.properties.DefaultDubboConfigBinder; |
| 24 | +import com.alibaba.dubbo.config.spring.context.properties.DubboConfigBinder; |
22 | 25 | import org.apache.commons.logging.Log; |
23 | 26 | import org.apache.commons.logging.LogFactory; |
24 | 27 | import org.springframework.beans.BeansException; |
25 | | -import org.springframework.beans.PropertyValues; |
| 28 | +import org.springframework.beans.factory.InitializingBean; |
26 | 29 | import org.springframework.beans.factory.config.BeanPostProcessor; |
27 | | -import org.springframework.validation.DataBinder; |
28 | | - |
29 | | -import java.util.Arrays; |
| 30 | +import org.springframework.context.ApplicationContext; |
| 31 | +import org.springframework.context.ApplicationContextAware; |
| 32 | +import org.springframework.core.env.Environment; |
30 | 33 |
|
31 | 34 | /** |
32 | 35 | * Dubbo Config Binding {@link BeanPostProcessor} |
|
35 | 38 | * @see DubboConfigBindingRegistrar |
36 | 39 | * @since 2.5.8 |
37 | 40 | */ |
38 | | -public class DubboConfigBindingBeanPostProcessor implements BeanPostProcessor { |
| 41 | + |
| 42 | +public class DubboConfigBindingBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware, InitializingBean { |
39 | 43 |
|
40 | 44 | private final Log log = LogFactory.getLog(getClass()); |
41 | 45 |
|
42 | 46 | /** |
43 | | - * Binding Bean Name |
| 47 | + * The prefix of Configuration Properties |
44 | 48 | */ |
45 | | - private final String beanName; |
| 49 | + private final String prefix; |
46 | 50 |
|
47 | 51 | /** |
48 | | - * Binding {@link PropertyValues} |
| 52 | + * Binding Bean Name |
49 | 53 | */ |
50 | | - private final PropertyValues propertyValues; |
| 54 | + private final String beanName; |
| 55 | + |
| 56 | + private DubboConfigBinder dubboConfigBinder; |
| 57 | + |
| 58 | + private ApplicationContext applicationContext; |
51 | 59 |
|
| 60 | + private boolean ignoreUnknownFields = true; |
| 61 | + |
| 62 | + private boolean ignoreInvalidFields = true; |
52 | 63 |
|
53 | 64 | /** |
54 | | - * @param beanName Binding Bean Name |
55 | | - * @param propertyValues {@link PropertyValues} |
| 65 | + * @param prefix the prefix of Configuration Properties |
| 66 | + * @param beanName the binding Bean Name |
56 | 67 | */ |
57 | | - public DubboConfigBindingBeanPostProcessor(String beanName, PropertyValues propertyValues) { |
| 68 | + public DubboConfigBindingBeanPostProcessor(String prefix, String beanName) { |
| 69 | + Assert.notNull(prefix, "The prefix of Configuration Properties must not be null"); |
58 | 70 | Assert.notNull(beanName, "The name of bean must not be null"); |
59 | | - Assert.notNull(propertyValues, "The PropertyValues of bean must not be null"); |
| 71 | + this.prefix = prefix; |
60 | 72 | this.beanName = beanName; |
61 | | - this.propertyValues = propertyValues; |
62 | 73 | } |
63 | 74 |
|
64 | 75 | @Override |
65 | 76 | public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
66 | 77 |
|
67 | | - if (beanName.equals(this.beanName)) { |
68 | | - DataBinder dataBinder = new DataBinder(bean); |
69 | | - // TODO ignore invalid fields by annotation attribute |
70 | | - dataBinder.setIgnoreInvalidFields(true); |
71 | | - dataBinder.bind(propertyValues); |
| 78 | + if (beanName.equals(this.beanName) && bean instanceof AbstractConfig) { |
| 79 | + |
| 80 | + AbstractConfig dubboConfig = (AbstractConfig) bean; |
| 81 | + |
| 82 | + dubboConfigBinder.bind(prefix, dubboConfig); |
| 83 | + |
72 | 84 | if (log.isInfoEnabled()) { |
73 | | - log.info("The properties of bean [name : " + beanName + "] have been binding by values : " |
74 | | - + Arrays.asList(propertyValues.getPropertyValues())); |
| 85 | + log.info("The properties of bean [name : " + beanName + "] have been binding by prefix of " + |
| 86 | + "configuration properties : " + prefix); |
75 | 87 | } |
76 | 88 | } |
77 | 89 |
|
78 | 90 | return bean; |
79 | 91 |
|
80 | 92 | } |
81 | 93 |
|
| 94 | + public boolean isIgnoreUnknownFields() { |
| 95 | + return ignoreUnknownFields; |
| 96 | + } |
| 97 | + |
| 98 | + public void setIgnoreUnknownFields(boolean ignoreUnknownFields) { |
| 99 | + this.ignoreUnknownFields = ignoreUnknownFields; |
| 100 | + } |
| 101 | + |
| 102 | + public boolean isIgnoreInvalidFields() { |
| 103 | + return ignoreInvalidFields; |
| 104 | + } |
| 105 | + |
| 106 | + public void setIgnoreInvalidFields(boolean ignoreInvalidFields) { |
| 107 | + this.ignoreInvalidFields = ignoreInvalidFields; |
| 108 | + } |
| 109 | + |
| 110 | + public DubboConfigBinder getDubboConfigBinder() { |
| 111 | + return dubboConfigBinder; |
| 112 | + } |
| 113 | + |
| 114 | + public void setDubboConfigBinder(DubboConfigBinder dubboConfigBinder) { |
| 115 | + this.dubboConfigBinder = dubboConfigBinder; |
| 116 | + } |
82 | 117 |
|
83 | 118 | @Override |
84 | 119 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
85 | 120 | return bean; |
86 | 121 | } |
87 | 122 |
|
| 123 | + @Override |
| 124 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 125 | + this.applicationContext = applicationContext; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void afterPropertiesSet() throws Exception { |
| 130 | + |
| 131 | + if (dubboConfigBinder == null) { |
| 132 | + try { |
| 133 | + dubboConfigBinder = applicationContext.getBean(DubboConfigBinder.class); |
| 134 | + } catch (BeansException ignored) { |
| 135 | + if (log.isDebugEnabled()) { |
| 136 | + log.debug("DubboConfigBinder Bean can't be found in ApplicationContext."); |
| 137 | + } |
| 138 | + // Use Default implementation |
| 139 | + dubboConfigBinder = createDubboConfigBinder(applicationContext.getEnvironment()); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + dubboConfigBinder.setIgnoreUnknownFields(ignoreUnknownFields); |
| 144 | + dubboConfigBinder.setIgnoreInvalidFields(ignoreInvalidFields); |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Create {@link DubboConfigBinder} instance. |
| 150 | + * |
| 151 | + * @param environment |
| 152 | + * @return {@link DefaultDubboConfigBinder} |
| 153 | + */ |
| 154 | + protected DubboConfigBinder createDubboConfigBinder(Environment environment) { |
| 155 | + DefaultDubboConfigBinder defaultDubboConfigBinder = new DefaultDubboConfigBinder(); |
| 156 | + defaultDubboConfigBinder.setEnvironment(environment); |
| 157 | + return defaultDubboConfigBinder; |
| 158 | + } |
| 159 | + |
88 | 160 | } |
0 commit comments