From d65c12beec443af5d9e6ef78578d5461b29a09ee Mon Sep 17 00:00:00 2001 From: Jermaine Hua Date: Tue, 14 May 2024 10:32:51 +0800 Subject: [PATCH] Fix lazyinit value transfer problem (#1317) * Fix lazyinit value transfer problem Signed-off-by: Jermaine Hua * Add integration tests for lazyinit Signed-off-by: Jermaine Hua --------- Signed-off-by: Jermaine Hua --- .../ServiceBeanFactoryPostProcessor.java | 9 ++- .../test/SofaServiceAndReferenceTest.java | 60 ++++++++++++++++++- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ServiceBeanFactoryPostProcessor.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ServiceBeanFactoryPostProcessor.java index 8522e39bc..8774eb6f6 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ServiceBeanFactoryPostProcessor.java +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/spring/ServiceBeanFactoryPostProcessor.java @@ -49,6 +49,7 @@ import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.GenericBeanDefinition; @@ -247,7 +248,9 @@ private void doGenerateSofaReferenceDefinition(BeanDefinition beanDefinition, if (!registry.containsBeanDefinition(referenceId)) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(); builder.getRawBeanDefinition().setScope(beanDefinition.getScope()); - builder.getRawBeanDefinition().setLazyInit(beanDefinition.isLazyInit()); + if (((AbstractBeanDefinition) beanDefinition).getLazyInit() != null) { + builder.getRawBeanDefinition().setLazyInit(beanDefinition.isLazyInit()); + } builder.getRawBeanDefinition().setBeanClass(ReferenceFactoryBean.class); builder.addAutowiredProperty(AbstractContractDefinitionParser.SOFA_RUNTIME_CONTEXT); builder @@ -314,7 +317,9 @@ private void generateSofaServiceDefinition(String beanId, SofaService sofaServic if (!registry.containsBeanDefinition(serviceId)) { builder.getRawBeanDefinition().setScope(beanDefinition.getScope()); - builder.setLazyInit(beanDefinition.isLazyInit()); + if (((AbstractBeanDefinition) beanDefinition).getLazyInit() != null) { + builder.setLazyInit(beanDefinition.isLazyInit()); + } builder.getRawBeanDefinition().setBeanClass(ServiceFactoryBean.class); builder.addAutowiredProperty(AbstractContractDefinitionParser.SOFA_RUNTIME_CONTEXT); builder diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/SofaServiceAndReferenceTest.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/SofaServiceAndReferenceTest.java index 043fa98b7..f2d05323c 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/SofaServiceAndReferenceTest.java +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/test/SofaServiceAndReferenceTest.java @@ -29,6 +29,7 @@ import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -100,7 +101,7 @@ public void testSofaReferenceOnSingleParameterMethod() { Map properties = new HashMap<>(); properties.put("spring.application.name", "SofaServiceAndReferenceTest"); SpringApplication springApplication = new SpringApplication( - TestSofaReferenceOnMethodConfiguration.class, RuntimeConfiguration.class); + TestSofaServiceOnMethodConfiguration.class, RuntimeConfiguration.class); springApplication.setWebApplicationType(WebApplicationType.NONE); springApplication.setDefaultProperties(properties); ApplicationContext ctx = springApplication.run(); @@ -173,7 +174,7 @@ public void testSofaReferenceOnStaticField() throws IOException, NoSuchFieldExce properties.put("spring.application.name", "SofaServiceAndReferenceTest"); properties.put("logging.path", logRootPath); SpringApplication springApplication = new SpringApplication( - TestSofaReferenceOnMethodConfiguration.class, RuntimeConfiguration.class); + TestSofaServiceOnMethodConfiguration.class, RuntimeConfiguration.class); springApplication.setWebApplicationType(WebApplicationType.NONE); springApplication.setDefaultProperties(properties); springApplication.run(); @@ -186,6 +187,53 @@ public void testSofaReferenceOnStaticField() throws IOException, NoSuchFieldExce + SofaServiceAndReferenceTest.class.getDeclaredField("staticSampleService"))); } + @Test + public void testSofaServiceBeanLazyInit() throws IOException { + String logRootPath = StringUtils.hasText(System.getProperty("logging.path")) ? System + .getProperty("logging.path") : "./logs"; + File sofaLog = new File(logRootPath + File.separator + "sofa-runtime" + File.separator + + "sofa-default.log"); + FileUtils.write(sofaLog, "", System.getProperty("file.encoding")); + + Map properties = new HashMap<>(); + properties.put("spring.application.name", "SofaServiceAndReferenceTest"); + properties.put("logging.path", logRootPath); + SpringApplication springApplication = new SpringApplication( + TestSofaServiceConfiguration.class, RuntimeConfiguration.class); + springApplication.setLazyInitialization(true); + springApplication.setWebApplicationType(WebApplicationType.NONE); + springApplication.setDefaultProperties(properties); + ConfigurableApplicationContext context = springApplication.run(); + + String serviceBeanName = SofaBeanNameGenerator.generateSofaServiceBeanName( + SampleService.class, ""); + Assert.assertTrue(context.getBeanFactory().getBeanDefinition(serviceBeanName).isLazyInit()); + } + + @Test + public void testSofaReferenceBeanLazyInit() throws IOException { + String logRootPath = StringUtils.hasText(System.getProperty("logging.path")) ? System + .getProperty("logging.path") : "./logs"; + File sofaLog = new File(logRootPath + File.separator + "sofa-runtime" + File.separator + + "sofa-default.log"); + FileUtils.write(sofaLog, "", System.getProperty("file.encoding")); + + Map properties = new HashMap<>(); + properties.put("spring.application.name", "SofaServiceAndReferenceTest"); + properties.put("logging.path", logRootPath); + SpringApplication springApplication = new SpringApplication( + TestSofaReferenceOnMethodConfiguration.class, RuntimeConfiguration.class); + springApplication.setLazyInitialization(true); + springApplication.setWebApplicationType(WebApplicationType.NONE); + springApplication.setDefaultProperties(properties); + ConfigurableApplicationContext context = springApplication.run(); + + String referenceBeanName = SofaBeanNameGenerator.generateSofaReferenceBeanName( + SampleService.class, ""); + Assert.assertTrue(context.getBeanFactory().getBeanDefinition(referenceBeanName) + .isLazyInit()); + } + @Configuration(proxyBeanMethods = false) static class MultipleBindingsSofaServiceConfiguration { /** @@ -209,6 +257,14 @@ public SampleService sampleService(@SofaReference(uniqueId = "rpc", binding = @S @Configuration(proxyBeanMethods = false) static class TestSofaReferenceOnMethodConfiguration { + @Bean + public SampleService sampleService(@SofaReference SampleService sampleService) { + return new DefaultSampleService("TestSofaReferenceConfiguration"); + } + } + + @Configuration(proxyBeanMethods = false) + static class TestSofaServiceOnMethodConfiguration { @Bean public SofaServiceAndReferenceTest sofaServiceAndReferenceTest() { return new SofaServiceAndReferenceTest();