-
Notifications
You must be signed in to change notification settings - Fork 31
feat:模块复用基座单例bean时,在模块销毁时不销毁基座bean #207
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * 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.koupleless.plugin.context; | ||
|
|
||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.context.properties.source.ConfigurationPropertySources; | ||
| import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment; | ||
| import org.springframework.core.env.ConfigurablePropertyResolver; | ||
| import org.springframework.core.env.MutablePropertySources; | ||
|
|
||
| /** | ||
| * copy from {@link org.springframework.boot.web.reactive.context.ApplicationReactiveWebEnvironment} | ||
| * due to not public class | ||
| * {@link StandardReactiveWebEnvironment} for typical use in a typical | ||
| * {@link SpringApplication}. | ||
| * | ||
| * @author Phillip Webb | ||
| */ | ||
| class ApplicationReactiveWebEnvironment extends StandardReactiveWebEnvironment { | ||
|
|
||
| @Override | ||
| protected String doGetActiveProfilesProperty() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| protected String doGetDefaultProfilesProperty() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| protected ConfigurablePropertyResolver createPropertyResolver(MutablePropertySources propertySources) { | ||
| return ConfigurationPropertySources.createPropertyResolver(propertySources); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * 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.koupleless.plugin.context; | ||
|
|
||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.context.properties.source.ConfigurationPropertySources; | ||
| import org.springframework.core.env.ConfigurablePropertyResolver; | ||
| import org.springframework.core.env.MutablePropertySources; | ||
| import org.springframework.web.context.support.StandardServletEnvironment; | ||
|
|
||
| /** | ||
| * copy from {@link org.springframework.boot.web.reactive.context.ApplicationServletEnvironment} | ||
| * due to not public class | ||
| * {@link StandardServletEnvironment} for typical use in a typical | ||
| * {@link SpringApplication}. | ||
| * | ||
| * @author Phillip Webb | ||
| */ | ||
| class ApplicationServletEnvironment extends StandardServletEnvironment { | ||
|
|
||
| @Override | ||
| protected String doGetActiveProfilesProperty() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| protected String doGetDefaultProfilesProperty() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| protected ConfigurablePropertyResolver createPropertyResolver(MutablePropertySources propertySources) { | ||
| return ConfigurationPropertySources.createPropertyResolver(propertySources); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * 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.koupleless.plugin.context; | ||
|
|
||
| import org.springframework.beans.factory.support.DefaultListableBeanFactory; | ||
| import org.springframework.boot.ApplicationContextFactory; | ||
| import org.springframework.boot.WebApplicationType; | ||
| import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext; | ||
| import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; | ||
| import org.springframework.context.ConfigurableApplicationContext; | ||
| import org.springframework.core.Ordered; | ||
| import org.springframework.core.env.ConfigurableEnvironment; | ||
|
|
||
| /** | ||
| * 目的是自定义 beanFactory 的销毁行为 | ||
| * | ||
| * @author duanzhiqiang | ||
| * @version BizAnnotationConfigReactiveWebServerApplicationContext.java, v 0.1 2024年11月08日 16:23 duanzhiqiang | ||
| */ | ||
| public class BizAnnotationConfigReactiveWebServerApplicationContext extends | ||
| AnnotationConfigReactiveWebServerApplicationContext { | ||
| /** | ||
| * 构造器 | ||
| * | ||
| * @param beanFactory 指定的beanFactory | ||
| */ | ||
| public BizAnnotationConfigReactiveWebServerApplicationContext(DefaultListableBeanFactory beanFactory) { | ||
| super(beanFactory); | ||
| } | ||
|
|
||
| /** | ||
| * 替换上下文创建行为利用 spring boot factories 扩展 并在默认的优先级前 | ||
| * {@link ApplicationContextFactory} registered in {@code spring.factories} to support | ||
| * {@link AnnotationConfigServletWebServerApplicationContext}. | ||
| */ | ||
| static class Factory implements ApplicationContextFactory, Ordered { | ||
|
|
||
| @Override | ||
| public Class<? extends ConfigurableEnvironment> getEnvironmentType(WebApplicationType webApplicationType) { | ||
| return (webApplicationType != WebApplicationType.REACTIVE) ? null | ||
| : ApplicationReactiveWebEnvironment.class; | ||
| } | ||
|
|
||
| @Override | ||
| public ConfigurableEnvironment createEnvironment(WebApplicationType webApplicationType) { | ||
| return (webApplicationType != WebApplicationType.REACTIVE) ? null | ||
| : new ApplicationReactiveWebEnvironment(); | ||
| } | ||
|
|
||
| @Override | ||
| public ConfigurableApplicationContext create(WebApplicationType webApplicationType) { | ||
| return (webApplicationType != WebApplicationType.REACTIVE) ? null : createContext(); | ||
| } | ||
|
|
||
| /** | ||
| * 创建上下文 | ||
| * | ||
| * @return 上下文 | ||
| */ | ||
| private ConfigurableApplicationContext createContext() { | ||
| //自定义BeanFactory的销毁 | ||
| DefaultListableBeanFactory beanFactory = new BizDefaultListableBeanFactory(); | ||
|
||
| return new BizAnnotationConfigReactiveWebServerApplicationContext(beanFactory); | ||
| } | ||
|
|
||
| @Override | ||
| public int getOrder() { | ||
| return Ordered.HIGHEST_PRECEDENCE; | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /* | ||
| * 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.koupleless.plugin.context; | ||
|
|
||
| import org.springframework.beans.BeansException; | ||
| import org.springframework.beans.factory.support.DefaultListableBeanFactory; | ||
| import org.springframework.boot.ApplicationContextFactory; | ||
| import org.springframework.boot.WebApplicationType; | ||
|
|
||
| import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; | ||
| import org.springframework.context.ConfigurableApplicationContext; | ||
| import org.springframework.core.Ordered; | ||
| import org.springframework.core.env.ConfigurableEnvironment; | ||
|
|
||
| /** | ||
| * 目的是自定义 beanFactory 的销毁行为 | ||
| * | ||
| * @author duanzhiqiang | ||
| * @version BizAnnotationConfigServletWebServerApplicationContext.java, v 0.1 2024年11月08日 16:23 duanzhiqiang | ||
| */ | ||
| public class BizAnnotationConfigServletWebServerApplicationContext extends | ||
| AnnotationConfigServletWebServerApplicationContext { | ||
| /** | ||
| * 构造器 | ||
| * | ||
| * @param beanFactory 指定的beanFactory | ||
| */ | ||
| public BizAnnotationConfigServletWebServerApplicationContext(DefaultListableBeanFactory beanFactory) { | ||
| super(beanFactory); | ||
| } | ||
|
|
||
| @Override | ||
| public Object getBean(String name) throws BeansException { | ||
| return super.getBean(name); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException { | ||
| return super.getBean(requiredType, args); | ||
| } | ||
|
|
||
| /** | ||
| * 替换上下文创建行为利用 spring boot factories 扩展 并在默认的优先级前 | ||
| * {@link ApplicationContextFactory} registered in {@code spring.factories} to support | ||
| * {@link AnnotationConfigServletWebServerApplicationContext}. | ||
| */ | ||
| static class Factory implements ApplicationContextFactory, Ordered { | ||
|
|
||
| @Override | ||
| public Class<? extends ConfigurableEnvironment> getEnvironmentType(WebApplicationType webApplicationType) { | ||
| return (webApplicationType != WebApplicationType.SERVLET) ? null | ||
| : ApplicationServletEnvironment.class; | ||
| } | ||
|
||
|
|
||
| @Override | ||
| public ConfigurableEnvironment createEnvironment(WebApplicationType webApplicationType) { | ||
| return (webApplicationType != WebApplicationType.SERVLET) ? null | ||
| : new ApplicationServletEnvironment(); | ||
| } | ||
|
|
||
| @Override | ||
| public ConfigurableApplicationContext create(WebApplicationType webApplicationType) { | ||
| return (webApplicationType != WebApplicationType.SERVLET) ? null : createContext(); | ||
| } | ||
|
|
||
| /** | ||
| * 创建上下文 | ||
| * | ||
| * @return 上下文 | ||
| */ | ||
| private ConfigurableApplicationContext createContext() { | ||
| //自定义BeanFactory的销毁 | ||
| DefaultListableBeanFactory beanFactory = new BizDefaultListableBeanFactory(); | ||
|
||
| return new BizAnnotationConfigServletWebServerApplicationContext(beanFactory); | ||
| } | ||
|
|
||
| @Override | ||
| public int getOrder() { | ||
| return HIGHEST_PRECEDENCE; | ||
| } | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Undefined class
ApplicationReactiveWebEnvironmentThe class
ApplicationReactiveWebEnvironmentis referenced in methodsgetEnvironmentTypeandcreateEnvironmentbut is not imported or defined in the provided code. This will lead to compilation errors.Ensure that
ApplicationReactiveWebEnvironmentis properly defined and imported.Also applies to: 60-61
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Gentten, thank you for removing the undefined reference to
ApplicationReactiveWebEnvironment.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!