use of com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean in project sofa-boot by sofastack.
the class SofaFactoryBeanTest method testServiceFactoryBean.
@Test
public void testServiceFactoryBean() throws Exception {
ServiceFactoryBean serviceFactoryBean;
ReferenceFactoryBean referenceFactoryBean;
// Configuration
serviceFactoryBean = (ServiceFactoryBean) ctx.getBean("&" + SofaBeanNameGenerator.generateSofaServiceBeanName(SampleService.class, "serviceViaBeanMethod"));
Assert.assertTrue(serviceFactoryBean.isApiType());
serviceFactoryBean = (ServiceFactoryBean) ctx.getBean("&" + SofaBeanNameGenerator.generateSofaServiceBeanName(ServiceWithoutInterface.class, "serviceWithoutInterface"));
Assert.assertTrue(serviceFactoryBean.isApiType());
referenceFactoryBean = (ReferenceFactoryBean) ctx.getBean("&" + SofaBeanNameGenerator.generateSofaReferenceBeanName(SampleService.class, "serviceViaBeanMethod"));
Assert.assertTrue(referenceFactoryBean.isApiType());
Assert.assertEquals(serviceViaBeanMethod, referenceFactoryBean.getObject());
// xml
serviceFactoryBean = (ServiceFactoryBean) ctx.getBean("&" + SofaBeanNameGenerator.generateSofaServiceBeanName(SampleService.class, ""));
Assert.assertFalse(serviceFactoryBean.isApiType());
referenceFactoryBean = (ReferenceFactoryBean) ctx.getBean("&xmlServiceWithoutInterface");
Assert.assertFalse(referenceFactoryBean.isApiType());
Assert.assertEquals(serviceWithoutInterface, referenceFactoryBean.getObject());
ctx.getBeansOfType(ServiceFactoryBean.class).forEach((key, value) -> {
Assert.assertTrue(key.startsWith("&ServiceFactoryBean#"));
});
ctx.getBeansOfType(ReferenceFactoryBean.class).forEach((key, value) -> {
if (value.isApiType()) {
Assert.assertTrue(key.startsWith("&ReferenceFactoryBean#"));
}
});
}
use of com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean in project sofa-boot by alipay.
the class BeanLoadCostBeanFactory method createBean.
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException {
Stack<BeanStat> parentStack = parentStackThreadLocal.get();
BeanStat bs = new BeanStat();
bs.setName(beanName);
if (parentStack == null) {
parentStack = new Stack<>();
parentStackThreadLocal.set(parentStack);
}
if (!parentStack.empty()) {
parentStack.peek().addChild(bs);
}
parentStack.push(bs);
bs.startRefresh();
Object object = super.createBean(beanName, mbd, args);
bs.finishRefresh();
if (mbd.getBeanClassName() == null) {
bs.setBeanClassName("Factory (" + mbd.getFactoryBeanName() + ")");
} else {
if (mbd.getBeanClassName().contains("ExtensionPointFactoryBean") || mbd.getBeanClassName().contains("ExtensionFactoryBean")) {
bs.setExtensionProperty(object.toString());
}
if (object instanceof ServiceFactoryBean) {
bs.setBeanClassName(mbd.getBeanClassName() + " (" + ((ServiceFactoryBean) object).getBeanId() + ")");
bs.setInterfaceType(((ServiceFactoryBean) object).getInterfaceType());
} else if (object instanceof ReferenceFactoryBean) {
bs.setBeanClassName(mbd.getBeanClassName() + " (" + beanName + ")");
bs.setInterfaceType(((ReferenceFactoryBean) object).getInterfaceType());
} else {
bs.setBeanClassName(mbd.getBeanClassName() + " (" + beanName + ")");
if (beanName.contains(mbd.getBeanClassName())) {
bs.setBeanClassName(mbd.getBeanClassName());
}
}
}
if (beanStatExtension != null) {
beanStatExtension.customBeanStat(beanName, mbd, args, bs);
}
parentStack.pop();
if (parentStack.empty() && bs.getRefreshElapsedTime() > beanLoadCost) {
beanStats.add(bs);
}
return object;
}
use of com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean in project sofa-boot by alipay.
the class SofaFactoryBeanTest method testServiceFactoryBean.
@Test
public void testServiceFactoryBean() throws Exception {
ServiceFactoryBean serviceFactoryBean;
ReferenceFactoryBean referenceFactoryBean;
// Configuration
serviceFactoryBean = (ServiceFactoryBean) ctx.getBean("&" + SofaBeanNameGenerator.generateSofaServiceBeanName(SampleService.class, "serviceViaBeanMethod"));
Assert.assertTrue(serviceFactoryBean.isApiType());
serviceFactoryBean = (ServiceFactoryBean) ctx.getBean("&" + SofaBeanNameGenerator.generateSofaServiceBeanName(ServiceWithoutInterface.class, "serviceWithoutInterface"));
Assert.assertTrue(serviceFactoryBean.isApiType());
referenceFactoryBean = (ReferenceFactoryBean) ctx.getBean("&" + SofaBeanNameGenerator.generateSofaReferenceBeanName(SampleService.class, "serviceViaBeanMethod"));
Assert.assertTrue(referenceFactoryBean.isApiType());
Assert.assertEquals(serviceViaBeanMethod, referenceFactoryBean.getObject());
// xml
serviceFactoryBean = (ServiceFactoryBean) ctx.getBean("&" + SofaBeanNameGenerator.generateSofaServiceBeanName(SampleService.class, ""));
Assert.assertFalse(serviceFactoryBean.isApiType());
referenceFactoryBean = (ReferenceFactoryBean) ctx.getBean("&xmlServiceWithoutInterface");
Assert.assertFalse(referenceFactoryBean.isApiType());
Assert.assertEquals(serviceWithoutInterface, referenceFactoryBean.getObject());
ctx.getBeansOfType(ServiceFactoryBean.class).forEach((key, value) -> {
Assert.assertTrue(key.startsWith("&ServiceFactoryBean#"));
});
ctx.getBeansOfType(ReferenceFactoryBean.class).forEach((key, value) -> {
if (value.isApiType()) {
Assert.assertTrue(key.startsWith("&ReferenceFactoryBean#"));
}
});
}
use of com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean in project sofa-boot by sofastack.
the class BeanLoadCostBeanFactory method createBean.
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException {
Stack<BeanStat> parentStack = parentStackThreadLocal.get();
BeanStat bs = new BeanStat();
if (parentStack == null) {
parentStack = new Stack<>();
parentStackThreadLocal.set(parentStack);
}
if (!parentStack.empty()) {
parentStack.peek().addChild(bs);
}
parentStack.push(bs);
bs.startRefresh();
Object object = super.createBean(beanName, mbd, args);
bs.finishRefresh();
if (mbd.getBeanClassName() == null) {
bs.setBeanClassName("Factory (" + mbd.getFactoryBeanName() + ")");
} else {
if (mbd.getBeanClassName().contains("ExtensionPointFactoryBean") || mbd.getBeanClassName().contains("ExtensionFactoryBean")) {
bs.setExtensionProperty(object.toString());
}
if (object instanceof ServiceFactoryBean) {
bs.setBeanClassName(mbd.getBeanClassName() + " (" + ((ServiceFactoryBean) object).getBeanId() + ")");
bs.setInterfaceType(((ServiceFactoryBean) object).getInterfaceType());
} else if (object instanceof ReferenceFactoryBean) {
bs.setBeanClassName(mbd.getBeanClassName() + " (" + beanName + ")");
bs.setInterfaceType(((ReferenceFactoryBean) object).getInterfaceType());
} else {
bs.setBeanClassName(mbd.getBeanClassName() + " (" + beanName + ")");
if (beanName.contains(mbd.getBeanClassName())) {
bs.setBeanClassName(mbd.getBeanClassName());
}
}
}
if (beanStatExtension != null) {
beanStatExtension.customBeanStat(beanName, mbd, args, bs);
}
parentStack.pop();
if (parentStack.empty() && bs.getRefreshElapsedTime() > beanLoadCost) {
beanStats.add(bs);
}
return object;
}
Aggregations