Skip to content

Commit

Permalink
fix: @service#parameters on issue#3072
Browse files Browse the repository at this point in the history
1. Support @service#parameters
2. Add some testcases
  • Loading branch information
pinxiong committed Jun 7, 2021
1 parent 115cc7f commit 0f0657d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
Expand Up @@ -52,11 +52,7 @@
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

import static com.alibaba.dubbo.config.spring.util.ObjectUtils.of;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;
Expand Down Expand Up @@ -378,6 +374,8 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class
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 +467,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 0f0657d

Please sign in to comment.