zoukankan      html  css  js  c++  java
  • 获取spring bean

    package com.git.utils;
    
    import java.util.Map;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
    @SuppressWarnings("all")
    public class SpringContextUtils implements ApplicationContextAware {
        private static ApplicationContext applicationContext;
       
        @Autowired
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }
     
        /**
         * 获取applicationContext对象
         * @return
         */
        public static ApplicationContext getApplicationContext(){
            return applicationContext;
        }
         
        /**
         * 根据bean的id来查找对象
         * @param id
         * @return
         */
        public static Object getBeanById(String id){
            return applicationContext.getBean(id);
        }
         
        /**
         * 根据bean的class来查找对象
         * @param c
         * @return
         */
        public static Object getBeanByClass(Class c){
            return applicationContext.getBean(c);
        }
         
        /**
         * 根据bean的class来查找所有的对象(包括子类)
         * @param c
         * @return
         */
        public static Map getBeansByClass(Class c){
            return applicationContext.getBeansOfType(c);
        }
    }
  • 相关阅读:
    redis 高级
    redis连接
    redis脚本
    Redis事务
    redis发布订阅
    加一
    C语言从代码中加载动态链接库
    GCC编译器
    Vim编辑器基础
    Linux用户的创建与授权及修改密码
  • 原文地址:https://www.cnblogs.com/Jeely/p/13168570.html
Copyright © 2011-2022 走看看