Skip to content

Commit

Permalink
Fix lazyinit value transfer problem (#1317)
Browse files Browse the repository at this point in the history
* Fix lazyinit value transfer problem

Signed-off-by: Jermaine Hua <[email protected]>

* Add integration tests for lazyinit

Signed-off-by: Jermaine Hua <[email protected]>

---------

Signed-off-by: Jermaine Hua <[email protected]>
  • Loading branch information
CrazyHZM authored May 14, 2024
1 parent 1089e7b commit d65c12b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,7 +101,7 @@ public void testSofaReferenceOnSingleParameterMethod() {
Map<String, Object> 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();
Expand Down Expand Up @@ -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();
Expand All @@ -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<String, Object> 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<String, Object> 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 {
/**
Expand All @@ -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();
Expand Down

0 comments on commit d65c12b

Please sign in to comment.