mirror of
https://gitee.com/dromara/hutool.git
synced 2025-07-30 22:10:27 +08:00
fix bug
This commit is contained in:
parent
bedc3dc916
commit
14b35d0e70
@ -89,18 +89,6 @@ public class SpringUtil implements ApplicationContextInitializer<ConfigurableApp
|
||||
|
||||
//通过name获取 Bean.
|
||||
|
||||
/**
|
||||
* 通过name获取 Bean
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param name Bean名称
|
||||
* @return Bean
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getBean(final String name) {
|
||||
return (T) getBeanFactory().getBean(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过class获取Bean
|
||||
*
|
||||
@ -110,6 +98,10 @@ public class SpringUtil implements ApplicationContextInitializer<ConfigurableApp
|
||||
* @return Bean对象
|
||||
*/
|
||||
public static <T> T getBean(final Class<T> clazz, final Object... args) {
|
||||
final ListableBeanFactory beanFactory = getBeanFactory();
|
||||
if (ArrayUtil.isEmpty(args)) {
|
||||
return beanFactory.getBean(clazz);
|
||||
}
|
||||
return getBeanFactory().getBean(clazz, args);
|
||||
}
|
||||
|
||||
@ -130,11 +122,17 @@ public class SpringUtil implements ApplicationContextInitializer<ConfigurableApp
|
||||
*
|
||||
* @param name Bean名称
|
||||
* @param args 创建bean需要的参数属性
|
||||
* @param <T> Bean类型
|
||||
* @return Bean对象
|
||||
* @since 5.8.34
|
||||
*/
|
||||
public static Object getBean(final String name, final Object... args) {
|
||||
return getBeanFactory().getBean(name, args);
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getBean(final String name, final Object... args) {
|
||||
final ListableBeanFactory beanFactory = getBeanFactory();
|
||||
if (ArrayUtil.isEmpty(args)) {
|
||||
return (T) beanFactory.getBean(name);
|
||||
}
|
||||
return (T) beanFactory.getBean(name, args);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -26,6 +26,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
@ -53,8 +54,6 @@ public class SpringUtilTest {
|
||||
final Demo2 registerBean2 = SpringUtil.getBean("registerBean");
|
||||
Assertions.assertEquals(123, registerBean2.getId());
|
||||
Assertions.assertEquals("222", registerBean2.getName());
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,6 +114,16 @@ public class SpringUtilTest {
|
||||
assert "dev".equals(activeProfile2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getEnvironmentTest() {
|
||||
ConfigurableEnvironment bean = SpringUtil.getBean(ConfigurableEnvironment.class);
|
||||
Assertions.assertNotNull(bean);
|
||||
bean = SpringUtil.getBean(ConfigurableEnvironment.class, new Object[0]);
|
||||
Assertions.assertNotNull(bean);
|
||||
bean = SpringUtil.getBean(ConfigurableEnvironment.class, (Object[])null);
|
||||
Assertions.assertNotNull(bean);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Demo2{
|
||||
private long id;
|
||||
|
Loading…
Reference in New Issue
Block a user