Java tutorial
/* * Copyright 2015-2020 Fengduo.com All right reserved. This software is the confidential and proprietary information of * Fengduo.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only * in accordance with the terms of the license agreement you entered into with Fengduo.com. */ package com.fengduo.bee.commons.core; import java.util.Map; 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.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * @author zxc May 28, 2015 5:24:45 PM */ @Component public class SpringContextAware implements ApplicationContextAware, BeanFactoryPostProcessor { private static ConfigurableListableBeanFactory beanFactory; private static ApplicationContext appContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextAware.appContext = applicationContext; } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { SpringContextAware.beanFactory = beanFactory; } /** * ? * * @param name * @return Object ??bean * @throws org.springframework.beans.BeansException */ @SuppressWarnings("unchecked") public static <T> T getBeanByFactory(String name) throws BeansException { return (T) beanFactory.getBean(name); } public static <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException { return appContext.getBeansOfType(type); } /** * ?requiredType * * @param clz * @return * @throws org.springframework.beans.BeansException */ public static <T> T getBean(Class<T> clz) throws BeansException { return (T) beanFactory.getBean(clz); } /** * BeanFactory????beantrue * * @param name * @return boolean */ public static boolean containsBean(String name) { return beanFactory.containsBean(name); } /** * ??beansingletonprototype ??beanNoSuchBeanDefinitionException * * @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); } public static ApplicationContext getApplicationContext() { return appContext; } public static Object getBean(String name) throws BeansException { return appContext.getBean(name); } }