Skip to content

Commit a2a7706

Browse files
authored
Spring Framework / Spring Boot Enhancements (#1611)
* Manually merge pull request #1486, to make travis ci and codecov work after apache incubator transition. * Polish #1306 * Optimize imports * Optimize imports * Update DubboConfigBinder.java Remove invalid JavaDoc
1 parent b2c5cb7 commit a2a7706

28 files changed

+1258
-121
lines changed

dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/ReferenceBean.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.alibaba.dubbo.config.annotation.Reference;
2626
import com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory;
2727
import com.alibaba.dubbo.config.support.Parameter;
28-
2928
import org.springframework.beans.factory.BeanFactoryUtils;
3029
import org.springframework.beans.factory.DisposableBean;
3130
import org.springframework.beans.factory.FactoryBean;

dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/ServiceBean.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.alibaba.dubbo.config.ServiceConfig;
2626
import com.alibaba.dubbo.config.annotation.Service;
2727
import com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory;
28-
2928
import org.springframework.aop.support.AopUtils;
3029
import org.springframework.beans.factory.BeanFactoryUtils;
3130
import org.springframework.beans.factory.BeanNameAware;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alibaba.dubbo.config.spring.beans.factory.annotation;
18+
19+
import org.springframework.beans.MutablePropertyValues;
20+
import org.springframework.beans.PropertyValue;
21+
import org.springframework.beans.PropertyValues;
22+
import org.springframework.core.env.PropertyResolver;
23+
24+
import java.lang.annotation.Annotation;
25+
26+
import static com.alibaba.dubbo.config.spring.util.AnnotationUtils.getAttributes;
27+
28+
/**
29+
* {@link Annotation} {@link PropertyValues} Adapter
30+
*
31+
* @see Annotation
32+
* @see PropertyValues
33+
* @since 2.5.11
34+
*/
35+
class AnnotationPropertyValuesAdapter implements PropertyValues {
36+
37+
private final Annotation annotation;
38+
39+
private final PropertyResolver propertyResolver;
40+
41+
private final boolean ignoreDefaultValue;
42+
43+
private final PropertyValues delegate;
44+
45+
public AnnotationPropertyValuesAdapter(Annotation annotation, PropertyResolver propertyResolver, boolean ignoreDefaultValue, String... ignoreAttributeNames) {
46+
this.annotation = annotation;
47+
this.propertyResolver = propertyResolver;
48+
this.ignoreDefaultValue = ignoreDefaultValue;
49+
this.delegate = adapt(annotation, ignoreDefaultValue, ignoreAttributeNames);
50+
}
51+
52+
public AnnotationPropertyValuesAdapter(Annotation annotation, PropertyResolver propertyResolver, String... ignoreAttributeNames) {
53+
this(annotation, propertyResolver, true, ignoreAttributeNames);
54+
}
55+
56+
private PropertyValues adapt(Annotation annotation, boolean ignoreDefaultValue, String... ignoreAttributeNames) {
57+
return new MutablePropertyValues(getAttributes(annotation, propertyResolver, ignoreDefaultValue, ignoreAttributeNames));
58+
}
59+
60+
public Annotation getAnnotation() {
61+
return annotation;
62+
}
63+
64+
public boolean isIgnoreDefaultValue() {
65+
return ignoreDefaultValue;
66+
}
67+
68+
@Override
69+
public PropertyValue[] getPropertyValues() {
70+
return delegate.getPropertyValues();
71+
}
72+
73+
@Override
74+
public PropertyValue getPropertyValue(String propertyName) {
75+
return delegate.getPropertyValue(propertyName);
76+
}
77+
78+
@Override
79+
public PropertyValues changesSince(PropertyValues old) {
80+
return delegate.changesSince(old);
81+
}
82+
83+
@Override
84+
public boolean contains(String propertyName) {
85+
return delegate.contains(propertyName);
86+
}
87+
88+
@Override
89+
public boolean isEmpty() {
90+
return delegate.isEmpty();
91+
}
92+
}

dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessor.java

Lines changed: 93 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@
1717
package com.alibaba.dubbo.config.spring.beans.factory.annotation;
1818

1919
import com.alibaba.dubbo.common.utils.Assert;
20+
import com.alibaba.dubbo.config.AbstractConfig;
2021
import com.alibaba.dubbo.config.spring.context.annotation.DubboConfigBindingRegistrar;
2122
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;
2225
import org.apache.commons.logging.Log;
2326
import org.apache.commons.logging.LogFactory;
2427
import org.springframework.beans.BeansException;
25-
import org.springframework.beans.PropertyValues;
28+
import org.springframework.beans.factory.InitializingBean;
2629
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;
3033

3134
/**
3235
* Dubbo Config Binding {@link BeanPostProcessor}
@@ -35,54 +38,123 @@
3538
* @see DubboConfigBindingRegistrar
3639
* @since 2.5.8
3740
*/
38-
public class DubboConfigBindingBeanPostProcessor implements BeanPostProcessor {
41+
42+
public class DubboConfigBindingBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware, InitializingBean {
3943

4044
private final Log log = LogFactory.getLog(getClass());
4145

4246
/**
43-
* Binding Bean Name
47+
* The prefix of Configuration Properties
4448
*/
45-
private final String beanName;
49+
private final String prefix;
4650

4751
/**
48-
* Binding {@link PropertyValues}
52+
* Binding Bean Name
4953
*/
50-
private final PropertyValues propertyValues;
54+
private final String beanName;
55+
56+
private DubboConfigBinder dubboConfigBinder;
57+
58+
private ApplicationContext applicationContext;
5159

60+
private boolean ignoreUnknownFields = true;
61+
62+
private boolean ignoreInvalidFields = true;
5263

5364
/**
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
5667
*/
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");
5870
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;
6072
this.beanName = beanName;
61-
this.propertyValues = propertyValues;
6273
}
6374

6475
@Override
6576
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
6677

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+
7284
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);
7587
}
7688
}
7789

7890
return bean;
7991

8092
}
8193

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+
}
82117

83118
@Override
84119
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
85120
return bean;
86121
}
87122

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+
88160
}

0 commit comments

Comments
 (0)