Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'apiUserController': Unsatisfied dependency expressed through field 'api…
项目使用spring, mybatis.因为分了多个模块,所以会这个模块引用了其它模块的现在,结果使用Junit测试的时候发现有两个模块不能自动注入dao和service问题.解决后在此记录一下. 解决思路,一直以为是dao的xml文件的配置问题,通过日志查看,发现所有的dao与mapper.xml的文件都加载进来了.想着可能是maven包的问题,通过与可测试模块的pom文件对比,解决了这个bug. 异常主要信息: java.lang.IllegalStateException: Failed…
看具体报错日志: 警告: Unable to proxy interface-implementing method [public final void cn.wlf.selection.proto.ComponentShowService._ComponentShowService$ComponentShowService.callMethod(com.google.protobuf.Descriptors$MethodDescriptor,com.google.protobuf.RpcCo…
报错如下: NoSuchBeanDefinitionException:No qualifying bean of type   found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. 错误原因:bean注入失败 解决方法: 1.在配置文件中启动注解功能<mvc:annotation-driven/> 2.在配置文件中添加扫描目录<c…
这个看起来很弱爆的问题其实是因为其他的配置文件中已经出现了为xx定义好的注入.如果用@Autowired就会得到上面的错误 , 但是用@Resource的时候就会看到类似下面的错误 Bean named 'moneyRecordDao' must be of type [com.fuscent.core.dao.MoneyRecordDao], but was actually of type [com.yooli.currentproduct.dao.impl.MoneyRecordDaoIm…
错误原因:没有引入相应mapper接口,导致spring没有找到依赖 解决方法一:使用注解的方法: 首先在spring配置文件中添加 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">         <property name="basePackage" value="com.forum" />         <property na…
在搭建SSM spring springmvc  mybatis整合的时候, 遇到了这个问题 说说我的问题吧!我在进行单元测试的时候,出现了这个错误,网上一查才知道是,配置文件中没有写扫描包信息.一看加载的是spring.xml文件,就去这个文件中查找了. 这个错误是因为自动扫描包配置出错.无法自动注入类到容器中 <!--配置自动扫描的包,用于注解的扫描@Service @Controller--> <context:component-scan base-package="c…
1.查看接口实现类是否加入注解,如service.repository等 2.查看spring配置文件是否自动扫描包    <context:component-scan base-package="xxx.xx.xxx"> 3.查看是否在web.xml中加载spring容器 4.service是否有实现impl…
spring中一个符号的错误居然让我浪费了四五个小时才找出来,不得不给自己了两个耳光.. 由于新建项目与原来项目的目录结构有所不同,copy过来的配置文件,有些地方修改的不彻底,导致spring扫描注解的时候,扫描不到,不能注入bean,导致运行出错.一个马虎浪费了这么多时间,符号这种问题也往往最容易忽视的.<context:component-scan base-package="com.oskyhang.*.service"/> <context:componen…
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.zxkj.lockserver.dao.CompanyDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.sprin…
报错类型 NoSuchBeanDefinitionException.No qualifying bean of type  XXXXX.***Mapper 报错信息详情 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.pflm.modules.job.dao.ScheduleJobMapper' available: expec…
SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册拦截器时,将拦截器注入为bean 代码: @Configuration public class InterceptorRegister extends WebMvcConfigurerAdapter { //以这种方式将拦截器注入为一个bean,可以防止拦截器中无法注入bean的问题出现 @Bea…
@PropertySource 使用指定的属性文件而不一定是application.xxx 同样可以注入相关内容 @ImportResource 导入Spring的配置文件,让配置文件里面的内容生效: Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别: 想让Spring的配置文件生效,加载进来:@ImportResource标注在一个配置类上 @Bean SpringBoot推荐给容器中添加组件的方式:推荐使用全注解的方式 package com.at…
这个需求应该也比较常见,在不同的条件下创建不同的bean,具体场景很多,能看到这篇的肯定懂我的意思. 倘若不了解spring4.X新加入的@Conditional注解的话,要实现不同条件创建不同的bean还是比较麻烦的,可能需要硬编码一些东西做if判断.那么现在有个@Conditional注解后,事情就简单多了.用法很简单,直接上代码. 新建一个springboot项目,添加一个Configuration标注的类,我们通过不同的条件表达式来创建bean.   package com.tianya…
@PropertySource:加载指定的配置文件 package com.hoje.springboot.bean; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.Enab…
背景 同一个接口有多种实现,项目启动时按某种规则来选择性的启用其中一种实现,再具体一点,比如Controller初始化的时候,根据配置文件的指定的实现类前缀,来记载具体Service,不同Service使用不同的Dao和数据库. 看到这里,我们会想到使用SPI机制,或Spring按条件加载Bean机制来实现,下面主要讨论后者. 定义接口 定义2个Service层接口:OrderService.OrderPromotionService,分别有一个方法,如下: // OrderService.ja…
一.@PropertySource @PropertySource:加载指定的配置文件 @PropertySource(value = {"classpath:person.properties"}) @Component @ConfigurationProperties(prefix = "person") public class Person { } 二.@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效 Spring Boo…
@Configuration 1.@Configuration注解底层是含有@Component ,所以@Configuration 具有和 @Component 的作用. 2.@Configuration注解相当于spring的xml配置文件中<beans>标签,里面可以配置bean. @Bean 1.@Bean注解相当于spring的xml配置文件<bean>标签,告诉容器注入一个bean. 2.@Bean注解的方法上如果没通过bean指定实例名,默认实例名与方法名相同. 3.…
spring 或 springboot 的 websocket 里面使用 @Autowired 注入 service 或 bean 时,报空指针异常,service 为 null(并不是不能被注入). 解决方法:将要注入的 service 改成 static,就不会为null了.参考代码: @Controller @ServerEndpoint(value="/chatSocket") public class ChatSocket { // 这里使用静态,让 service 属于类…
1.普通方式装配类对象   (1)添加带有@Bean注解的方法  User.java(带@Component注解) package com.demo.boot.bootenable.beanDemo1; import org.springframework.stereotype.Component; @Component public class User { } User.java (2)需要装配的类添加@Component注解 Book.java(不带@Component注解) packag…
前言 前段时间直接上手使用springboot开发了一个数据平台的后台部分,但是自身对于springboot的原理和过程还不是很清晰,所以反过来学习下springboot的基础. 大家都知道springboot是基于注解的,IOC和AOP是它的两大重要特性,然后AOP又是基于IOC来实现的.那么弄懂IOC就很有必要了. IOC:控制反转,一种设计思想,它是Spring的核心.简单点说就是spring管理bean的容器.IOC容器一般具备两个基本功能: 1.通过描述管理Bean,包括发布和获取.…
在使用springboo的时候,遇到一个问题,想在tool类中注入一个bean,一直失败,翻了下源码,是因为工具类的初始化方法为反射class调用newInstance方法,详见 http://www.cnblogs.com/xiaoQLu/p/7292971.html 那还有没有其他方法实现呢,请教了组内的大神,丢了如下一段代码我 @Configuration public class WebAppConfigTest extends WebMvcConfigurerAdapter { @Ov…
问题分析: 通过问题的英文可知,这个错误是service的bean注入失败,那么为什么会产生这个问题呢? 主要是框架的Application产生的,所以我们建立项目的时候,要保证项目中的类跟Application启动服务器类在一个目录层级,如图:        观察目录名称就可以得到,com.spring.boot.jap.perform,这个根目录,其他的子目录都要以它开头,这样就不会出现这个错误了. 解决方案二: 在Application中加入注解来识别bean注入,注解用@Componen…
今天在自己定义的filter中,想要直接注入spring容器的其它bean进行操作,发现不能正常的注入: 原因:web容器加载顺序导致, 加载顺序是listener——filter——servlet,当项目启动时,filter先于servlet初始化, 而Spring中默认bean的初始化是在Servlet后进行的,所以会注入失败 解决办法:接下来编写一个SpringUtils进行手动注入bean @Component public class SpringUtils implements Ap…
question: nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are act…
需要在启动类的@EnableAutoConfiguration或@SpringBootApplication中添加exclude = {DataSourceAutoConfiguration.class},排除此类的autoconfig.启动以后就可以正常运行. …
Spring的自动装配,也就是定义bean的时候让spring自动帮你匹配到所需的bean,而不需要我们自己指定了. 例如: User实体类里面有一个属性role 1 2 3 4 5 6 7 public class User {     private int id;     private String username;     private String password;     private Role role;     /*****省略get and set****/ } 在我…
1. 场景描述 启动的时候报"springboot available: expected at least 1 bean which qualifies as autowire candidate". 2. 解决方案 该错误一眼看过去,简单说就是bean无效,无法注入,使用的@Autowired或者@Resource注入bean有问题. 才想起来昨天从其他地方新加入一个mapper文件,为了区分现在的mapper,就单独放到一个文件夹里了. 2.1 问题定位 (1)首先确认新建文件夹…
报错信息: 2018-06-25 14:26:17.103  WARN 49752 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyExcept…