1 package com.example.demo1.utils.spring; 2 3 import org.springframework.aop.framework.AopContext; 4 import org.springframework.beans.BeansException; 5 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 6 import org.springframework.beans.factory.config.BeanFactoryPostProcessor; 7 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 8 import org.springframework.context.ApplicationContext; 9 import org.springframework.context.ApplicationContextAware; 10 import org.springframework.stereotype.Component; 11 import org.springframework.util.StringUtils; 12 13 /** 14 * spring工具类 方便在非spring管理环境中获取bean 15 * 16 * @author ruoyi 17 */ 18 @Component 19 public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware 20 { 21 /** Spring应用上下文环境 */ 22 private static ConfigurableListableBeanFactory beanFactory; 23 24 private static ApplicationContext applicationContext; 25 26 @Override 27 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException 28 { 29 SpringUtils.beanFactory = beanFactory; 30 } 31 32 @Override 33 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException 34 { 35 SpringUtils.applicationContext = applicationContext; 36 } 37 38 /** 39 * 获取对象 40 * 41 * @param name 42 * @return Object 一个以所给名字注册的bean的实例 43 * @throws BeansException 44 * 45 */ 46 @SuppressWarnings("unchecked") 47 public static <T> T getBean(String name) throws BeansException 48 { 49 return (T) beanFactory.getBean(name); 50 } 51 52 /** 53 * 获取类型为requiredType的对象 54 * 55 * @param clz 56 * @return 57 * @throws BeansException 58 * 59 */ 60 public static <T> T getBean(Class<T> clz) throws BeansException 61 { 62 T result = (T) beanFactory.getBean(clz); 63 return result; 64 } 65 66 /** 67 * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true 68 * 69 * @param name 70 * @return boolean 71 */ 72 public static boolean containsBean(String name) 73 { 74 return beanFactory.containsBean(name); 75 } 76 77 /** 78 * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException) 79 * 80 * @param name 81 * @return boolean 82 * @throws NoSuchBeanDefinitionException 83 * 84 */ 85 public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException 86 { 87 return beanFactory.isSingleton(name); 88 } 89 90 /** 91 * @param name 92 * @return Class 注册对象的类型 93 * @throws NoSuchBeanDefinitionException 94 * 95 */ 96 public static Class<?> getType(String name) throws NoSuchBeanDefinitionException 97 { 98 return beanFactory.getType(name); 99 } 100 101 /** 102 * 如果给定的bean名字在bean定义中有别名,则返回这些别名 103 * 104 * @param name 105 * @return 106 * @throws NoSuchBeanDefinitionException 107 * 108 */ 109 public static String[] getAliases(String name) throws NoSuchBeanDefinitionException 110 { 111 return beanFactory.getAliases(name); 112 } 113 114 /** 115 * 获取aop代理对象 116 * 117 * @param invoker 118 * @return 119 */ 120 @SuppressWarnings("unchecked") 121 public static <T> T getAopProxy(T invoker) 122 { 123 return (T) AopContext.currentProxy(); 124 } 125 126 /** 127 * 获取当前的环境配置,无配置返回null 128 * 129 * @return 当前的环境配置 130 */ 131 public static String[] getActiveProfiles() 132 { 133 return applicationContext.getEnvironment().getActiveProfiles(); 134 } 135 136 /** 137 * 获取当前的环境配置,当有多个环境配置时,只获取第一个 138 * 139 * @return 当前的环境配置 140 */ 141 public static String getActiveProfile() 142 { 143 final String[] activeProfiles = getActiveProfiles(); 144 return StringUtils.isEmpty(activeProfiles) ? activeProfiles[0] : null; 145 } 146 147 /** 148 * 获取配置文件中的值 149 * 150 * @param key 配置文件的key 151 * @return 当前的配置文件的值 152 * 153 */ 154 public static String getRequiredProperty(String key) 155 { 156 return applicationContext.getEnvironment().getRequiredProperty(key); 157 } 158 159 }
标签:return,name,Spring,throws,static,Java,工具,public,String From: https://www.cnblogs.com/luyj00436/p/18502318