手动获取容器对象时,报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. DLL注入_拦截技术之Hook方式

    后卫大师教你进程注入 首先提一下,由于文章完全是我手写,所以打不了太多,请包含,由于我已经提供了源代码,所以我在这里详细讲一下理论,至于想看代码的下载代码就可以了.代码中关于注入的部分做了详细的注释. ...

  2. 基于KVM建立虚拟机的步骤及总结说明

    1.前言 目前正在涉足云计算IaaS工作,虚拟化是IaaS的重要部分,因此这段时间对各个虚拟机化技术和工具进行研究,研究的目的不仅仅是为了会使用这个工具,而是通过研究了解技术的实现机制和原理,即知其然 ...

  3. ios post空文件流导致400错误

  4. 一个用C++写的Json解析与处理库

    什么是Json?这个库能做什么? JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is e ...

  5. C# 保留小数点后两位(方法总结)

    最简单使用: float i=1.6667f; string show=i.ToString("0.00"); //结果1.67(四舍五入) 其他类似方法: string show ...

  6. zzbank oneOpencloud Env linuxaix6.1 interactiveMaintain(nfs,aix genintall基于系统iso光盘,aix6.1 puppet-Agent,Cent6.4 puppetServer,agent time no syn case Er)

    1,puppet--server,Client,Agent time no syn case eror puppet agent --server frontend -terr: Could not ...

  7. linux 使用ssh到远端并且使用while的坑

    如果要使用ssh批量登录到其它系统上操作时,我们会采用循环的方式去处理,那么这里存在一个巨大坑,你必须要小心了. 现在是想用一个脚本获取远程服务器端/root下面的文件: #!/bin/bash ca ...

  8. .NET WIN7+IIS 7.5下URLRewriter组件伪静态设置

    原文地址:WIN7+IIS 7.5伪静态的设置 --------------------------------------------------------偶是分割线君-------------- ...

  9. mysql 时间差问题集锦

    SELECT * from grouptoadd where taskid = '103244'; select datediff(max(spreadtime),min(createtime)) f ...

  10. BZOJ 1497: [NOI2006]最大获利( 最大流 )

    下午到周六早上是期末考试...但是我还是坚守在机房....要挂的节奏啊.... 这道题就是网络流 , 建图后就最大流跑啊跑啊跑... --------------------------------- ...