Search in sources :

Example 1 with ServiceFactoryBean

use of com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean 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 ServiceFactoryBean

use of com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean in project sofa-boot by sofastack.

the class SofaServiceAndReferenceTest method testSofaServiceWithMultipleBindings.

@Test
public void testSofaServiceWithMultipleBindings() {
    Map<String, Object> properties = new HashMap<>();
    properties.put("spring.application.name", "SofaServiceAndReferenceTest");
    SpringApplication springApplication = new SpringApplication(MultipleBindingsSofaServiceConfiguration.class, RuntimeConfiguration.class);
    springApplication.setWebApplicationType(WebApplicationType.NONE);
    springApplication.setDefaultProperties(properties);
    ApplicationContext ctx = springApplication.run();
    ServiceFactoryBean bean = ctx.getBean(ServiceFactoryBean.class);
    Assert.assertEquals(2, bean.getBindings().size());
    Assert.assertEquals(new BindingType("jvm"), bean.getBindings().get(0).getBindingType());
    Assert.assertEquals(new BindingType("jvm"), bean.getBindings().get(1).getBindingType());
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) HashMap(java.util.HashMap) SpringApplication(org.springframework.boot.SpringApplication) BindingType(com.alipay.sofa.runtime.api.binding.BindingType) ServiceFactoryBean(com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean) Test(org.junit.Test)

Example 3 with ServiceFactoryBean

use of com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean 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 4 with ServiceFactoryBean

use of com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean 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 5 with ServiceFactoryBean

use of com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean in project sofa-boot by alipay.

the class SofaServiceAndReferenceTest method testSofaServiceWithMultipleBindings.

@Test
public void testSofaServiceWithMultipleBindings() {
    Map<String, Object> properties = new HashMap<>();
    properties.put("spring.application.name", "SofaServiceAndReferenceTest");
    SpringApplication springApplication = new SpringApplication(MultipleBindingsSofaServiceConfiguration.class, RuntimeConfiguration.class);
    springApplication.setWebApplicationType(WebApplicationType.NONE);
    springApplication.setDefaultProperties(properties);
    ApplicationContext ctx = springApplication.run();
    ServiceFactoryBean bean = ctx.getBean(ServiceFactoryBean.class);
    Assert.assertEquals(2, bean.getBindings().size());
    Assert.assertEquals(new BindingType("jvm"), bean.getBindings().get(0).getBindingType());
    Assert.assertEquals(new BindingType("jvm"), bean.getBindings().get(1).getBindingType());
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) HashMap(java.util.HashMap) SpringApplication(org.springframework.boot.SpringApplication) BindingType(com.alipay.sofa.runtime.api.binding.BindingType) ServiceFactoryBean(com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean) Test(org.junit.Test)

Aggregations

ServiceFactoryBean (com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean)6 ReferenceFactoryBean (com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean)4 Test (org.junit.Test)4 BeanStat (com.alipay.sofa.boot.startup.BeanStat)2 BindingType (com.alipay.sofa.runtime.api.binding.BindingType)2 HashMap (java.util.HashMap)2 SpringApplication (org.springframework.boot.SpringApplication)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 ApplicationContext (org.springframework.context.ApplicationContext)2