Class SpringUtil

java.lang.Object
cn.sinozg.applet.common.utils.SpringUtil
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.config.BeanFactoryPostProcessor, org.springframework.context.ApplicationContextAware

@Component public final class SpringUtil extends Object implements org.springframework.beans.factory.config.BeanFactoryPostProcessor, org.springframework.context.ApplicationContextAware
spring工具类 方便在非spring管理环境中获取bean
  • Field Details

    • beanFactory

      private static org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory
      Spring应用上下文环境
    • context

      private static org.springframework.context.ApplicationContext context
  • Constructor Details

    • SpringUtil

      public SpringUtil()
  • Method Details

    • postProcessBeanFactory

      public void postProcessBeanFactory(@NotNull @NotNull org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory) throws org.springframework.beans.BeansException
      Specified by:
      postProcessBeanFactory in interface org.springframework.beans.factory.config.BeanFactoryPostProcessor
      Throws:
      org.springframework.beans.BeansException
    • setApplicationContext

      public void setApplicationContext(@NotNull @NotNull org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException
      Specified by:
      setApplicationContext in interface org.springframework.context.ApplicationContextAware
      Throws:
      org.springframework.beans.BeansException
    • getApplicationContext

      public static org.springframework.context.ApplicationContext getApplicationContext()
      获取上下文
      Returns:
      上下文
    • getBean

      public static <T> T getBean(String name) throws org.springframework.beans.BeansException
      获取对象
      Parameters:
      name - bean名称
      Returns:
      Object 一个以所给名字注册的bean的实例
      Throws:
      org.springframework.beans.BeansException - Exception
    • getBean

      public static <T> T getBean(Class<T> clazz) throws org.springframework.beans.BeansException
      获取类型为requiredType的对象
      Parameters:
      clazz - bean类型
      Returns:
      对象
      Throws:
      org.springframework.beans.BeansException - Exception
    • containsBean

      public static boolean containsBean(String name)
      如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
      Parameters:
      name - bean名称
      Returns:
      boolean
    • isSingleton

      public static boolean isSingleton(String name) throws org.springframework.beans.factory.NoSuchBeanDefinitionException
      判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
      Parameters:
      name - bean名称
      Returns:
      boolean
      Throws:
      org.springframework.beans.factory.NoSuchBeanDefinitionException - Exception
    • getType

      public static Class<?> getType(String name) throws org.springframework.beans.factory.NoSuchBeanDefinitionException
      Parameters:
      name - bean名称
      Returns:
      Class 注册对象的类型
      Throws:
      org.springframework.beans.factory.NoSuchBeanDefinitionException - Exception
    • getAliases

      public static String[] getAliases(String name) throws org.springframework.beans.factory.NoSuchBeanDefinitionException
      如果给定的bean名字在bean定义中有别名,则返回这些别名
      Parameters:
      name - bean名称
      Returns:
      结果
      Throws:
      org.springframework.beans.factory.NoSuchBeanDefinitionException - Exception
    • getAopProxy

      public static <T> T getAopProxy(T invoker)
      获取aop代理对象
      Parameters:
      invoker - invoker
      Returns:
      对象
    • getActiveProfiles

      public static String[] getActiveProfiles()
      获取当前的环境配置,无配置返回null
      Returns:
      当前的环境配置
    • getActiveProfile

      public static String getActiveProfile()
      获取当前的环境配置,当有多个环境配置时,只获取第一个
      Returns:
      当前的环境配置
    • beansOfType

      public static <T> Map<String,T> beansOfType(Class<T> clazz)
      获取bean
      Type Parameters:
      T - 类型
      Parameters:
      clazz - 类型
      Returns:
      实例
    • beanOfType

      public static <T, R> R beanOfType(T type, Class<?> clazz, Map<T,R> map, Function<R,T> fun)
      从多个实现类获取到bean
      Type Parameters:
      T - 类型
      R - 实例
      Parameters:
      type - 类型
      clazz - 类
      map - 缓存map
      fun - 获取类型的函数
      Returns:
      实例
    • beanOfType

      public static <T, R> R beanOfType(T type, Class<?> clazz, Map<T,R> map, Function<R,T> fun, Predicate<T> predicate)
      从多个实现类获取到bean
      Type Parameters:
      T - 类型
      R - 实例
      Parameters:
      type - 类型
      clazz - 类
      map - 缓存map
      fun - 获取类型的函数
      predicate - 判断相等的函数
      Returns:
      实例
    • beanOfNmTp

      public static <T> T beanOfNmTp(String name, Class<T> clazz)
      获取bean
      Type Parameters:
      T - 类型
      Parameters:
      name - 名称
      clazz - 类型
      Returns:
      实例
    • registerBean

      public static <T> T registerBean(String beanName, Class<T> clazz, Object... args)
      动态注册bean
      Type Parameters:
      T - 对象
      Parameters:
      beanName - bean的名称
      clazz - 对象类型
      args - 参数
      Returns:
      bean