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

Fixed Service annotation method parameters are not in effect #4598

Merged
merged 5 commits into from
Jul 31, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
*/
package org.apache.dubbo.config.spring.beans.factory.annotation;

import java.util.Collections;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.config.MethodConfig;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.config.spring.ServiceBean;
import org.apache.dubbo.config.spring.context.annotation.DubboClassPathBeanDefinitionScanner;
Expand Down Expand Up @@ -387,6 +390,11 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Annotation serviceAnno
builder.addPropertyValue("interface", interfaceClass.getName());
// Convert parameters into map
builder.addPropertyValue("parameters", convertParameters(serviceAnnotationAttributes.getStringArray("parameters")));
// Add methods parameters
List<MethodConfig> methodConfigs = convertMethodConfigs(serviceAnnotationAttributes.get("methods"));
if (!methodConfigs.isEmpty()) {
builder.addPropertyValue("methods", methodConfigs);
}

/**
* Add {@link org.apache.dubbo.config.ProviderConfig} Bean reference
Expand Down Expand Up @@ -447,6 +455,12 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Annotation serviceAnno

}

private List convertMethodConfigs(Object methodsAnnotation) {
if (methodsAnnotation == null){
return Collections.EMPTY_LIST;
}
return MethodConfig.constructMethodConfig((Method[])methodsAnnotation);
}

Leishunyu marked this conversation as resolved.
Show resolved Hide resolved
private ManagedList<RuntimeBeanReference> toRuntimeBeanReferences(String... beanNames) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,21 +343,9 @@ public static Map<String, Object> getAttributes(Annotation annotation, PropertyR
if (ignoreDefaultValue && nullSafeEquals(attributeValue, getDefaultValue(annotation, attributeName))) {
continue;
}

/**
* @since 2.7.1
* ignore annotation member
*/
if (attributeValue.getClass().isAnnotation()) {
continue;
}
if (attributeValue.getClass().isArray() && attributeValue.getClass().getComponentType().isAnnotation()) {
continue;
}
actualAttributes.put(attributeName, attributeValue);
}


return resolvePlaceholders(actualAttributes, propertyResolver, ignoreAttributeNames);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.config.spring.beans.factory.annotation;

import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.spring.ReferenceBean;
import org.apache.dubbo.config.spring.api.DemoService;
Expand Down Expand Up @@ -47,14 +48,14 @@
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(
classes = {
ServiceAnnotationTestConfiguration.class,
ReferenceAnnotationBeanPostProcessorTest.class
})
classes = {
ServiceAnnotationTestConfiguration.class,
ReferenceAnnotationBeanPostProcessorTest.class
})
@TestPropertySource(properties = {
"packagesToScan = org.apache.dubbo.config.spring.context.annotation.provider",
"consumer.version = ${demo.service.version}",
"consumer.url = dubbo://127.0.0.1:12345?version=2.5.7",
"packagesToScan = org.apache.dubbo.config.spring.context.annotation.provider",
"consumer.version = ${demo.service.version}",
"consumer.url = dubbo://127.0.0.1:12345?version=2.5.7",
})
public class ReferenceAnnotationBeanPostProcessorTest {

Expand All @@ -79,7 +80,7 @@ public ReferenceAnnotationBeanPostProcessor referenceAnnotationBeanPostProcessor
@Qualifier("helloServiceImpl")
private HelloService helloServiceImpl;

@Reference(id = "helloService")
@Reference(id = "helloService", methods = @Method(name = "sayName", timeout = 100))
private HelloService helloService;

@Test
Expand Down Expand Up @@ -131,16 +132,14 @@ public void test() throws Exception {
public void testGetReferenceBeans() {

ReferenceAnnotationBeanPostProcessor beanPostProcessor = context.getBean(BEAN_NAME,
ReferenceAnnotationBeanPostProcessor.class);
ReferenceAnnotationBeanPostProcessor.class);

Collection<ReferenceBean<?>> referenceBeans = beanPostProcessor.getReferenceBeans();

Assert.assertEquals(2, referenceBeans.size());

ReferenceBean<?> referenceBean = referenceBeans.iterator().next();

TestBean testBean = context.getBean(TestBean.class);

Assert.assertNotNull(referenceBean.get());

}
Expand All @@ -149,11 +148,10 @@ public void testGetReferenceBeans() {
public void testGetInjectedFieldReferenceBeanMap() {

ReferenceAnnotationBeanPostProcessor beanPostProcessor = context.getBean(BEAN_NAME,
ReferenceAnnotationBeanPostProcessor.class);

ReferenceAnnotationBeanPostProcessor.class);

Map<InjectionMetadata.InjectedElement, ReferenceBean<?>> referenceBeanMap =
beanPostProcessor.getInjectedFieldReferenceBeanMap();
beanPostProcessor.getInjectedFieldReferenceBeanMap();

Assert.assertEquals(2, referenceBeanMap.size());

Expand All @@ -162,7 +160,7 @@ public void testGetInjectedFieldReferenceBeanMap() {
InjectionMetadata.InjectedElement injectedElement = entry.getKey();

Assert.assertEquals("org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor$AnnotatedFieldElement",
injectedElement.getClass().getName());
injectedElement.getClass().getName());

}

Expand All @@ -172,11 +170,10 @@ public void testGetInjectedFieldReferenceBeanMap() {
public void testGetInjectedMethodReferenceBeanMap() {

ReferenceAnnotationBeanPostProcessor beanPostProcessor = context.getBean(BEAN_NAME,
ReferenceAnnotationBeanPostProcessor.class);

ReferenceAnnotationBeanPostProcessor.class);

Map<InjectionMetadata.InjectedElement, ReferenceBean<?>> referenceBeanMap =
beanPostProcessor.getInjectedMethodReferenceBeanMap();
beanPostProcessor.getInjectedMethodReferenceBeanMap();

Assert.assertEquals(2, referenceBeanMap.size());

Expand All @@ -185,33 +182,32 @@ public void testGetInjectedMethodReferenceBeanMap() {
InjectionMetadata.InjectedElement injectedElement = entry.getKey();

Assert.assertEquals("org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor$AnnotatedMethodElement",
injectedElement.getClass().getName());
injectedElement.getClass().getName());

}

}

// @Test
// public void testModuleInfo() {
//
// ReferenceAnnotationBeanPostProcessor beanPostProcessor = context.getBean(BEAN_NAME,
// ReferenceAnnotationBeanPostProcessor.class);
//
//
// Map<InjectionMetadata.InjectedElement, ReferenceBean<?>> referenceBeanMap =
// beanPostProcessor.getInjectedMethodReferenceBeanMap();
//
// for (Map.Entry<InjectionMetadata.InjectedElement, ReferenceBean<?>> entry : referenceBeanMap.entrySet()) {
// ReferenceBean<?> referenceBean = entry.getValue();
//
// assertThat(referenceBean.getModule().getName(), is("defaultModule"));
// assertThat(referenceBean.getMonitor(), not(nullValue()));
// }
// }
// @Test
// public void testModuleInfo() {
//
// ReferenceAnnotationBeanPostProcessor beanPostProcessor = context.getBean(BEAN_NAME,
// ReferenceAnnotationBeanPostProcessor.class);
//
//
// Map<InjectionMetadata.InjectedElement, ReferenceBean<?>> referenceBeanMap =
// beanPostProcessor.getInjectedMethodReferenceBeanMap();
//
// for (Map.Entry<InjectionMetadata.InjectedElement, ReferenceBean<?>> entry : referenceBeanMap.entrySet()) {
// ReferenceBean<?> referenceBean = entry.getValue();
//
// assertThat(referenceBean.getModule().getName(), is("defaultModule"));
// assertThat(referenceBean.getMonitor(), not(nullValue()));
// }
// }

private static class AncestorBean {


private DemoService demoServiceFromAncestor;

@Autowired
Expand All @@ -232,7 +228,6 @@ public ApplicationContext getApplicationContext() {

}


private static class ParentBean extends AncestorBean {

@Reference(version = "${consumer.version}", url = "${consumer.url}")
Expand All @@ -242,7 +237,6 @@ public DemoService getDemoServiceFromParent() {
return demoServiceFromParent;
}


}

static class TestBean extends ParentBean {
Expand All @@ -265,4 +259,21 @@ public void setDemoService(DemoService demoService) {
}
}

@Test
public void testReferenceBeansMethodAnnotation() {

ReferenceAnnotationBeanPostProcessor beanPostProcessor = context.getBean(BEAN_NAME,
ReferenceAnnotationBeanPostProcessor.class);

Collection<ReferenceBean<?>> referenceBeans = beanPostProcessor.getReferenceBeans();

Assert.assertEquals(2, referenceBeans.size());

ReferenceBean<?> referenceBean = referenceBeans.iterator().next();

if ("helloService".equals(referenceBean.getId())) {
Assert.assertNotNull(referenceBean.getMethods());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.config.spring.beans.factory.annotation;

import org.apache.dubbo.config.spring.ServiceBean;
import org.apache.dubbo.config.spring.api.DemoService;
import org.apache.dubbo.config.spring.api.HelloService;

import org.junit.Assert;
Expand Down Expand Up @@ -79,4 +80,17 @@ public void test() {

}

@Test
public void testMethodAnnotation() {

Map<String, ServiceBean> serviceBeansMap = beanFactory.getBeansOfType(ServiceBean.class);

Assert.assertEquals(2, serviceBeansMap.size());

ServiceBean demoServiceBean = serviceBeansMap.get("ServiceBean:org.apache.dubbo.config.spring.api.DemoService:2.5.7");

Assert.assertNotNull(demoServiceBean.getMethods());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.config.spring.context.annotation.provider;

import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.spring.api.Box;
import org.apache.dubbo.config.spring.api.DemoService;

Expand All @@ -32,7 +33,8 @@
version = "2.5.7",
application = "${demo.service.application}",
protocol = "${demo.service.protocol}",
registry = "${demo.service.registry}"
registry = "${demo.service.registry}",
methods = @Method(timeout = 100,name = "sayName")
)
@Service
@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@

package org.apache.dubbo.demo.consumer.comp;

import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.demo.DemoService;

import org.springframework.stereotype.Component;

@Component("demoServiceComponent")
public class DemoServiceComponent implements DemoService {
@Reference
@Reference(timeout = 100,methods = @Method(timeout = 500,name = "sayHello"))
private DemoService demoService;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
#

dubbo.application.name=dubbo-demo-annotation-consumer
dubbo.registry.address=multicast://224.5.6.7:1234
dubbo.registry.address=zookeeper://127.0.0.1:2181
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static class ProviderConfiguration {
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("multicast://224.5.6.7:1234");
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
return registryConfig;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@
*/
package org.apache.dubbo.demo.provider;

import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.demo.DemoService;
import org.apache.dubbo.rpc.RpcContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
@Service(timeout = 100,methods = @Method(timeout = 1000,name = "sayHello"))
public class DemoServiceImpl implements DemoService {
private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);

@Override
public String sayHello(String name) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
Expand Down