Skip to content

Commit

Permalink
⚡ 使用 BeanFactoryPostProcessor 来提升 SpringUtils 的初始化时机
Browse files Browse the repository at this point in the history
  • Loading branch information
Hccake committed Jul 3, 2024
1 parent e440831 commit 6cd5790
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
/**
* @author lingting 2020/6/12 16:36
*/
@Component
@Component(SpringUtils.BEAN_NAME)
public class SpringUtils implements ApplicationContextAware {

public static final String BEAN_NAME = "ballcatSpringUtils";

/**
* Spring应用上下文环境
*/
Expand Down
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);
}

}
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

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

0 comments on commit 6cd5790

Please sign in to comment.