-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ 使用 BeanFactoryPostProcessor 来提升 SpringUtils 的初始化时机
- Loading branch information
Showing
4 changed files
with
48 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...n-util/src/main/java/org/ballcat/common/util/SpringUtilsInitBeanFactoryPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.ballcat.common.util; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* <p> | ||
* 通过 BeanFactoryPostProcessor 提前获取 SpringUtils, 可以提升 SpringUtils 的注册时机,防止在使用时还未注册导致获取 | ||
* context 为 null 的情况。 | ||
* </p> | ||
* | ||
* @author Hccake | ||
* @since 2.0.0 | ||
*/ | ||
@Component | ||
public class SpringUtilsInitBeanFactoryPostProcessor implements BeanFactoryPostProcessor { | ||
|
||
@Override | ||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { | ||
beanFactory.getBean(SpringUtils.class); | ||
} | ||
|
||
} |
3 changes: 2 additions & 1 deletion
3
common/ballcat-common-util/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
org.ballcat.common.util.SpringUtils | ||
org.ballcat.common.util.SpringUtils,\ | ||
org.ballcat.common.util.SpringUtilsInitBeanFactoryPostProcessor | ||
|
3 changes: 2 additions & 1 deletion
3
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
org.ballcat.common.util.SpringUtils | ||
org.ballcat.common.util.SpringUtils | ||
org.ballcat.common.util.SpringUtilsInitBeanFactoryPostProcessor |