Spring工具类,获取Spring容器中的Bean

xiaoxiao2021-02-27  116

package com.ls . util ; import org.springframework.beans.BeansException ; import org.springframework.context.ApplicationContext ; import org.springframework.context.ApplicationContextAware ; /** * Spring工具类,获取Spring容器中的Bean * @author ls * */ public class SpringUtil implements ApplicationContextAware { private static ApplicationContext ctx ; /** * 获取bean * @param id * @return */ @SuppressWarnings ( "unchecked" ) public static < T > T getBean ( String id ){ return ( T ) ctx . getBean ( id ); } /** * 按类型获取bean * @param clazz * @return */ public static < T > T getBean ( Class < T > clazz ){ return ctx . getBean ( clazz ); } /** * 按类型及ID获取bean * @param id * @param clazz * @return */ public static < T > T getBean ( String id , Class < T > clazz ){ return ctx . getBean ( id , clazz ); } /** * * 检查SpringUtil是否已完成初始化 * @param * @return boolean * @throws */ public static boolean isInited (){ return null != ctx ; } @Override public void setApplicationContext ( ApplicationContext applicationContext ) throws BeansException { ctx = applicationContext ; } }
转载请注明原文地址: https://www.6miu.com/read-14472.html

最新回复(0)