1. package com.yiban.abc.util;
  2.  
  3. import org.springframework.beans.BeansException;
  4. import org.springframework.beans.factory.BeanFactory;
  5. import org.springframework.beans.factory.BeanFactoryAware;
  6. import org.springframework.beans.factory.support.DefaultListableBeanFactory;
  7. import org.springframework.context.annotation.Configuration;
  8.  
  9. import java.util.Map;
  10.  
  11. @Configuration
  12. public class SpringBeanUtil implements BeanFactoryAware {
  13. private static BeanFactory beanFactory;
  14. private static DefaultListableBeanFactory listtableBeanFactory;
  15.  
  16. public SpringBeanUtil() {
  17. }
  18.  
  19. public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
  20. SpringBeanUtil.beanFactory = beanFactory;
  21. listtableBeanFactory = (DefaultListableBeanFactory)beanFactory;
  22. }
  23.  
  24. public static Object getBean(String name) throws BeansException {
  25. return beanFactory.getBean(name);
  26. }
  27.  
  28. public static <T> T getBean(Class<T> requiredType) throws BeansException {
  29. return beanFactory.getBean(requiredType);
  30. }
  31.  
  32. public static <T> T getBean(String name, Class<T> requiredType) throws BeansException {
  33. return beanFactory.getBean(name, requiredType);
  34. }
  35.  
  36. public static <T> Map<String, T> getBeansOfType(Class<T> requiredType) throws BeansException {
  37. return listtableBeanFactory.getBeansOfType(requiredType);
  38. }
  39. }

不需要spring管理,自己根据名字取到对应的bean的更多相关文章

  1. spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常

    java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not al ...

  2. Spring 管理数据源

    Spring 管理数据源 不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的.在以往的应用中,数据源一般是Web应用服务器提供的.在Spring中,你不 ...

  3. SpringContextHolder 静态持有SpringContext的引用(如何取得Spring管理的bean )

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  4. 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer

     spring管理属性配置文件properties--使用PropertiesFactoryBean 对于属性配置,一般采用的是键值对的形式,如:key=value属性配置文件一般使用的是XXX.pr ...

  5. 关于Spring管理的类如何创建对象

    今天项目中出现了空指针错误,其实一看这个错误我就知道是哪里错了.而且以前也总是说,没有真正的改过啊.今天把改进方法和大家共享.现在我们的项目中,大多数我们的管理方式都是交由Spring去管理,至于好处 ...

  6. Spring核心技术(九)——Spring管理的组件和Classpath扫描

    Spring管理的组件和Classpath的扫描 在前文描述中使用到的Spring中的Bean的定义,都是通过指定的XML来配置的.而前文中描述的注解的解析则是在源代码级别来提供配置元数据的.在那些例 ...

  7. Mybaits 源码解析 (十二)----- Mybatis的事务如何被Spring管理?Mybatis和Spring事务中用的Connection是同一个吗?

    不知道一些同学有没有这种疑问,为什么Mybtis中要配置dataSource,Spring的事务中也要配置dataSource?那么Mybatis和Spring事务中用的Connection是同一个吗 ...

  8. 把对象交给spring管理的3种方法及经典应用

    背景 先说一说什么叫把对象交给spring管理.它区别于把类交给spring管理.在spring里采用注解方式@Service.@Component这些,实际上管理的是类,把这些类交给spring来负 ...

  9. Spring管理 hibernate 事务配置的五种方式

    Spring配置文件中关于事务配置总是由三个组成部分,DataSource.TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块! 首先我创建了两个类 ...

随机推荐

  1. 【源码解析】凭什么?spring boot 一个 jar 就能开发 web 项目

    问题 为什么开发web项目,spring-boot-starter-web 一个jar就搞定了?这个jar做了什么? 通过 spring-boot 工程可以看到所有开箱即用的的引导模块 spring- ...

  2. LeetCode初级算法--链表01:反转链表

    LeetCode初级算法--链表01:反转链表 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...

  3. 代码审计-凡诺CMS 2.1文件包含漏洞

    0x01代码审计 后台账号密码: admin admin 安装好了是这样的 漏洞文件:/channel.php if (ism()) { include($dir.$t_mpath.$c_mcmode ...

  4. javascript 对象和字符串互转

    Object  =>  String : console.log(JSON.stringify(e)); String => Object : JSON.parse(str)

  5. dos命令创建批处理脚本

    win+r  打开cmd 输入 copy con  1.bat 回车 进入编辑状态输入 @echo off echo xxxx Ctrl+z 结束编辑   会在当前目录生成一个bat文件

  6. Python编程系列---使用装饰器传参+字典实现动态路由

    # 实现一个空路由表,利用装饰器将url和功能函数的对应关系自动存到这个字典中 router_dict = {} # 定义一个装饰器 # 再给一层函数定义,用来传入一个参数,这个参数就是访问的页面地址 ...

  7. 楼上请让路 RoarCTF2019 Writeup

    Misc 签到题 RoarCTF{签到!!!} 黄金六年 文件尾部有一段base64,解码为16进制可以看到是一个压缩包 打开压缩包需要密码 使用pr抽帧 可以看到部分帧中有二维码,依次扫码即可得到k ...

  8. 死磕 java线程系列之线程池深入解析——生命周期

    (手机横屏看源码更方便) 注:java源码分析部分如无特殊说明均基于 java8 版本. 注:线程池源码部分如无特殊说明均指ThreadPoolExecutor类. 简介 上一章我们一起重温了下线程的 ...

  9. [AHOI2002]哈利·波特与魔法石

    这道题比较简单,就是一个最短路(SSSP).数据水,用Floyd即可AC.这里用了Dijkstra. #include <iostream> #include <cstdio> ...

  10. vue.js 使用 vue-router 修改页面标题

    module.exports = { name: 'myComponent', data: {} route{ data: function(){ document.title = "页面标 ...