mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
add SpringUtil in spring.factories
This commit is contained in:
parent
09e33610a2
commit
d1af3c44f0
@ -6,6 +6,8 @@
|
||||
# 5.5.8 (2021-01-09)
|
||||
|
||||
### 新特性
|
||||
* 【extra 】 增加自动装配SpringUtil类(pr#1366@Github)
|
||||
|
||||
### Bug修复
|
||||
* 【core 】 修复FileUtil.move以及PathUtil.copy等无法自动创建父目录的问题(issue#I2CKTI@Gitee)
|
||||
|
||||
|
@ -87,6 +87,7 @@ public class NetUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void isOpenTest(){
|
||||
InetSocketAddress address = new InetSocketAddress("www.hutool.cn", 443);
|
||||
Assert.assertTrue(NetUtil.isOpen(address, 200));
|
||||
|
@ -0,0 +1,74 @@
|
||||
package cn.hutool.extra.spring;
|
||||
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = {SpringUtilWithAutoConfigTest.Demo2.class})
|
||||
//@Import(cn.hutool.extra.spring.SpringUtil.class)
|
||||
@EnableAutoConfiguration
|
||||
public class SpringUtilWithAutoConfigTest {
|
||||
|
||||
/**
|
||||
* 注册验证bean
|
||||
*/
|
||||
@Test
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBeanTest(){
|
||||
final Demo2 testDemo = SpringUtil.getBean("testDemo");
|
||||
Assert.assertEquals(12345, testDemo.getId());
|
||||
Assert.assertEquals("test", testDemo.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBeanWithTypeReferenceTest() {
|
||||
Map<String, Object> mapBean = SpringUtil.getBean(new TypeReference<Map<String, Object>>() {});
|
||||
Assert.assertNotNull(mapBean);
|
||||
Assert.assertEquals("value1", mapBean.get("key1"));
|
||||
Assert.assertEquals("value2", mapBean.get("key2"));
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Demo2{
|
||||
private long id;
|
||||
private String name;
|
||||
|
||||
@Bean(name="testDemo")
|
||||
public Demo2 generateDemo() {
|
||||
Demo2 demo = new Demo2();
|
||||
demo.setId(12345);
|
||||
demo.setName("test");
|
||||
return demo;
|
||||
}
|
||||
|
||||
@Bean(name="mapDemo")
|
||||
public Map<String, Object> generateMap() {
|
||||
HashMap<String, Object> map = MapUtil.newHashMap();
|
||||
map.put("key1", "value1");
|
||||
map.put("key2", "value2");
|
||||
return map;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user