mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
use ApplicationContextInitializer
This commit is contained in:
parent
4afe2aee24
commit
692bdd9e88
@ -1,31 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.extra.spring;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 启用SpringUtil, 即注入SpringUtil到容器中
|
||||
*
|
||||
* @author sidian
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Import(SpringUtil.class)
|
||||
public @interface EnableSpringUtil {
|
||||
}
|
@ -12,20 +12,14 @@
|
||||
|
||||
package org.dromara.hutool.extra.spring;
|
||||
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.exception.HutoolException;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.*;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.util.Arrays;
|
||||
@ -42,28 +36,15 @@ import java.util.Map;
|
||||
* @author loolly
|
||||
* @since 5.1.0
|
||||
*/
|
||||
@Component
|
||||
public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextAware {
|
||||
public class SpringUtil implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
|
||||
/**
|
||||
* "@PostConstruct"注解标记的类中,由于ApplicationContext还未加载,导致空指针<br>
|
||||
* 因此实现BeanFactoryPostProcessor注入ConfigurableListableBeanFactory实现bean的操作
|
||||
*/
|
||||
private static ConfigurableListableBeanFactory beanFactory;
|
||||
/**
|
||||
* Spring应用上下文环境
|
||||
*/
|
||||
private static ApplicationContext applicationContext;
|
||||
private static ConfigurableApplicationContext applicationContext;
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@Override
|
||||
public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
SpringUtil.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@Override
|
||||
public void setApplicationContext(final ApplicationContext applicationContext) {
|
||||
public void initialize(final ConfigurableApplicationContext applicationContext) {
|
||||
SpringUtil.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@ -83,7 +64,7 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA
|
||||
* @since 5.7.0
|
||||
*/
|
||||
public static ListableBeanFactory getBeanFactory() {
|
||||
final ListableBeanFactory factory = null == beanFactory ? applicationContext : beanFactory;
|
||||
final ListableBeanFactory factory = applicationContext;
|
||||
if(null == factory){
|
||||
throw new HutoolException("No ConfigurableListableBeanFactory or ApplicationContext injected, maybe not in the Spring environment?");
|
||||
}
|
||||
@ -98,15 +79,7 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA
|
||||
* @since 5.7.7
|
||||
*/
|
||||
public static ConfigurableListableBeanFactory getConfigurableBeanFactory() throws HutoolException {
|
||||
final ConfigurableListableBeanFactory factory;
|
||||
if (null != beanFactory) {
|
||||
factory = beanFactory;
|
||||
} else if (applicationContext instanceof ConfigurableApplicationContext) {
|
||||
factory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
|
||||
} else {
|
||||
throw new HutoolException("No ConfigurableListableBeanFactory from context!");
|
||||
}
|
||||
return factory;
|
||||
return applicationContext.getBeanFactory();
|
||||
}
|
||||
|
||||
//通过name获取 Bean.
|
||||
|
@ -1,3 +1,3 @@
|
||||
# Auto Configure
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
# Initializers
|
||||
org.springframework.context.ApplicationContextInitializer=\
|
||||
org.dromara.hutool.extra.spring.SpringUtil
|
||||
|
@ -1 +0,0 @@
|
||||
org.dromara.hutool.extra.spring.SpringUtil
|
@ -1,24 +0,0 @@
|
||||
package org.dromara.hutool.extra.spring;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
/**
|
||||
* @author sidian
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(classes = EnableSpringUtilTest.class)
|
||||
@EnableSpringUtil
|
||||
public class EnableSpringUtilTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
// 使用@EnableSpringUtil注解后, 能获取上下文
|
||||
Assertions.assertNotNull(SpringUtil.getApplicationContext());
|
||||
// 不使用时, 为null
|
||||
// Assertions.assertNull(SpringUtil.getApplicationContext());
|
||||
}
|
||||
}
|
@ -21,6 +21,8 @@ import java.util.Map;
|
||||
//@Import(spring.org.dromara.hutool.extra.SpringUtil.class)
|
||||
public class SpringUtilTest {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 注册验证bean
|
||||
*/
|
||||
|
@ -16,7 +16,6 @@ import java.util.Map;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(classes = {SpringUtilWithAutoConfigTest.Demo2.class})
|
||||
//@Import(spring.org.dromara.hutool.extra.SpringUtil.class)
|
||||
@EnableAutoConfiguration
|
||||
public class SpringUtilWithAutoConfigTest {
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user