Search in sources :

Example 1 with ReferenceFactoryBean

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#"));
        }
    });
}
Also used : ServiceFactoryBean(com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean) ReferenceFactoryBean(com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with 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;
}
Also used : ServiceFactoryBean(com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean) ReferenceFactoryBean(com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean) BeanStat(com.alipay.sofa.boot.startup.BeanStat)

Example 3 with ReferenceFactoryBean

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#"));
        }
    });
}
Also used : ServiceFactoryBean(com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean) ReferenceFactoryBean(com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with 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;
}
Also used : ServiceFactoryBean(com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean) ReferenceFactoryBean(com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean) BeanStat(com.alipay.sofa.boot.startup.BeanStat)

Aggregations

ReferenceFactoryBean (com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean)4 ServiceFactoryBean (com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean)4 BeanStat (com.alipay.sofa.boot.startup.BeanStat)2 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2