diff --git a/CHANGELOG.md b/CHANGELOG.md index bbcd3b523..4989e0187 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.7.0 (2021-06-12) +# 5.7.0 (2021-06-13) ### 🐣新特性 * 【jwt 】 添加JWT模块,实现了JWT的创建、解析和验证 @@ -16,6 +16,7 @@ ### 🐞Bug修复 * 【db 】 修复count方法丢失参数问题(issue#I3VBSL@Gitee) +* 【db 】 修复SpringUtil工具在`@PostConstruct` 注解标注的方法下失效问题(pr#341@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-cache/src/test/java/cn/hutool/cache/test/CacheConcurrentTest.java b/hutool-cache/src/test/java/cn/hutool/cache/CacheConcurrentTest.java similarity index 97% rename from hutool-cache/src/test/java/cn/hutool/cache/test/CacheConcurrentTest.java rename to hutool-cache/src/test/java/cn/hutool/cache/CacheConcurrentTest.java index c7e3bf8bf..22445b28b 100644 --- a/hutool-cache/src/test/java/cn/hutool/cache/test/CacheConcurrentTest.java +++ b/hutool-cache/src/test/java/cn/hutool/cache/CacheConcurrentTest.java @@ -1,6 +1,5 @@ -package cn.hutool.cache.test; +package cn.hutool.cache; -import cn.hutool.cache.Cache; import cn.hutool.cache.impl.FIFOCache; import cn.hutool.cache.impl.LRUCache; import cn.hutool.cache.impl.WeakCache; diff --git a/hutool-cache/src/test/java/cn/hutool/cache/test/CacheTest.java b/hutool-cache/src/test/java/cn/hutool/cache/CacheTest.java similarity index 92% rename from hutool-cache/src/test/java/cn/hutool/cache/test/CacheTest.java rename to hutool-cache/src/test/java/cn/hutool/cache/CacheTest.java index 1014ca524..1509cd56d 100644 --- a/hutool-cache/src/test/java/cn/hutool/cache/test/CacheTest.java +++ b/hutool-cache/src/test/java/cn/hutool/cache/CacheTest.java @@ -1,10 +1,9 @@ -package cn.hutool.cache.test; +package cn.hutool.cache; -import cn.hutool.cache.Cache; -import cn.hutool.cache.CacheUtil; import cn.hutool.cache.impl.TimedCache; import cn.hutool.core.date.DateUnit; import cn.hutool.core.thread.ThreadUtil; +import cn.hutool.core.util.RandomUtil; import org.junit.Assert; import org.junit.Test; @@ -34,6 +33,15 @@ public class CacheTest { Assert.assertNull(value1); } + @Test + public void fifoCacheCapacityTest(){ + Cache fifoCache = CacheUtil.newFIFOCache(100); + for (int i = 0; i < RandomUtil.randomInt(100, 1000); i++) { + fifoCache.put("key" + i, "value" + i); + } + Assert.assertEquals(100, fifoCache.size()); + } + @Test public void lfuCacheTest(){ Cache lfuCache = CacheUtil.newLFUCache(3); diff --git a/hutool-cache/src/test/java/cn/hutool/cache/test/FileCacheTest.java b/hutool-cache/src/test/java/cn/hutool/cache/FileCacheTest.java similarity index 91% rename from hutool-cache/src/test/java/cn/hutool/cache/test/FileCacheTest.java rename to hutool-cache/src/test/java/cn/hutool/cache/FileCacheTest.java index 9010e52e4..eaadb83db 100644 --- a/hutool-cache/src/test/java/cn/hutool/cache/test/FileCacheTest.java +++ b/hutool-cache/src/test/java/cn/hutool/cache/FileCacheTest.java @@ -1,4 +1,4 @@ -package cn.hutool.cache.test; +package cn.hutool.cache; import org.junit.Assert; import org.junit.Test; diff --git a/hutool-extra/src/main/java/cn/hutool/extra/spring/SpringUtil.java b/hutool-extra/src/main/java/cn/hutool/extra/spring/SpringUtil.java index ecb51dec2..0a0fdd097 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/spring/SpringUtil.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/spring/SpringUtil.java @@ -3,6 +3,7 @@ package cn.hutool.extra.spring; import cn.hutool.core.lang.TypeReference; import cn.hutool.core.util.ArrayUtil; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.ApplicationContext; @@ -29,10 +30,13 @@ import java.util.Map; public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextAware { /** - * Spring应用上下文环境 + * "@PostConstruct"注解标记的类中,由于ApplicationContext还未加载,导致空指针
+ * 因此实现BeanFactoryPostProcessor注入ConfigurableListableBeanFactory实现bean的操作 */ private static ConfigurableListableBeanFactory beanFactory; - + /** + * Spring应用上下文环境 + */ private static ApplicationContext applicationContext; @Override @@ -46,14 +50,24 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA } /** - * 获取applicationContext + * 获取{@link ApplicationContext} * - * @return ApplicationContext + * @return {@link ApplicationContext} */ public static ApplicationContext getApplicationContext() { return applicationContext; } + /** + * 获取{@link ListableBeanFactory},可能为{@link ConfigurableListableBeanFactory} 或 {@link ApplicationContextAware} + * + * @return {@link ListableBeanFactory} + * @since 5.7.0 + */ + public static ListableBeanFactory getBeanFactory() { + return null == beanFactory ? applicationContext : beanFactory; + } + //通过name获取 Bean. /** @@ -65,7 +79,7 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA */ @SuppressWarnings("unchecked") public static T getBean(String name) { - return (T) beanFactory.getBean(name); + return (T) getBeanFactory().getBean(name); } /** @@ -76,7 +90,7 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA * @return Bean对象 */ public static T getBean(Class clazz) { - return beanFactory.getBean(clazz); + return getBeanFactory().getBean(clazz); } /** @@ -88,7 +102,7 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA * @return Bean对象 */ public static T getBean(String name, Class clazz) { - return beanFactory.getBean(name, clazz); + return getBeanFactory().getBean(name, clazz); } /** @@ -104,7 +118,7 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA final ParameterizedType parameterizedType = (ParameterizedType) reference.getType(); final Class rawType = (Class) parameterizedType.getRawType(); final Class[] genericTypes = Arrays.stream(parameterizedType.getActualTypeArguments()).map(type -> (Class) type).toArray(Class[]::new); - final String[] beanNames = beanFactory.getBeanNamesForType(ResolvableType.forClassWithGenerics(rawType, genericTypes)); + final String[] beanNames = getBeanFactory().getBeanNamesForType(ResolvableType.forClassWithGenerics(rawType, genericTypes)); return getBean(beanNames[0], rawType); } @@ -117,7 +131,7 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA * @since 5.3.3 */ public static Map getBeansOfType(Class type) { - return beanFactory.getBeansOfType(type); + return getBeanFactory().getBeansOfType(type); } /** @@ -128,7 +142,7 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA * @since 5.3.3 */ public static String[] getBeanNamesForType(Class type) { - return beanFactory.getBeanNamesForType(type); + return getBeanFactory().getBeanNamesForType(type); } /** @@ -139,6 +153,9 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA * @since 5.3.3 */ public static String getProperty(String key) { + if(null == applicationContext){ + return null; + } return applicationContext.getEnvironment().getProperty(key); } @@ -149,6 +166,9 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA * @since 5.3.3 */ public static String[] getActiveProfiles() { + if(null == applicationContext){ + return null; + } return applicationContext.getEnvironment().getActiveProfiles(); } @@ -175,7 +195,12 @@ public class SpringUtil implements BeanFactoryPostProcessor, ApplicationContextA * @since 5.4.2 */ public static void registerBean(String beanName, T bean) { - beanFactory.registerSingleton(beanName, bean); + if(null != beanFactory){ + beanFactory.registerSingleton(beanName, bean); + } else if(applicationContext instanceof ConfigurableApplicationContext){ + ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext; + context.getBeanFactory().registerSingleton(beanName, bean); + } } } diff --git a/hutool-jwt/pom.xml b/hutool-jwt/pom.xml index 7f3ccdd05..0d6e1b90c 100644 --- a/hutool-jwt/pom.xml +++ b/hutool-jwt/pom.xml @@ -12,7 +12,7 @@ hutool-jwt ${project.artifactId} - Hutool JWT生成、解析和验证实现 + JWT生成、解析和验证实现 @@ -30,6 +30,7 @@ hutool-crypto ${project.parent.version} + org.bouncycastle bcprov-jdk15to18 diff --git a/hutool-jwt/src/test/java/cn/hutool/jwt/JWTSignerTest.java b/hutool-jwt/src/test/java/cn/hutool/jwt/JWTSignerTest.java index e391a4e00..21205a9bf 100644 --- a/hutool-jwt/src/test/java/cn/hutool/jwt/JWTSignerTest.java +++ b/hutool-jwt/src/test/java/cn/hutool/jwt/JWTSignerTest.java @@ -116,6 +116,6 @@ public class JWTSignerTest { .setSigner(signer); String token = jwt.sign(); - Assert.assertTrue(JWT.of(token).setSigner(signer).verify()); + Assert.assertTrue(JWT.of(token).verify(signer)); } }