【Spring】手动获取spring容器对象时,报no qualifying bean of type is defined
手动获取容器对象时,报no qualifying bean of type is defined,
经过调查,发现手动获取的时候,该类所在的包必须经过spring容器初始化。
1.SpringConfigHolder 代码,添加@Component进行ioc
package com.xxxxxxx.utils.holder; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; /**
* spring工具类,通过ApplicationContext.getBean获取注册的bean
*
*/
@Component
public class SpringConfigHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringConfigHolder.applicationContext = applicationContext;
} /**
* 获取spring的bean
* */
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
} }
2.配置springmvc-servlet.xml,自动扫描指定包进行ioc
<context:component-scan base-package="com.xxxxxxx.utils.holder" />
3.这样在通过SpringConfigHolder.getBean(XXXX.class)的时候就不会出现no qualifying bean of type is defined了。
【Spring】手动获取spring容器对象时,报no qualifying bean of type is defined的更多相关文章
- Spring Task Scheduler - No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
1. Overview In this article, we are discussing the Springorg.springframework.beans.factory.NoSuchBea ...
- 配置Spring的用于初始化容器对象的监听器
<!-- 配置Spring的用于初始化容器对象的监听器 --> <listener> <listener-class>org.springframework.web ...
- spring Boot异步操作报错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.self.spring.springboot.Jeep' available
我也是最近开始学习Spring Boot,在执行异步操作的时候总是汇报如下的错误: Exception in thread "main" org.springframework.b ...
- javascript客户端遍历控件与获取父容器对象
javascript客户端遍历控件与获取父容器对象示例代码 1,遍历也面中所有的控件function findControlAll() { var inputs=document. ...
- Spring mvc 报错:No qualifying bean of type [java.lang.String] found for dependency:
具体错误: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean w ...
- spring cloud gateway网关启动报错:No qualifying bean of type 'org.springframework.web.reactive.DispatcherHandler'
网关配置好后启动报错如下: org.springframework.context.ApplicationContextException: Unable to start web server; n ...
- Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined(转)
最近项目里面,用了spring的定时任务,一直以来,项目运行的不错.定时器也能正常使用.可是,今天启动项目测试的时候,盯着启动Log看了一阵子,突然间发现,启动的Log中居然有一个异常,虽然一闪而过, ...
- Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined stac ...
- spring注入时报错::No qualifying bean of type 'xxx.xxMapper'
做一个小项目,因为有 baseService,所以偷懒就没有写单独的每个xxService接口,直接写的xxServiceImpl,结果在service实现类中注入Mapper的时候,用的 @Auto ...
随机推荐
- Dockerfile
http://blog.csdn.net/jiashiwen/article/details/48806243 一:如何使用: Dockerfile 用来创建一个自定义的image,包含了用户指定的软 ...
- ObjectiveC 文件操作一
1,引用和使用文件 NSFileManager 是一个单例对象,在mac应用中可以获取任何地址,在IOS中获取的是相对应的应用程序的地址.可以使用 defaultManager 来得到当前应用程序地址 ...
- MySQL Binlog的介绍
binlog基本定义:二进制日志,也成为二进制日志,记录对数据发生或潜在发生更改的SQL语句,并以二进制的形式保存在磁盘中: 作用:MySQL的作用类似于Oracle的归档日志,可以用来查看数据库的变 ...
- Codeforces Round#1
A. Theatre Square 题目大意:有一个长宽为m和n的广场,用边长为a的正方形去铺盖,问铺满最少需要多少正方形 题解:题目分解为用长度为a的线条分别去覆盖长度为m和n的线条,计算两者的乘积 ...
- appledoc:Objective-C注释文档生成工具
appledoc是帮助Objective-C开发者从特殊格式的源代码注释中生成类似apple资源代码帮助文档的命令行工具. 安装和使用都非常简单: 安装 git clone git://github. ...
- UI基础:UILabel.UIFont
UILabel:标签 继承自UIView ,在UIView基础上扩充了显示文本的功能.(文本框) UILabel的使用步骤 1.创建控件 UILabel *aLabel=[[UILabel alloc ...
- Ubuntu 用 pptp 建立 vpn 服务
1.下载pptp sudo apt-get install pptpd 2.配置pptp 须要改动配置下面的文件: pptpd.conf文件:配置链接后的主机ip和能够分配的内存范围 vi /etc/ ...
- mac的svn之cornerstone简易教程
链接地址:http://jingyan.baidu.com/article/9989c74612a55af648ecfef2.html 背景: 关于cornerstone的介绍很少: 这里介绍mac的 ...
- python 中文异常问题记录
头上加入以下内容试试: # -*- coding:utf-8import sysimport osreload(sys)sys.setdefaultencoding( "utf-8" ...
- poj 3273 Monthly Expence 简单二分
/** 大意: 有连续的n天,每一天有一定的花费,将其分成m份,每一份占一天或者连续的几天,求这m份中的最大值 思路: 二分其最大上限,看在此最大上线,能分成多少份,若大于m份,说明上限过小,需要扩大 ...