Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: @Service#parameters on issue#3072 #8002

Merged
merged 1 commit into from Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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