Skip to content

Commit

Permalink
Service annotation #parameters on issue#3072 (#8002)
Browse files Browse the repository at this point in the history
fixes #3072
  • Loading branch information
pinxiong committed Jun 8, 2021
1 parent c5ed7de commit fc1bdfa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Expand Up @@ -57,6 +57,8 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.HashMap;

import static com.alibaba.dubbo.config.spring.util.ObjectUtils.of;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;
Expand Down Expand Up @@ -370,14 +372,16 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class
MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();

String[] ignoreAttributeNames = of("provider", "monitor", "application", "module", "registry", "protocol",
"interface", "interfaceName");
"interface", "interfaceName", "parameters");

propertyValues.addPropertyValues(new AnnotationPropertyValuesAdapter(service, environment, ignoreAttributeNames));

// References "ref" property to annotated-@Service Bean
addPropertyReference(builder, "ref", annotatedServiceBeanName);
// Set interface
builder.addPropertyValue("interface", interfaceClass.getName());
// Set parameters
builder.addPropertyValue("parameters",convertParameters(service.parameters()));

/**
* Add {@link com.alibaba.dubbo.config.ProviderConfig} Bean reference
Expand Down Expand Up @@ -469,6 +473,26 @@ private void addPropertyReference(BeanDefinitionBuilder builder, String property
builder.addPropertyReference(propertyName, resolvedBeanName);
}

/**
* Converts the string array parameters to map.
* @param parameters the parameters to convert.
* @return the converted parameters as a map.
*/
private Map<String, String> convertParameters(String[] parameters) {
if (parameters == null || parameters.length == 0) {
return null;
}

if (parameters.length % 2 != 0) {
throw new IllegalArgumentException("parameter attribute must be paired with key followed by value");
}

Map<String, String> map = new HashMap<String,String>();
for (int i = 0; i < parameters.length; i += 2) {
map.put(parameters[i], parameters[i + 1]);
}
return map;
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/
package com.alibaba.dubbo.config.spring.beans.factory.annotation;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.config.spring.ServiceBean;
import com.alibaba.dubbo.config.spring.api.HelloService;

Expand Down Expand Up @@ -79,4 +80,20 @@ public void test() {

}

/**
* Test if the {@link Service#parameters()} works well
* see issue: https://github.com/apache/dubbo/issues/3072
*/
@Test
public void testDubboServiceParameter() {
/**
* get the {@link ServiceBean} of {@link com.alibaba.dubbo.config.spring.context.annotation.provider.DefaultHelloService}
* */
ServiceBean serviceBean = beanFactory.getBean("ServiceBean:com.alibaba.dubbo.config.spring.api.HelloService", ServiceBean.class);
Assert.assertNotNull(serviceBean);
Assert.assertNotNull(serviceBean.getParameters());
Assert.assertTrue(serviceBean.getParameters().size() == 1);
Assert.assertEquals(serviceBean.toUrl().getParameter("sayHello.timeout"), "3000");
}

}
Expand Up @@ -27,7 +27,7 @@
* @since TODO
*/
@Service
@com.alibaba.dubbo.config.annotation.Service
@com.alibaba.dubbo.config.annotation.Service(parameters = {"sayHello.timeout", "3000"})
public class DefaultHelloService implements HelloService {

@Override
Expand Down

0 comments on commit fc1bdfa

Please sign in to comment.