mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
feat(SpringUtil): 增加新特性
1.添加向Spring动态注册Bean的方法
This commit is contained in:
parent
6d52779006
commit
e58ac72bbb
@ -4,6 +4,7 @@ import cn.hutool.core.lang.TypeReference;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@ -148,6 +149,22 @@ public class SpringUtil implements ApplicationContextAware {
|
||||
final String[] activeProfiles = getActiveProfiles();
|
||||
return ArrayUtil.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态向Spring注册Bean
|
||||
* <p>
|
||||
* 由{@link org.springframework.beans.factory.BeanFactory} 实现,通过工具开放API
|
||||
*
|
||||
* @param beanName 名称
|
||||
* @param bean bean
|
||||
* @param <T> 泛型
|
||||
* @author shadow
|
||||
* @date 2020-09-04
|
||||
*/
|
||||
public static <T> void registerBean(String beanName, T bean) {
|
||||
ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext;
|
||||
context.getBeanFactory().registerSingleton(beanName, bean);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -10,6 +10,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ -18,11 +19,34 @@ import java.util.Map;
|
||||
//@Import(cn.hutool.extra.spring.SpringUtil.class)
|
||||
public class SpringUtilTest {
|
||||
|
||||
/**
|
||||
* 注册bean
|
||||
*
|
||||
* 为了保证顺序采用顺序abcdef...形式命名方法
|
||||
*/
|
||||
@Test
|
||||
public void a() {
|
||||
Demo2 registerBean = new Demo2();
|
||||
registerBean.id = 123;
|
||||
registerBean.name = "222";
|
||||
SpringUtil.registerBean("registerBean", registerBean);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证注册的bean
|
||||
*/
|
||||
@Test
|
||||
public void b() {
|
||||
Demo2 registerBean = SpringUtil.getBean("registerBean");
|
||||
Assert.assertEquals(123, registerBean.id);
|
||||
Assert.assertEquals("222", registerBean.name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBeanTest(){
|
||||
final Demo2 testDemo = SpringUtil.getBean("testDemo");
|
||||
Assert.assertEquals(12345, testDemo.getId());
|
||||
Assert.assertEquals("test", testDemo.getName());
|
||||
Assert.assertEquals(12345, testDemo.id);
|
||||
Assert.assertEquals("test", testDemo.name);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -41,8 +65,8 @@ public class SpringUtilTest {
|
||||
@Bean(name="testDemo")
|
||||
public Demo2 generateDemo() {
|
||||
Demo2 demo = new Demo2();
|
||||
demo.setId(12345);
|
||||
demo.setName("test");
|
||||
demo.id = 12345;
|
||||
demo.name= "test";
|
||||
return demo;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user