mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add method
This commit is contained in:
parent
9d7888acd1
commit
8c809353ca
@ -12,6 +12,7 @@
|
||||
* 【db 】 AbstractDb增加executeBatch重载(issue#1053@Github)
|
||||
* 【extra 】 新增方便引入SpringUtil的注解@EnableSpringUtil(pr#172@Gitee)
|
||||
* 【poi 】 RowUtil增加插入和删除行(pr#1060@Github)
|
||||
* 【extra 】 SpringUtil增加注册bean(pr#174@Gitee)
|
||||
|
||||
### Bug修复#
|
||||
* 【core 】 重新整理农历节假日,解决一个pr过来的玩笑导致的问题
|
||||
|
@ -150,21 +150,21 @@ public class SpringUtil implements ApplicationContextAware {
|
||||
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);
|
||||
}
|
||||
/**
|
||||
* 动态向Spring注册Bean
|
||||
* <p>
|
||||
* 由{@link org.springframework.beans.factory.BeanFactory} 实现,通过工具开放API
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param beanName 名称
|
||||
* @param bean bean
|
||||
* @author shadow
|
||||
* @since 5.4.2
|
||||
*/
|
||||
public static <T> void registerBean(String beanName, T bean) {
|
||||
ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext;
|
||||
context.getBeanFactory().registerSingleton(beanName, bean);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -10,9 +10,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ -21,26 +19,20 @@ import java.util.Map;
|
||||
public class SpringUtilTest {
|
||||
|
||||
/**
|
||||
* 注册bean
|
||||
* 方便执行顺序使用abcd...的顺序命名方法
|
||||
* 注册验证bean
|
||||
*/
|
||||
@Test
|
||||
public void a() {
|
||||
public void registerBeanTest() {
|
||||
Demo2 registerBean = new Demo2();
|
||||
registerBean.setId(123);
|
||||
registerBean.setName("222");
|
||||
SpringUtil.registerBean("registerBean", registerBean);
|
||||
|
||||
Demo2 registerBean2 = SpringUtil.getBean("registerBean");
|
||||
Assert.assertEquals(123, registerBean2.getId());
|
||||
Assert.assertEquals("222", registerBean2.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证注册的bean
|
||||
*/
|
||||
@Test
|
||||
public void b() {
|
||||
Demo2 registerBean = SpringUtil.getBean("registerBean");
|
||||
Assert.assertEquals(123, registerBean.getId());
|
||||
Assert.assertEquals("222", registerBean.getName());
|
||||
}
|
||||
@Test
|
||||
public void getBeanTest(){
|
||||
final Demo2 testDemo = SpringUtil.getBean("testDemo");
|
||||
|
Loading…
x
Reference in New Issue
Block a user