mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix name
This commit is contained in:
parent
a3992452e2
commit
0026ffff93
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
# 5.7.6 (2021-07-30)
|
# 5.7.7 (2021-08-01)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【core 】 增加LookupFactory和MethodHandleUtil(issue#I42TVY@Gitee)
|
* 【core 】 增加LookupFactory和MethodHandleUtil(issue#I42TVY@Gitee)
|
||||||
@ -11,9 +11,12 @@
|
|||||||
* 【core 】 PhoneUtil中新增获取固话号码中区号,以及固话号码中号码的方法(pr#387@Gitee)
|
* 【core 】 PhoneUtil中新增获取固话号码中区号,以及固话号码中号码的方法(pr#387@Gitee)
|
||||||
* 【json 】 JSONGetter增加getLocalDateTime方法(pr#387@Gitee)
|
* 【json 】 JSONGetter增加getLocalDateTime方法(pr#387@Gitee)
|
||||||
* 【core 】 增加JNDIUtil(issue#1727@Github)
|
* 【core 】 增加JNDIUtil(issue#1727@Github)
|
||||||
|
* 【core 】 SpringUtil增加unregisterBean方法(pr#388@Gitee)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【jwt 】 修复JWTUtil中几个方法非static的问题(issue#1735@Github)
|
* 【jwt 】 修复JWTUtil中几个方法非static的问题(issue#1735@Github)
|
||||||
|
* 【core 】 修复SpringUtil无法处理autowired问题(pr#388@Gitee)
|
||||||
|
* 【core 】 修复AbsCollValueMap中常量拼写错误(pr#1736@Github)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package cn.hutool.extra.spring;
|
package cn.hutool.extra.spring;
|
||||||
|
|
||||||
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
import cn.hutool.core.lang.TypeReference;
|
import cn.hutool.core.lang.TypeReference;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.ListableBeanFactory;
|
import org.springframework.beans.factory.ListableBeanFactory;
|
||||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
|
||||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
|
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
|
||||||
@ -21,9 +21,10 @@ import java.util.Map;
|
|||||||
/**
|
/**
|
||||||
* Spring(Spring boot)工具封装,包括:
|
* Spring(Spring boot)工具封装,包括:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <ol>
|
||||||
* 1、Spring IOC容器中的bean对象获取
|
* <li>Spring IOC容器中的bean对象获取</li>
|
||||||
* </pre>
|
* <li>注册和注销Bean</li>
|
||||||
|
* </ol>
|
||||||
*
|
*
|
||||||
* @author loolly
|
* @author loolly
|
||||||
* @since 5.1.0
|
* @since 5.1.0
|
||||||
@ -72,6 +73,25 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA
|
|||||||
return null == beanFactory ? applicationContext : beanFactory;
|
return null == beanFactory ? applicationContext : beanFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取{@link ConfigurableListableBeanFactory}
|
||||||
|
*
|
||||||
|
* @return {@link ConfigurableListableBeanFactory}
|
||||||
|
* @since 5.7.7
|
||||||
|
* @throws UtilException 当上下文非ConfigurableListableBeanFactory抛出异常
|
||||||
|
*/
|
||||||
|
public static ConfigurableListableBeanFactory getConfigurableBeanFactory() throws UtilException{
|
||||||
|
final ConfigurableListableBeanFactory factory;
|
||||||
|
if (null != beanFactory) {
|
||||||
|
factory = beanFactory;
|
||||||
|
} else if (applicationContext instanceof ConfigurableApplicationContext) {
|
||||||
|
factory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
|
||||||
|
} else {
|
||||||
|
throw new UtilException("No ConfigurableListableBeanFactory from context!");
|
||||||
|
}
|
||||||
|
return factory;
|
||||||
|
}
|
||||||
|
|
||||||
//通过name获取 Bean.
|
//通过name获取 Bean.
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -201,15 +221,9 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA
|
|||||||
* @since 5.4.2
|
* @since 5.4.2
|
||||||
*/
|
*/
|
||||||
public static <T> void registerBean(String beanName, T bean) {
|
public static <T> void registerBean(String beanName, T bean) {
|
||||||
if (null != beanFactory) {
|
final ConfigurableListableBeanFactory factory = getConfigurableBeanFactory();
|
||||||
beanFactory.autowireBean(bean);
|
factory.autowireBean(bean);
|
||||||
beanFactory.registerSingleton(beanName, bean);
|
factory.registerSingleton(beanName, bean);
|
||||||
} else if (applicationContext instanceof ConfigurableApplicationContext) {
|
|
||||||
ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext;
|
|
||||||
AutowireCapableBeanFactory factory = context.getAutowireCapableBeanFactory();
|
|
||||||
factory.autowireBean(bean);
|
|
||||||
context.getBeanFactory().registerSingleton(beanName, bean);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -221,32 +235,15 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA
|
|||||||
* @author shadow
|
* @author shadow
|
||||||
* @since 5.7.7
|
* @since 5.7.7
|
||||||
*/
|
*/
|
||||||
public static void unRegisterBean(String beanName) {
|
public static void unregisterBean(String beanName) {
|
||||||
if (applicationContext instanceof ConfigurableApplicationContext) {
|
final ConfigurableListableBeanFactory factory = getConfigurableBeanFactory();
|
||||||
ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext;
|
if(factory instanceof DefaultSingletonBeanRegistry){
|
||||||
DefaultSingletonBeanRegistry registry = (DefaultSingletonBeanRegistry) context.getBeanFactory();
|
DefaultSingletonBeanRegistry registry = (DefaultSingletonBeanRegistry) factory;
|
||||||
registry.destroySingleton(beanName);
|
registry.destroySingleton(beanName);
|
||||||
|
} else {
|
||||||
|
throw new UtilException("Can not unregister bean, the factory is not a DefaultSingletonBeanRegistry!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* 替换Bean
|
|
||||||
* 组合{@link SpringUtil#unRegisterBean(String)} 和 {@link SpringUtil#replaceBean(String, Object)}
|
|
||||||
* 将Spring持有bean先注销再注册
|
|
||||||
* 需要注意的替换Bean的内部对象的指针指向并不会变化
|
|
||||||
* 所有替换的bean被持有的情况下,需要有下至上逐步替换
|
|
||||||
*
|
|
||||||
* @param beanName bean名称
|
|
||||||
* @param bean bean
|
|
||||||
* @param <T> 泛型
|
|
||||||
* @author shadow
|
|
||||||
* @since 5.7.7
|
|
||||||
*/
|
|
||||||
/*public static <T> void replaceBean(String beanName, T bean) {
|
|
||||||
unRegisterBean(beanName);
|
|
||||||
registerBean(beanName, bean);
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -34,13 +34,29 @@ public class SpringUtilTest {
|
|||||||
Demo2 registerBean2 = SpringUtil.getBean("registerBean");
|
Demo2 registerBean2 = SpringUtil.getBean("registerBean");
|
||||||
Assert.assertEquals(123, registerBean2.getId());
|
Assert.assertEquals(123, registerBean2.getId());
|
||||||
Assert.assertEquals("222", registerBean2.getName());
|
Assert.assertEquals("222", registerBean2.getName());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 测试注销bean
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void unregisterBeanTest() {
|
||||||
|
registerTestAutoWired();
|
||||||
|
Assert.assertNotNull(SpringUtil.getBean("testAutoWired"));
|
||||||
|
SpringUtil.unregisterBean("testAutoWired1");
|
||||||
|
try {
|
||||||
|
SpringUtil.getBean("testAutoWired");
|
||||||
|
} catch (NoSuchBeanDefinitionException e) {
|
||||||
|
Assert.assertEquals(e.getClass(), NoSuchBeanDefinitionException.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试自动注入
|
* 测试自动注入
|
||||||
*/
|
*/
|
||||||
@Test
|
private void registerTestAutoWired() {
|
||||||
public void registerBeanTest2() {
|
|
||||||
TestAutoWired testAutoWired = new TestAutoWired();
|
TestAutoWired testAutoWired = new TestAutoWired();
|
||||||
TestBean testBean = new TestBean();
|
TestBean testBean = new TestBean();
|
||||||
testBean.setId("123");
|
testBean.setId("123");
|
||||||
@ -50,43 +66,11 @@ public class SpringUtilTest {
|
|||||||
testAutoWired = SpringUtil.getBean("testAutoWired");
|
testAutoWired = SpringUtil.getBean("testAutoWired");
|
||||||
Assert.assertNotNull(testAutoWired);
|
Assert.assertNotNull(testAutoWired);
|
||||||
Assert.assertNotNull(testAutoWired.getAutowiredBean());
|
Assert.assertNotNull(testAutoWired.getAutowiredBean());
|
||||||
|
Assert.assertNotNull(testAutoWired.getResourceBean());
|
||||||
Assert.assertEquals("123", testAutoWired.getAutowiredBean().getId());
|
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
|
@Test
|
||||||
public void getBeanTest(){
|
public void getBeanTest(){
|
||||||
final Demo2 testDemo = SpringUtil.getBean("testDemo");
|
final Demo2 testDemo = SpringUtil.getBean("testDemo");
|
||||||
@ -130,6 +114,9 @@ public class SpringUtilTest {
|
|||||||
@Autowired
|
@Autowired
|
||||||
// @Resource
|
// @Resource
|
||||||
private TestBean autowiredBean;
|
private TestBean autowiredBean;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private TestBean resourceBean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user