Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lazyinit value transfer problem #1316

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
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 @@ -266,7 +267,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 @@ -331,7 +334,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
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.smoke.tests.runtime.spring;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.service.component.impl.ServiceImpl;
import com.alipay.sofa.runtime.spring.bean.SofaBeanNameGenerator;
import com.alipay.sofa.smoke.tests.runtime.RuntimeSofaBootApplication;
import com.alipay.sofa.smoke.tests.runtime.service.SampleService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Component;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Integration tests for {@link SofaService} and {@link SofaReference} Bean Lazy Initialization.
*
* @author Jermaine Hua
*/
@SpringBootTest(classes = RuntimeSofaBootApplication.class, properties = "spring.main.lazy-initialization=true")
@Import(SofaBeanLazyInitTests.ServiceBeanAnnotationConfiguration.class)
public class SofaBeanLazyInitTests {

@Autowired
private ApplicationContext applicationContext;

@Test
public void checkSofaServiceBeanDefinitionLazyInitAttribute() {
String beanName = SofaBeanNameGenerator.generateSofaServiceBeanName(SampleService.class,
"sampleServiceImpl");
assertThat(applicationContext.containsBean(beanName)).isTrue();
assertThat(applicationContext.getBean(beanName)).isInstanceOf(ServiceImpl.class);

assertThat(
((GenericApplicationContext) applicationContext).getBeanFactory()
.getBeanDefinition(beanName).isLazyInit()).isTrue();

}

@Test
public void checkSofaReferenceBeanDefinitionLazyInitAttribute() {
String beanName = SofaBeanNameGenerator.generateSofaReferenceBeanName(SampleService.class,
"");
assertThat(applicationContext.containsBean(beanName)).isTrue();
assertThat(
((GenericApplicationContext) applicationContext).getBeanFactory()
.getBeanDefinition(beanName).isLazyInit()).isTrue();

}

@Configuration
@Import({ SampleServiceImpl.class })
static class ServiceBeanAnnotationConfiguration {
@Bean
public SampleService methodSampleService(@SofaReference SampleService sampleServiceImpl) {
return () -> "methodSampleService";
}
}

@SofaService
@Component("sampleServiceImpl")
static class SampleServiceImpl implements SampleService {

@Override
public String service() {
return "sampleService";
}
}

}
Loading