diff --git a/bootstrap/src/main/java/com/alibaba/nacos/bootstrap/NacosBootstrap.java b/bootstrap/src/main/java/com/alibaba/nacos/bootstrap/NacosBootstrap.java index aed61090db3..a7f952a2365 100644 --- a/bootstrap/src/main/java/com/alibaba/nacos/bootstrap/NacosBootstrap.java +++ b/bootstrap/src/main/java/com/alibaba/nacos/bootstrap/NacosBootstrap.java @@ -27,6 +27,8 @@ import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.io.ClassPathResource; +import org.springframework.jmx.support.MBeanRegistrationSupport; +import org.springframework.jmx.support.RegistrationPolicy; /** * Nacos bootstrap class. @@ -38,6 +40,7 @@ public class NacosBootstrap { public static void main(String[] args) { ConfigurableApplicationContext coreContext = startCoreContext(args); + coreContext.getBean(MBeanRegistrationSupport.class).setRegistrationPolicy(RegistrationPolicy.IGNORE_EXISTING); String type = coreContext.getEnvironment() .getProperty(Constants.NACOS_DEPLOYMENT_TYPE, Constants.NACOS_DEPLOYMENT_TYPE_MERGED); if (Constants.NACOS_DEPLOYMENT_TYPE_MERGED.equals(type)) { diff --git a/console/src/main/java/com/alibaba/nacos/console/config/NacosConsoleBeanPostProcessorConfiguration.java b/console/src/main/java/com/alibaba/nacos/console/config/NacosConsoleBeanPostProcessorConfiguration.java new file mode 100644 index 00000000000..c79502289c3 --- /dev/null +++ b/console/src/main/java/com/alibaba/nacos/console/config/NacosConsoleBeanPostProcessorConfiguration.java @@ -0,0 +1,50 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.console.config; + +import com.alibaba.nacos.core.web.NacosWebBean; +import com.alibaba.nacos.sys.env.Constants; +import com.alibaba.nacos.sys.env.NacosDuplicateConfigurationBeanPostProcessor; +import com.alibaba.nacos.sys.env.NacosDuplicateSpringBeanPostProcessor; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Bean Post Processor Configuration for nacos console. + * + * @author xiweng.yy + */ +@Configuration +@NacosWebBean +@ConditionalOnProperty(value = Constants.NACOS_DUPLICATE_BEAN_ENHANCEMENT_ENABLED, havingValue = "true", matchIfMissing = true) +public class NacosConsoleBeanPostProcessorConfiguration { + + @Bean + public InstantiationAwareBeanPostProcessor nacosDuplicateSpringBeanPostProcessor( + ConfigurableApplicationContext context) { + return new NacosDuplicateSpringBeanPostProcessor(context); + } + + @Bean + public InstantiationAwareBeanPostProcessor nacosDuplicateConfigurationBeanPostProcessor( + ConfigurableApplicationContext context) { + return new NacosDuplicateConfigurationBeanPostProcessor(context); + } +} diff --git a/prometheus/src/main/java/com/alibaba/nacos/prometheus/conf/PrometheusSecurityConfiguration.java b/prometheus/src/main/java/com/alibaba/nacos/prometheus/conf/PrometheusSecurityConfiguration.java index f8b2d2bae38..2dd852737ff 100644 --- a/prometheus/src/main/java/com/alibaba/nacos/prometheus/conf/PrometheusSecurityConfiguration.java +++ b/prometheus/src/main/java/com/alibaba/nacos/prometheus/conf/PrometheusSecurityConfiguration.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.prometheus.conf; +import com.alibaba.nacos.core.web.NacosWebBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; @@ -31,6 +32,7 @@ * @author vividfish */ @Configuration +@NacosWebBean public class PrometheusSecurityConfiguration { @Bean diff --git a/server/src/main/java/com/alibaba/nacos/NacosServerWebApplication.java b/server/src/main/java/com/alibaba/nacos/NacosServerWebApplication.java index 3df2a917f53..2575f7ffa4b 100644 --- a/server/src/main/java/com/alibaba/nacos/NacosServerWebApplication.java +++ b/server/src/main/java/com/alibaba/nacos/NacosServerWebApplication.java @@ -18,6 +18,7 @@ import com.alibaba.nacos.server.NacosNormalBeanTypeFilter; import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; @@ -29,10 +30,11 @@ * * @author xiweng.yy */ -@SpringBootApplication +@SpringBootApplication() @ComponentScan(basePackages = "com.alibaba.nacos", excludeFilters = { @Filter(type = FilterType.REGEX, pattern = "com\\.alibaba\\.nacos\\.console.*"), - @Filter(type = FilterType.CUSTOM, classes = {NacosNormalBeanTypeFilter.class})}) + @Filter(type = FilterType.CUSTOM, classes = {NacosNormalBeanTypeFilter.class, + AutoConfigurationExcludeFilter.class})}) @PropertySource("classpath:nacos-server.properties") public class NacosServerWebApplication { diff --git a/server/src/main/java/com/alibaba/nacos/server/NacosWebBeanPostProcessorConfiguration.java b/server/src/main/java/com/alibaba/nacos/server/NacosWebBeanPostProcessorConfiguration.java new file mode 100644 index 00000000000..154fbe08fd3 --- /dev/null +++ b/server/src/main/java/com/alibaba/nacos/server/NacosWebBeanPostProcessorConfiguration.java @@ -0,0 +1,50 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.server; + +import com.alibaba.nacos.core.web.NacosWebBean; +import com.alibaba.nacos.sys.env.Constants; +import com.alibaba.nacos.sys.env.NacosDuplicateConfigurationBeanPostProcessor; +import com.alibaba.nacos.sys.env.NacosDuplicateSpringBeanPostProcessor; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Bean Post Processor Configuration for nacos web server. + * + * @author xiweng.yy + */ +@Configuration +@NacosWebBean +@ConditionalOnProperty(value = Constants.NACOS_DUPLICATE_BEAN_ENHANCEMENT_ENABLED, havingValue = "true", matchIfMissing = true) +public class NacosWebBeanPostProcessorConfiguration { + + @Bean + public InstantiationAwareBeanPostProcessor nacosDuplicateSpringBeanPostProcessor( + ConfigurableApplicationContext context) { + return new NacosDuplicateSpringBeanPostProcessor(context); + } + + @Bean + public InstantiationAwareBeanPostProcessor nacosDuplicateConfigurationBeanPostProcessor( + ConfigurableApplicationContext context) { + return new NacosDuplicateConfigurationBeanPostProcessor(context); + } +} diff --git a/server/src/main/resources/nacos-server.properties b/server/src/main/resources/nacos-server.properties index f33ff7dd47e..f9cc852885c 100644 --- a/server/src/main/resources/nacos-server.properties +++ b/server/src/main/resources/nacos-server.properties @@ -16,4 +16,5 @@ ### nacos console port: server.port=${nacos.server.main.port:8080} -server.servlet.contextPath=${nacos.server.contextPath:/nacos} \ No newline at end of file +server.servlet.contextPath=${nacos.server.contextPath:/nacos} +spring.sql.init.mode=never \ No newline at end of file diff --git a/sys/src/main/java/com/alibaba/nacos/sys/env/AbstractNacosDuplicateBeanPostProcessor.java b/sys/src/main/java/com/alibaba/nacos/sys/env/AbstractNacosDuplicateBeanPostProcessor.java new file mode 100644 index 00000000000..26d1b8bddae --- /dev/null +++ b/sys/src/main/java/com/alibaba/nacos/sys/env/AbstractNacosDuplicateBeanPostProcessor.java @@ -0,0 +1,55 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.sys.env; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; +import org.springframework.context.ConfigurableApplicationContext; + +/** + * Abstract Nacos Duplicate Bean Post Processor of {@link InstantiationAwareBeanPostProcessor} to reduce duplicate rebuild bean for spring beans. + * + * @author xiweng.yy + */ +public abstract class AbstractNacosDuplicateBeanPostProcessor implements InstantiationAwareBeanPostProcessor { + + private final ConfigurableApplicationContext coreContext; + + protected AbstractNacosDuplicateBeanPostProcessor(ConfigurableApplicationContext context) { + coreContext = null == context.getParent() ? context : (ConfigurableApplicationContext) context.getParent(); + } + + @Override + public Object postProcessBeforeInstantiation(Class> beanClass, String beanName) throws BeansException { + if (!coreContext.containsBean(beanName)) { + return null; + } + BeanDefinition beanDefinition = coreContext.getBeanFactory().getBeanDefinition(beanName); + return isReUsingBean(beanClass, beanName, beanDefinition) ? coreContext.getBean(beanName) : null; + } + + /** + * Judge whether re-use beans from core context. + * + * @param beanClass bean class + * @param beanName bean name + * @param beanDefinition bean definition + * @return {@code true} means re-use beans from core context, otherwise {@code false} means to re-build bean in sub context. + */ + protected abstract boolean isReUsingBean(Class> beanClass, String beanName, BeanDefinition beanDefinition); +} diff --git a/sys/src/main/java/com/alibaba/nacos/sys/env/Constants.java b/sys/src/main/java/com/alibaba/nacos/sys/env/Constants.java index 72c3a599ee5..96494a28d91 100644 --- a/sys/src/main/java/com/alibaba/nacos/sys/env/Constants.java +++ b/sys/src/main/java/com/alibaba/nacos/sys/env/Constants.java @@ -89,4 +89,6 @@ public interface Constants { String NACOS_DEPLOYMENT_TYPE_SERVER = "server"; String NACOS_DEPLOYMENT_TYPE_CONSOLE = "console"; + + String NACOS_DUPLICATE_BEAN_ENHANCEMENT_ENABLED = "nacos.sys.duplicate.bean.enhancement.enabled"; } diff --git a/sys/src/main/java/com/alibaba/nacos/sys/env/NacosDuplicateConfigurationBeanPostProcessor.java b/sys/src/main/java/com/alibaba/nacos/sys/env/NacosDuplicateConfigurationBeanPostProcessor.java new file mode 100644 index 00000000000..2fc46378d0e --- /dev/null +++ b/sys/src/main/java/com/alibaba/nacos/sys/env/NacosDuplicateConfigurationBeanPostProcessor.java @@ -0,0 +1,45 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.sys.env; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Configuration; + +/** + * Nacos {@link InstantiationAwareBeanPostProcessor} to reduce duplicate rebuild bean for spring configuration. + * + * @author xiweng.yy + */ +public class NacosDuplicateConfigurationBeanPostProcessor extends AbstractNacosDuplicateBeanPostProcessor { + + public NacosDuplicateConfigurationBeanPostProcessor(ConfigurableApplicationContext context) { + super(context); + } + + @Override + protected boolean isReUsingBean(Class> beanClass, String beanName, BeanDefinition beanDefinition) { + return isConfiguration(beanClass); + } + + private boolean isConfiguration(Class> beanClass) { + return null != beanClass.getAnnotation(Configuration.class) || null != beanClass.getAnnotation( + AutoConfiguration.class); + } +} diff --git a/sys/src/main/java/com/alibaba/nacos/sys/env/NacosDuplicateSpringBeanPostProcessor.java b/sys/src/main/java/com/alibaba/nacos/sys/env/NacosDuplicateSpringBeanPostProcessor.java new file mode 100644 index 00000000000..f608e593505 --- /dev/null +++ b/sys/src/main/java/com/alibaba/nacos/sys/env/NacosDuplicateSpringBeanPostProcessor.java @@ -0,0 +1,51 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.sys.env; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; +import org.springframework.context.ConfigurableApplicationContext; + +/** + * Nacos {@link InstantiationAwareBeanPostProcessor} to reduce duplicate rebuild bean for spring beans. + * + *
+ * For some important spring beans like spring context beans, if reuse from parent, will cause some problem. So skip. + *
+ * + * @author xiweng.yy + */ +public class NacosDuplicateSpringBeanPostProcessor extends AbstractNacosDuplicateBeanPostProcessor { + + public NacosDuplicateSpringBeanPostProcessor(ConfigurableApplicationContext context) { + super(context); + } + + @Override + protected boolean isReUsingBean(Class> beanClass, String beanName, BeanDefinition beanDefinition) { + return !isContextBean(beanClass); + } + + private boolean isContextBean(Class> beanClass) { + return isContextClass(beanClass.getCanonicalName()); + } + + private boolean isContextClass(String beanClassName) { + return beanClassName.startsWith("org.springframework.context") + || beanClassName.startsWith("org.springframework.boot.context"); + } +} diff --git a/sys/src/test/java/com/alibaba/nacos/sys/env/NacosDuplicateConfigurationBeanPostProcessorTest.java b/sys/src/test/java/com/alibaba/nacos/sys/env/NacosDuplicateConfigurationBeanPostProcessorTest.java new file mode 100644 index 00000000000..737b994f9d9 --- /dev/null +++ b/sys/src/test/java/com/alibaba/nacos/sys/env/NacosDuplicateConfigurationBeanPostProcessorTest.java @@ -0,0 +1,96 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.sys.env; + +import com.alibaba.nacos.sys.env.mock.MockAutoConfiguration; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.LifecycleProcessor; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class NacosDuplicateConfigurationBeanPostProcessorTest { + + @Mock + NacosDuplicateConfigurationBeanPostProcessor processor; + + @Mock + ConfigurableApplicationContext context; + + @Mock + ConfigurableListableBeanFactory beanFactory; + + @Mock + BeanDefinition beanDefinition; + + @BeforeEach + void setUp() { + processor = new NacosDuplicateConfigurationBeanPostProcessor(context); + } + + @Test + void testPostProcessBeforeInstantiationNonExist() { + Class beanClass = LifecycleProcessor.class; + assertNull(processor.postProcessBeforeInstantiation(beanClass, "lifecycleProcessor")); + verify(context, never()).getBean("lifecycleProcessor"); + } + + @Test + void testPostProcessBeforeInstantiationForConfigurationAnnotation() { + String beanName = "com.alibaba.nacos.sys.env.mock.MockAutoConfiguration$MockConfiguration"; + when(context.containsBean(beanName)).thenReturn(true); + when(context.getBeanFactory()).thenReturn(beanFactory); + when(beanFactory.getBeanDefinition(beanName)).thenReturn(beanDefinition); + Class beanClass = MockAutoConfiguration.MockConfiguration.class; + MockAutoConfiguration.MockConfiguration existBean = new MockAutoConfiguration.MockConfiguration(); + when(context.getBean(beanName)).thenReturn(existBean); + assertEquals(existBean, processor.postProcessBeforeInstantiation(beanClass, beanName)); + } + + @Test + void testPostProcessBeforeInstantiationForAutoConfigurationAnnotation() { + String beanName = "com.alibaba.nacos.sys.env.mock.MockAutoConfiguration"; + when(context.containsBean(beanName)).thenReturn(true); + when(context.getBeanFactory()).thenReturn(beanFactory); + when(beanFactory.getBeanDefinition(beanName)).thenReturn(beanDefinition); + Class beanClass = MockAutoConfiguration.class; + MockAutoConfiguration existBean = new MockAutoConfiguration(); + when(context.getBean(beanName)).thenReturn(existBean); + assertEquals(existBean, processor.postProcessBeforeInstantiation(beanClass, beanName)); + } + + @Test + void testPostProcessBeforeInstantiationForNormalBean() { + when(context.containsBean("testBean")).thenReturn(true); + when(context.getBeanFactory()).thenReturn(beanFactory); + when(beanFactory.getBeanDefinition("testBean")).thenReturn(beanDefinition); + Class beanClass = NacosDuplicateConfigurationBeanPostProcessor.class; + assertNull(processor.postProcessBeforeInstantiation(beanClass, "testBean")); + verify(context, never()).getBean("testBean"); + } +} \ No newline at end of file diff --git a/sys/src/test/java/com/alibaba/nacos/sys/env/NacosDuplicateSpringBeanPostProcessorTest.java b/sys/src/test/java/com/alibaba/nacos/sys/env/NacosDuplicateSpringBeanPostProcessorTest.java new file mode 100644 index 00000000000..63e16ed3e44 --- /dev/null +++ b/sys/src/test/java/com/alibaba/nacos/sys/env/NacosDuplicateSpringBeanPostProcessorTest.java @@ -0,0 +1,92 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.sys.env; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.boot.context.properties.BoundConfigurationProperties; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.LifecycleProcessor; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class NacosDuplicateSpringBeanPostProcessorTest { + + @Mock + NacosDuplicateSpringBeanPostProcessor processor; + + @Mock + ConfigurableApplicationContext context; + + @Mock + ConfigurableListableBeanFactory beanFactory; + + @Mock + BeanDefinition beanDefinition; + + @BeforeEach + void setUp() { + processor = new NacosDuplicateSpringBeanPostProcessor(context); + } + + @Test + void testPostProcessBeforeInstantiationNonExist() { + Class beanClass = LifecycleProcessor.class; + assertNull(processor.postProcessBeforeInstantiation(beanClass, "lifecycleProcessor")); + verify(context, never()).getBean("lifecycleProcessor"); + } + + @Test + void testPostProcessBeforeInstantiationForContextBean() { + when(context.containsBean("lifecycleProcessor")).thenReturn(true); + when(context.getBeanFactory()).thenReturn(beanFactory); + when(beanFactory.getBeanDefinition("lifecycleProcessor")).thenReturn(beanDefinition); + Class beanClass = LifecycleProcessor.class; + assertNull(processor.postProcessBeforeInstantiation(beanClass, "lifecycleProcessor")); + verify(context, never()).getBean("lifecycleProcessor"); + } + + @Test + void testPostProcessBeforeInstantiationForBootContextBean() { + when(context.containsBean("boundConfigurationProperties")).thenReturn(true); + when(context.getBeanFactory()).thenReturn(beanFactory); + when(beanFactory.getBeanDefinition("boundConfigurationProperties")).thenReturn(beanDefinition); + Class beanClass = BoundConfigurationProperties.class; + assertNull(processor.postProcessBeforeInstantiation(beanClass, "boundConfigurationProperties")); + verify(context, never()).getBean("boundConfigurationProperties"); + } + + @Test + void testPostProcessBeforeInstantiationForNotContextBean() { + when(context.containsBean("testBean")).thenReturn(true); + when(context.getBeanFactory()).thenReturn(beanFactory); + when(beanFactory.getBeanDefinition("testBean")).thenReturn(beanDefinition); + Class beanClass = NacosDuplicateSpringBeanPostProcessorTest.class; + when(context.getBean("testBean")).thenReturn(this); + assertEquals(this, processor.postProcessBeforeInstantiation(beanClass, "testBean")); + } +} \ No newline at end of file diff --git a/sys/src/test/java/com/alibaba/nacos/sys/env/mock/MockAutoConfiguration.java b/sys/src/test/java/com/alibaba/nacos/sys/env/mock/MockAutoConfiguration.java new file mode 100644 index 00000000000..9a6498f6678 --- /dev/null +++ b/sys/src/test/java/com/alibaba/nacos/sys/env/mock/MockAutoConfiguration.java @@ -0,0 +1,29 @@ +/* + * Copyright 1999-2023 Alibaba Group Holding Ltd. + * + * Licensed 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.alibaba.nacos.sys.env.mock; + +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.context.annotation.Configuration; + +@AutoConfiguration +public class MockAutoConfiguration { + + @Configuration + public static class MockConfiguration { + + } +}