當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring的工具类,方便在非spring管理环境中获取bean
生活随笔
收集整理的這篇文章主要介紹了
Spring的工具类,方便在非spring管理环境中获取bean
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
在SpringBoot的后臺項目中,如果想要引入并且調用某個bean,可以直接通過注解的方式。
比如在單元測試中引入某業務的Controller
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public class TestBcgl {@Autowiredprivate KqBcglController kqBcglController;或者在Controller中引入service
@RestController @RequestMapping("/kqgl/bcgl") public class KqBcglController extends BaseController {@Autowiredprivate IKqBcglService kqBcglService;但是如果想要再某個工具類中的靜態工具方法中調用某個bean的方法,即在非spring管理環境中獲取bean的話,應該怎么使用。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
比如現在有一個spring redis的工具類
@Component public class RedisCache現在需要在某個工具類DictUtils中,獲取不受spring管理中獲取上面的RedisCache這個bean
public class DictUtils {/*** 獲取字典緩存** @param key 參數鍵* @return dictDatas 字典數據列表*/public static List<SysDictData> getDictCache(String key){Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));if (StringUtils.isNotNull(cacheObj)){List<SysDictData> DictDatas = StringUtils.cast(cacheObj);return DictDatas;}return null;}根據上面的SpringUtils.getBean(RedisCache.class)就能獲取,那么這個SpringUtils就是封裝的
Spring? 工具類
import org.springframework.aop.framework.AopContext; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.stereotype.Component;/*** spring工具類 方便在非spring管理環境中獲取bean**/ @Component public final class SpringUtils implements BeanFactoryPostProcessor {/** Spring應用上下文環境 */private static ConfigurableListableBeanFactory beanFactory;@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException{SpringUtils.beanFactory = beanFactory;}/*** 獲取對象** @param name* @return Object 一個以所給名字注冊的bean的實例* @throws org.springframework.beans.BeansException**/@SuppressWarnings("unchecked")public static <T> T getBean(String name) throws BeansException{return (T) beanFactory.getBean(name);}/*** 獲取類型為requiredType的對象** @param clz* @return* @throws org.springframework.beans.BeansException**/public static <T> T getBean(Class<T> clz) throws BeansException{T result = (T) beanFactory.getBean(clz);return result;}/*** 如果BeanFactory包含一個與所給名稱匹配的bean定義,則返回true** @param name* @return boolean*/public static boolean containsBean(String name){return beanFactory.containsBean(name);}/*** 判斷以給定名字注冊的bean定義是一個singleton還是一個prototype。 如果與給定名字相應的bean定義沒有被找到,將會拋出一個異常(NoSuchBeanDefinitionException)** @param name* @return boolean* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException**/public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException{return beanFactory.isSingleton(name);}/*** @param name* @return Class 注冊對象的類型* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException**/public static Class<?> getType(String name) throws NoSuchBeanDefinitionException{return beanFactory.getType(name);}/*** 如果給定的bean名字在bean定義中有別名,則返回這些別名** @param name* @return* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException**/public static String[] getAliases(String name) throws NoSuchBeanDefinitionException{return beanFactory.getAliases(name);}/*** 獲取aop代理對象** @param invoker* @return*/@SuppressWarnings("unchecked")public static <T> T getAopProxy(T invoker){return (T) AopContext.currentProxy();} }?
總結
以上是生活随笔為你收集整理的Spring的工具类,方便在非spring管理环境中获取bean的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot+Vue+Redis
- 下一篇: SpringBoot中操作spring