不需要spring管理,自己根据名字取到对应的bean
package com.yiban.abc.util; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.Configuration; import java.util.Map; @Configuration
public class SpringBeanUtil implements BeanFactoryAware {
private static BeanFactory beanFactory;
private static DefaultListableBeanFactory listtableBeanFactory; public SpringBeanUtil() {
} public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
SpringBeanUtil.beanFactory = beanFactory;
listtableBeanFactory = (DefaultListableBeanFactory)beanFactory;
} public static Object getBean(String name) throws BeansException {
return beanFactory.getBean(name);
} public static <T> T getBean(Class<T> requiredType) throws BeansException {
return beanFactory.getBean(requiredType);
} public static <T> T getBean(String name, Class<T> requiredType) throws BeansException {
return beanFactory.getBean(name, requiredType);
} public static <T> Map<String, T> getBeansOfType(Class<T> requiredType) throws BeansException {
return listtableBeanFactory.getBeansOfType(requiredType);
}
}
不需要spring管理,自己根据名字取到对应的bean的更多相关文章
- 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 ...
- Spring 管理数据源
Spring 管理数据源 不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的.在以往的应用中,数据源一般是Web应用服务器提供的.在Spring中,你不 ...
- 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 ...
- 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
spring管理属性配置文件properties--使用PropertiesFactoryBean 对于属性配置,一般采用的是键值对的形式,如:key=value属性配置文件一般使用的是XXX.pr ...
- 关于Spring管理的类如何创建对象
今天项目中出现了空指针错误,其实一看这个错误我就知道是哪里错了.而且以前也总是说,没有真正的改过啊.今天把改进方法和大家共享.现在我们的项目中,大多数我们的管理方式都是交由Spring去管理,至于好处 ...
- Spring核心技术(九)——Spring管理的组件和Classpath扫描
Spring管理的组件和Classpath的扫描 在前文描述中使用到的Spring中的Bean的定义,都是通过指定的XML来配置的.而前文中描述的注解的解析则是在源代码级别来提供配置元数据的.在那些例 ...
- Mybaits 源码解析 (十二)----- Mybatis的事务如何被Spring管理?Mybatis和Spring事务中用的Connection是同一个吗?
不知道一些同学有没有这种疑问,为什么Mybtis中要配置dataSource,Spring的事务中也要配置dataSource?那么Mybatis和Spring事务中用的Connection是同一个吗 ...
- 把对象交给spring管理的3种方法及经典应用
背景 先说一说什么叫把对象交给spring管理.它区别于把类交给spring管理.在spring里采用注解方式@Service.@Component这些,实际上管理的是类,把这些类交给spring来负 ...
- Spring管理 hibernate 事务配置的五种方式
Spring配置文件中关于事务配置总是由三个组成部分,DataSource.TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块! 首先我创建了两个类 ...
随机推荐
- [Luogu1379]八数码难题
题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了 ...
- opencv::图像上采样和降采样
图像金字塔概念 . 我们在图像处理中常常会调整图像大小,最常见的就是放大(zoom in)和缩小(zoom out),尽管几何变换也可以实现图像放大和缩小,但是这里我们介绍图像金字塔 . 一个图像金字 ...
- linux自启动脚本.sh
while [ 1 ]; do PRO_NUM=`ps -ef | grep "cms$" | grep -v "grep" | wc ...
- 《Java并发编程实战》读书笔记-第3章 对象的共享
可见性 在没有同步的情况下,编译器.处理器以及运行时都可能做指令重排.执行结果可能会出现错误 volatile变量 编译器与运行时不会进行指令重排,不会进行缓存,使用volatile变量要满足以下条件 ...
- windows 系统对应的内核版本和自带 iis 版本
Windows 10 10.0* Windows Server 2016 10.0* Windows 8.1 6.3* Windows Server 2012 R2 6.3* Windows 8 6. ...
- python中如何通过报错信息定位问题(异常传播轨迹)
class SelfException(Exception): pass def main(): firstMethod() def firstMethod(): secondMethod() def ...
- 重装系统快速恢复使用习惯(各种图标摆放位置、环境变量、C盘可能存放的文件备份、编辑器配置、语言环境导出)
目录 电脑重装系统注意点 - 尽可能快速恢复你熟悉的 windows 基本使用场景 最好截图的点 常见文件拷贝 入口 查看桌面图标.快捷方式有哪些 程序员大合集 环境变量相关 pycharm 快捷键等 ...
- (记录)Ubuntu系统中运行需要导入jar包的Java程序
在学习Redis的过程中,在学到Redis客户端Jedis的时候,考虑到能不能在ubuntu下用Vim编写Java程序并且能够运行呢? 于是,首先在网上调研了一番用Vim写Java程序的可实现性. 相 ...
- Python enumerate() 函数笔记
enumerate函数说明: 函数原型:enumerate(sequence, [start=0]) #第二个参数为指定索引 功能:将可循环序列sequence以start开始分别列出序列数据和数据 ...
- 创建WebApi
一.创建 Web 项目 使用vs创建项目,选择“ASP.NET Core Web 应用程序”模板,将项目命名为 TodoApi,然后单击“确定”. 在“新建 ASP.NET Core Web 应用程序 ...