mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix(SpringUtil): 修复@Autowired的问题
1. 修复registerBean中自动注入的问题 2. 增加unRegisterBean注销bean的方法
This commit is contained in:
parent
aa894f1f65
commit
8f29d0cc67
@ -6,10 +6,13 @@ import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ -33,6 +36,57 @@ public class SpringUtilTest {
|
||||
Assert.assertEquals("222", registerBean2.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试自动注入
|
||||
*/
|
||||
@Test
|
||||
public void registerBeanTest2() {
|
||||
TestAutoWired testAutoWired = new TestAutoWired();
|
||||
TestBean testBean = new TestBean();
|
||||
testBean.setId("123");
|
||||
SpringUtil.registerBean("testBean", testBean);
|
||||
SpringUtil.registerBean("testAutoWired", testAutoWired);
|
||||
|
||||
testAutoWired = SpringUtil.getBean("testAutoWired");
|
||||
Assert.assertNotNull(testAutoWired);
|
||||
Assert.assertNotNull(testAutoWired.getAutowiredBean());
|
||||
Assert.assertEquals("123", testAutoWired.getAutowiredBean().getId());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试注销bean
|
||||
*/
|
||||
@Test
|
||||
public void unRegisterBeanTest() {
|
||||
registerBeanTest2();
|
||||
Assert.assertNotNull(SpringUtil.getBean("testAutoWired"));
|
||||
SpringUtil.unRegisterBean("testAutoWired1");
|
||||
try {
|
||||
SpringUtil.getBean("testAutoWired");
|
||||
} catch (NoSuchBeanDefinitionException e) {
|
||||
Assert.assertEquals(e.getClass(), NoSuchBeanDefinitionException.class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试替换bean
|
||||
|
||||
@Test
|
||||
public void replaceBeanTest() {
|
||||
registerBeanTest2();
|
||||
TestAutoWired testAutoWired = new TestAutoWired();
|
||||
TestBean testBean = new TestBean();
|
||||
testBean.setId("222");
|
||||
Assert.assertEquals("123", SpringUtil.getBean("testBean", TestBean.class).getId());
|
||||
SpringUtil.replaceBean("testBean", testBean);
|
||||
SpringUtil.replaceBean("testAutoWired", testAutoWired);
|
||||
testAutoWired = SpringUtil.getBean("testAutoWired");
|
||||
TestBean testBean1 = testAutoWired.getAutowiredBean();
|
||||
Assert.assertEquals("222", testAutoWired.getAutowiredBean().getId());
|
||||
Assert.assertEquals("222", testBean1.getId());
|
||||
|
||||
}*/
|
||||
@Test
|
||||
public void getBeanTest(){
|
||||
final Demo2 testDemo = SpringUtil.getBean("testDemo");
|
||||
@ -69,4 +123,17 @@ public class SpringUtilTest {
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class TestAutoWired {
|
||||
|
||||
@Autowired
|
||||
// @Resource
|
||||
private TestBean autowiredBean;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class TestBean {
|
||||
private String id;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user