spring exception--No unique bean of type
今天碰到一个问题,就是我现有项目需要加一个定时器任务,我的代码如下:
- <!-- 每日数据同步 总数监测任务******************begin -->
- <bean id="dataMonitorServiceImpl"
- class="com.netqin.function.dataMonitor.service.impl.DataMonitorServiceImpl">
- </bean>
- <bean id="scheduledDataMonitorDetail"
- class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject">
- <ref bean="dataMonitorServiceImpl" />
- </property>
- <property name="targetMethod">
- <value>autoMonitorSyncStatus</value>
- </property>
- </bean>
- <bean id="dataMonitorCronReportTrigger"
- class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail">
- <ref bean="scheduledDataMonitorDetail" />
- </property>
- <property name="cronExpression">
- <value>0 41 10 * * ?</value>
- </property>
- </bean>
- <!-- 每日数据同步 总数监测任务******************end -->
这里我初始化了一个叫:dataMonitorServiceImpl的bean,这个bean是一个现有Service的实现类。
- @Service
- public class DataMonitorServiceImpl implements IDataMonitorService {
- Logger logger = Logger.getLogger(DataMonitorServiceImpl.class);
- @Autowired
- private IBaseDao dao;
- @Autowired
- private IDasBaseDao dasDao;
如上所示,我这里是用了注解@Service,spring会自动加载这个类,
因为上面我对这个bean有定义了一次,理论上来说会报错,因为同一个bean加载了两次,那么当向IDataMonitorService注入的时候就会发现有两个DataMonitorServiceImpl的bean,
但是我想想中的报错场景并没有发生,tomcat正常启动,我就很想知道为什么。
我把xml里面定义bean的名字改了一下,第一个字母改成大写,重启tomcat,ok,我看见了我想看见的错误:
- No unique bean of type [com.netqin.function.dataMonitor.service.IDataMonitorService] is defined: expected single matching bean but found 2: [dataMonitorServiceImpl, DataMonitorServiceImpl]
很明显,后边那个bean是我在xml里面声明的,那么第一个就是我用annotation的@Service声明的,这就是说明
1. @Service在声明bean的时候如果不指定名称的话,会默认以类名的一个字母小写命名。
2. 当spring初始化bean时,会监测要初始化的bean是否已经存在相同名称的bean。
做出上面的两个结论之后,我又有了俩个疑问,
1. bean在初始化的时候,xml声明和annotation的声明初始化bean的先后顺序是怎么样的。
2. bean在监测是否有相同名称的bean的时候,是只检测名称一致性,还是说是在每个bean的类别下的名称一致性?
第一个问题的答案是这样的,他们的初始化的顺序是有你指定的xml所决定的,举个例子:
你在applicationcontext.xml(这里只是举例)里显示声明了annotation需要扫描的文件目录包括那些,然后接在在下面又声明了很多bean,这种情况就是先初始化annotation,在初始化xml生命的bean。
spring是依据你声明的顺序来初始化一切,spring会从最原始的spring的xml文件开始扫描,扫描一条处理一条,也可能你的spring又引用了很多别的xml配置,那也是一样的,看引入的先后顺序。
第二个问题我做了如下的实验,:
- <span style=""><bean id="dataMonitorServiceImpl"
- class="com.netqin.function.channel.service.impl.ChannelManagementServiceImpl">
- </bean>
- <!-- <bean id="dataMonitorServiceImpl"
- class="com.netqin.function.dataMonitor.service.impl.DataMonitorServiceImpl">
- </bean> --></span>
原先的bean我注释掉了 我换了一个,但是bean名字没变,
- <span style="">public DataMonitorServiceImpl(){
- System.out.println("DataMonitorServiceImpl is loaded!");
- }</span>
我给DataMonitorServiceImpl写了一个空的构造方法,已确定他是否已经被spring装载。
ok,看看tomcat说什么:
- <span style=""> No unique bean of type [com.netqin.function.dataMonitor.service.IDataMonitorService] is defined: Unsatisfied dependency of type [interface com.netqin.function.dataMonitor.service.IDataMonitorService]: expected at least 1 matching bean</span>
spring说没有这个类型的bean被定义,这个类型的bean被期望的是最少有一个匹配的bean。
第二个问题的答案就是spring在初始化bean的时候是只保持名称的一致性,这个其实和xml里面声明bean的时候bean不能重复是一致的,多了annotation也不影响。
总结一下今天的结论:
1. @Service在声明bean的时候如果不指定名称的话,会默认以类名的一个字母小写命名。
2. 当spring初始化bean时,会监测要初始化的bean是否已经存在相同名称的bean。
3. spring初始化bean的顺序依赖于你在xml里面配置的顺序。
4.spring在初始化bean的时候是只保持名称的一致性。
spring exception--No unique bean of type的更多相关文章
- Spring Error : No unique bean of type [org.apache.ibatis.session.SqlSessionFactory] is defined
报错信息: Injection of autowired dependencies failed; nested exception is org.springframework.beans.fa ...
- paip . 解决spring No unique bean of type [com.mijie.homi.search.service.index.MoodUserIndexService]
paip . 解决spring No unique bean of type [com.mijie.homi.search.service.index.MoodUserIndexService] ...
- 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 eureka required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found.
spring在集成第三方过程很容易出现类名相同,且基本作用相同的类.这样给初学者带来一定的困惑. 导致用错类而出现以下问题. required a bean of type 'com.netflix. ...
- Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined(转)
最近项目里面,用了spring的定时任务,一直以来,项目运行的不错.定时器也能正常使用.可是,今天启动项目测试的时候,盯着启动Log看了一阵子,突然间发现,启动的Log中居然有一个异常,虽然一闪而过, ...
- No unique bean of type..... Unsatisfied dependency of type
比如在XXXServiceImpl里面写了aa()方法给别的地方调用 但是自己又调用了自己 在开头写了 @Autowired Private XXX xxx; xxx.aa(); 这样重复调用自己的b ...
- No unique bean of type [net.shougongfang.action.paymoney.AlipayPayMoneyReturnObj] is defined: Unsat
0 你把@Service放到实现类上吧.这个问题好像不止一个人在问啦 2013年10月25日 10:34 shidan66 30 0 1 1 加入评论 00 1,@service放到实现上 2. ...
- 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 Boot:Consider defining a bean of type '*.*.*' in your configuration解决方案
果然不看教程直接使用在遇到问题会懵逼,连解决问题都得搜半天还不一定能帮你解决了... ***************************APPLICATION FAILED TO START*** ...
随机推荐
- 【学习总结】OS X , IOS , IOS SDK , XCode之间的关系
几个基本的概念 : OS X : 属于桌面PC级别(IMac,MacPro等)对应安装的操作系统 IOS : 属于移动设备级别(Iphone,Ipad等)对应安装的操作系统 XCode: 是一个IDE ...
- 详解C#中的反射(转载)
反射(Reflection) 2008年01月02日 星期三 11:21 两个现实中的例子: 1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何做到的呢?B超是 ...
- hbase-site.xml 参数设置
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="confi ...
- poj 2060 Taxi Cab Scheme (最小路径覆盖)
http://poj.org/problem?id=2060 Taxi Cab Scheme Time Limit: 1000MS Memory Limit: 30000K Total Submi ...
- Linux学习笔记(2)-用户和用户组
用户(user)和用户组(group)概念 linux是一个多用户操作系统,他允许多个用户登录linux系统进行各自不同的操作.为了方便管理用户不同的权限,组的概念应用而生,一个组可以包含多个用户,共 ...
- CROSS APPLY和 OUTER APPLY 区别
转 http://www.cnblogs.com/end/archive/2011/02/17/1957011.html FROM employees AS e join employee ...
- Workspace in use or cannot be created, choose a different one.--错误解决办法
eclipse 使用一段时间后,有时会因为一些故障自己就莫名奇妙的关闭了,再打开时有时没有问题,有时有会提示错误 Workspace Unavailable: 原因:出现这种情况一般是workspac ...
- Tiny6410 LED字符设备驱动
1.查看用户手册 led1.led2.led3.led4 连接的分别是 GPK4.GPK5.GPK6.GPK7 2.查询6410芯片手册 下面还需要3个步骤: 1.设置GPIO为OUTPUT. 将GP ...
- POJ2527+多项式除法
模拟一遍即可. 注意一些特殊情况,见代码. #include<stdio.h> #include<stdlib.h> #include<math.h> #inclu ...
- *[topcoder]GooseTattarrattatDiv1
http://community.topcoder.com/stat?c=problem_statement&pm=12730&rd=15701 这道题有点意思.首先把字符串变成回文, ...