手动获取容器对象时,报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. mybatis之动态SQL

    <if>的使用 如果第一个if不成立的话可能会出现where and的语法错误,解决方法是在外层加<where>标签,此时如果以and和or衔接where的话会被删除. < ...

  2. java——%

    %; //结果为1 %; //结果为-1 ; //结果为1.2000000000000002

  3. Ext Store Proxy Ajax

    使用Store ajax的方式来获取数据 <div id="grid1"> </div> <script> Ext.onReady(functi ...

  4. ExtJS 自定义组件

    主要参考的是官方文档 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  5. Linux菜鸟之路[4]-cal,date,bc,echo $LANG,man

    由于前四天一直在看鸟哥的linux书本的计算机的一些基础知识,今天才接触基本的命令,从今天起每天记录一下自己的linux学习过程. cal:日历 cal: cal 2015:列出2015年所有日历 c ...

  6. hihoCoder 1388 Periodic Signal(FFT)

    [题目链接] http://hihocoder.com/problemset/problem/1388 [题目大意] 给出A数列和B数列,求下图式子: [题解] 我们将多项式拆开,我们可以得到固定项A ...

  7. Cube Stacking(并差集深度+结点个数)

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 21567   Accepted: 7554 Ca ...

  8. 模式匹配-KMP算法

    /***字符串匹配算法***/ #include<cstring> #include<iostream> using namespace std; #define OK 1 # ...

  9. 新浪微博开放平台开发-android客户端(1)【转】

    http://www.cnblogs.com/virusswb/archive/2011/08/05/2128941.html 最近不是太忙,花了一些时间学习android的应用开发.经过两个星期的学 ...

  10. android环境下解决java.io.IOException: Malformed ipv6异常的方法

    今天做客户端想服务端提交信息的时候,报出了如标题所显示的方法 方法以及参数如下: 输入的参数为:http://192.168.1.173:8080/Api/petinfo/petinfo?flag=a ...