All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.woodwhales.common.util.SpringUtil Maven / Gradle / Ivy

There is a newer version: 3.8.3
Show newest version
package cn.woodwhales.common.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Spring(Spring boot)工具封装,包括:
 *
 * 
 *     1、Spring IOC容器中的bean对象获取
 * 
* * @author loolly * @since 5.1.0 */ @Component public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) { SpringUtil.applicationContext = applicationContext; } /** * 获取applicationContext * * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { return applicationContext; } //通过name获取 Bean. /** * 通过name获取 Bean * * @param Bean类型 * @param name Bean名称 * @return Bean */ @SuppressWarnings("unchecked") public static T getBean(String name) { return (T) applicationContext.getBean(name); } /** * 通过class获取Bean * * @param Bean类型 * @param clazz Bean类 * @return Bean对象 */ public static T getBean(Class clazz) { return applicationContext.getBean(clazz); } /** * 通过name,以及Clazz返回指定的Bean * * @param bean类型 * @param name Bean名称 * @param clazz bean类型 * @return Bean对象 */ public static T getBean(String name, Class clazz) { return applicationContext.getBean(name, clazz); } /** * 获取指定类型对应的所有Bean,包括子类 * * @param Bean类型 * @param type 类、接口,null表示获取所有bean * @return 类型对应的bean,key是bean注册的name,value是Bean * @since 5.3.3 */ public static Map getBeansOfType(Class type) { return applicationContext.getBeansOfType(type); } /** * 获取指定类型对应的Bean名称,包括子类 * * @param type 类、接口,null表示获取所有bean名称 * @return bean名称 * @since 5.3.3 */ public static String[] getBeanNamesForType(Class type) { return applicationContext.getBeanNamesForType(type); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy