手动获取容器对象时,报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的更多相关文章

  1. 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 ...

  2. 配置Spring的用于初始化容器对象的监听器

    <!-- 配置Spring的用于初始化容器对象的监听器 --> <listener> <listener-class>org.springframework.web ...

  3. 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 ...

  4. javascript客户端遍历控件与获取父容器对象

    javascript客户端遍历控件与获取父容器对象示例代码 1,遍历也面中所有的控件function findControlAll()    {        var inputs=document. ...

  5. 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 ...

  6. spring cloud gateway网关启动报错:No qualifying bean of type 'org.springframework.web.reactive.DispatcherHandler'

    网关配置好后启动报错如下: org.springframework.context.ApplicationContextException: Unable to start web server; n ...

  7. Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined(转)

    最近项目里面,用了spring的定时任务,一直以来,项目运行的不错.定时器也能正常使用.可是,今天启动项目测试的时候,盯着启动Log看了一阵子,突然间发现,启动的Log中居然有一个异常,虽然一闪而过, ...

  8. 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 ...

  9. spring注入时报错::No qualifying bean of type 'xxx.xxMapper'

    做一个小项目,因为有 baseService,所以偷懒就没有写单独的每个xxService接口,直接写的xxServiceImpl,结果在service实现类中注入Mapper的时候,用的 @Auto ...

随机推荐

  1. python strip()函数介绍

    函数原型 声明:str为字符串,s为要删除的字符序列 str.strip(s)        删除str字符串中开头.结尾处,位于 s删除序列的字符 str.lstrip(s)       删除str ...

  2. 简谈python反射

    写出一个简单类:import sysclass webserver(object): def __init__(self,host,post): self.host = host self.post ...

  3. superMap Object 属性查看的一点代码

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. DEV GridControl 小结(持续添加)

    一.属性: 1.Views OptionsBehavior=>Editable:False  列表不可编辑 OptionsSelection=>EnableAppearanceFocuse ...

  5. TreeSet两种比较

    TreeSet底层数据结构是二叉树 判断对象是否一致是通过是对象自身有比较的方法,即使类实现Comparable接口,重写compareTo方法,自己定义比较规则, 若是不想用元素本身的比较方法,又不 ...

  6. 多线程笔记--原子操作Interlocked系列函数

    前面写了一个多线程报数的功能,为了描述方便和代码简洁起见,只输出最后的报数结果来观察程序运行结果.这非常类似一个网站的客户访问统计,每个用户登录用一个线程模拟,线程运行时将一个表示计数的变量递增.程序 ...

  7. servlet操作数据库

    工具:myeclipse 数据库工具:mysql java ee操作数据库,首先要导入数据库驱动文件,我用的是mysql 刚开始,很多人代码正确但是就是连接不上,原因就是忘了驱动文件的导入. 我的驱动 ...

  8. object-c 内存管理机制的学习

    1.内存的创建和释放 让我们以Object-c世界中最最简单的申请内存方式展开,谈谈关于一个对象的生命周期.首先创建一个对象: //“ClassName”是任何你想写的类名,比如NSString NS ...

  9. android-JSON解析

    构建JSON文本 方法1. // 假设现在要创建这样一个json文本 // { // "phone" : ["12345678", "87654321 ...

  10. 国内BI工具/报表工具厂商简介

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...