use of org.apache.dubbo.config.spring.ReferenceBean in project dubbo by alibaba.
the class ReferenceAnnotationBeanPostProcessorTest method testGetReferenceBeans.
/**
* Test on {@link ReferenceAnnotationBeanPostProcessor#getReferenceBeans()}
*/
@Test
public void testGetReferenceBeans() {
ReferenceAnnotationBeanPostProcessor beanPostProcessor = context.getBean(BEAN_NAME, ReferenceAnnotationBeanPostProcessor.class);
Collection<ReferenceBean<?>> referenceBeans = beanPostProcessor.getReferenceBeans();
Assertions.assertEquals(8, referenceBeans.size());
ReferenceBean<?> referenceBean = referenceBeans.iterator().next();
Assertions.assertNotNull(ReferenceConfigCache.getCache().get(referenceBean));
ReferenceBean helloService2Bean = getReferenceBean("helloService2", referenceBeans);
Assertions.assertEquals("demo_tag", helloService2Bean.getTag());
}
use of org.apache.dubbo.config.spring.ReferenceBean in project dubbo by alibaba.
the class ReferenceAnnotationBeanPostProcessor method doGetInjectedBean.
@Override
protected Object doGetInjectedBean(AnnotationAttributes attributes, Object bean, String beanName, Class<?> injectedType, InjectionMetadata.InjectedElement injectedElement) throws Exception {
/**
* The name of bean that annotated Dubbo's {@link Service @Service} in local Spring {@link ApplicationContext}
*/
String referencedBeanName = buildReferencedBeanName(attributes, injectedType);
/**
* The name of bean that is declared by {@link Reference @Reference} annotation injection
*/
String referenceBeanName = getReferenceBeanName(attributes, injectedType);
referencedBeanNameIdx.computeIfAbsent(referencedBeanName, k -> new TreeSet<String>()).add(referenceBeanName);
ReferenceBean referenceBean = buildReferenceBeanIfAbsent(referenceBeanName, attributes, injectedType);
boolean localServiceBean = isLocalServiceBean(referencedBeanName, referenceBean, attributes);
prepareReferenceBean(referencedBeanName, referenceBean, localServiceBean);
registerReferenceBean(referencedBeanName, referenceBean, localServiceBean, referenceBeanName);
cacheInjectedReferenceBean(referenceBean, injectedElement);
return getBeanFactory().applyBeanPostProcessorsAfterInitialization(referenceBean.get(), referenceBeanName);
}
use of org.apache.dubbo.config.spring.ReferenceBean in project dubbo by alibaba.
the class AnnotationPropertyValuesAdapterTest method test.
@Test
public void test() {
MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setProperty("version", "1.0.0");
mockEnvironment.setProperty("url", " dubbo://localhost:12345");
Field field = ReflectionUtils.findField(TestBean.class, "demoService");
Reference reference = AnnotationUtils.getAnnotation(field, Reference.class);
AnnotationPropertyValuesAdapter propertyValues = new AnnotationPropertyValuesAdapter(reference, mockEnvironment);
ReferenceBean referenceBean = new ReferenceBean();
DataBinder dataBinder = new DataBinder(referenceBean);
dataBinder.setDisallowedFields("application", "module", "consumer", "monitor", "registry");
DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new Converter<String[], String>() {
@Override
public String convert(String[] source) {
return arrayToCommaDelimitedString(source);
}
});
conversionService.addConverter(new Converter<String[], Map<String, String>>() {
@Override
public Map<String, String> convert(String[] source) {
return CollectionUtils.toStringMap(source);
}
});
dataBinder.setConversionService(conversionService);
dataBinder.bind(propertyValues);
// System.out.println(referenceBean);
Assertions.assertEquals(DemoService.class, referenceBean.getInterfaceClass());
Assertions.assertEquals("org.apache.dubbo.config.spring.api.DemoService", referenceBean.getInterface());
Assertions.assertEquals("1.0.0", referenceBean.getVersion());
Assertions.assertEquals("group", referenceBean.getGroup());
Assertions.assertEquals("dubbo://localhost:12345", referenceBean.getUrl());
Assertions.assertEquals("client", referenceBean.getClient());
Assertions.assertEquals(true, referenceBean.isGeneric());
Assertions.assertNull(referenceBean.isInjvm());
Assertions.assertEquals(false, referenceBean.isCheck());
Assertions.assertEquals(true, referenceBean.isInit());
Assertions.assertEquals(true, referenceBean.getLazy());
Assertions.assertEquals(true, referenceBean.getStubevent());
Assertions.assertEquals("reconnect", referenceBean.getReconnect());
Assertions.assertEquals(true, referenceBean.getSticky());
Assertions.assertEquals("javassist", referenceBean.getProxy());
Assertions.assertEquals("stub", referenceBean.getStub());
Assertions.assertEquals("failover", referenceBean.getCluster());
Assertions.assertEquals(Integer.valueOf(1), referenceBean.getConnections());
Assertions.assertEquals(Integer.valueOf(1), referenceBean.getCallbacks());
Assertions.assertEquals("onconnect", referenceBean.getOnconnect());
Assertions.assertEquals("ondisconnect", referenceBean.getOndisconnect());
Assertions.assertEquals("owner", referenceBean.getOwner());
Assertions.assertEquals("layer", referenceBean.getLayer());
Assertions.assertEquals(Integer.valueOf(1), referenceBean.getRetries());
Assertions.assertEquals("random", referenceBean.getLoadbalance());
Assertions.assertEquals(true, referenceBean.isAsync());
Assertions.assertEquals(Integer.valueOf(1), referenceBean.getActives());
Assertions.assertEquals(true, referenceBean.getSent());
Assertions.assertEquals("mock", referenceBean.getMock());
Assertions.assertEquals("validation", referenceBean.getValidation());
Assertions.assertEquals(Integer.valueOf(2), referenceBean.getTimeout());
Assertions.assertEquals("cache", referenceBean.getCache());
Assertions.assertEquals("default,default", referenceBean.getFilter());
Assertions.assertEquals("default,default", referenceBean.getListener());
Map<String, String> data = new LinkedHashMap<String, String>();
data.put("key1", "value1");
Assertions.assertEquals(data, referenceBean.getParameters());
// Bean compare
Assertions.assertNull(referenceBean.getRegistry());
}
use of org.apache.dubbo.config.spring.ReferenceBean in project dubbo by alibaba.
the class ReferenceBeanBuilderTest method testBuild.
@Test
public void testBuild() throws Exception {
DubboReference reference = findAnnotation(findField(getClass(), "TEST_FIELD"), DubboReference.class);
AnnotationAttributes attributes = AnnotationUtils.getAnnotationAttributes(reference, false, false);
ReferenceBeanBuilder beanBuilder = ReferenceBeanBuilder.create(attributes, context);
beanBuilder.interfaceClass(CharSequence.class);
ReferenceBean referenceBean = beanBuilder.build();
Assertions.assertEquals(CharSequence.class, referenceBean.getInterfaceClass());
Assertions.assertEquals("1.0.0", referenceBean.getVersion());
Assertions.assertEquals("TEST_GROUP", referenceBean.getGroup());
Assertions.assertEquals("dubbo://localhost:12345", referenceBean.getUrl());
Assertions.assertEquals("client", referenceBean.getClient());
Assertions.assertEquals(true, referenceBean.isGeneric());
Assertions.assertTrue(referenceBean.isInjvm());
Assertions.assertEquals(false, referenceBean.isCheck());
Assertions.assertFalse(referenceBean.isInit());
Assertions.assertEquals(true, referenceBean.getLazy());
Assertions.assertEquals(true, referenceBean.getStubevent());
Assertions.assertEquals("reconnect", referenceBean.getReconnect());
Assertions.assertEquals(true, referenceBean.getSticky());
Assertions.assertEquals("javassist", referenceBean.getProxy());
Assertions.assertEquals("java.lang.CharSequence", referenceBean.getStub());
Assertions.assertEquals("failover", referenceBean.getCluster());
Assertions.assertEquals(Integer.valueOf(3), referenceBean.getConnections());
Assertions.assertEquals(Integer.valueOf(1), referenceBean.getCallbacks());
Assertions.assertEquals("onconnect", referenceBean.getOnconnect());
Assertions.assertEquals("ondisconnect", referenceBean.getOndisconnect());
Assertions.assertEquals("owner", referenceBean.getOwner());
Assertions.assertEquals("layer", referenceBean.getLayer());
Assertions.assertEquals(Integer.valueOf(1), referenceBean.getRetries());
Assertions.assertEquals("random", referenceBean.getLoadbalance());
Assertions.assertEquals(true, referenceBean.isAsync());
Assertions.assertEquals(Integer.valueOf(3), referenceBean.getActives());
Assertions.assertEquals(true, referenceBean.getSent());
Assertions.assertEquals("mock", referenceBean.getMock());
Assertions.assertEquals("validation", referenceBean.getValidation());
Assertions.assertEquals(Integer.valueOf(3), referenceBean.getTimeout());
Assertions.assertEquals("cache", referenceBean.getCache());
Assertions.assertEquals("echo,generic,accesslog", referenceBean.getFilter());
Assertions.assertEquals("deprecated", referenceBean.getListener());
Assertions.assertEquals("reference", referenceBean.getId());
Assertions.assertEquals(ofSet("service1", "service2", "service3"), referenceBean.getSubscribedServices());
Assertions.assertEquals("service1,service2,service3", referenceBean.getProvidedBy());
Assertions.assertEquals("fastjson", referenceBean.getSerialization());
// parameters
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("n1", "v1");
parameters.put("n2", "v2");
parameters.put("n3", "v3");
Assertions.assertEquals(parameters, referenceBean.getParameters());
// Asserts Null fields
Assertions.assertThrows(IllegalStateException.class, () -> referenceBean.getApplication());
Assertions.assertNull(referenceBean.getModule());
Assertions.assertNull(referenceBean.getConsumer());
Assertions.assertNull(referenceBean.getMonitor());
Assertions.assertEquals(Collections.emptyList(), referenceBean.getRegistries());
}
Aggregations