今天碰到一个问题,就是我现有项目需要加一个定时器任务,我的代码如下:

  1. <!-- 每日数据同步 总数监测任务******************begin -->
  2. <bean id="dataMonitorServiceImpl"
  3. class="com.netqin.function.dataMonitor.service.impl.DataMonitorServiceImpl">
  4. </bean>
  5. <bean id="scheduledDataMonitorDetail"
  6. class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  7. <property name="targetObject">
  8. <ref bean="dataMonitorServiceImpl" />
  9. </property>
  10. <property name="targetMethod">
  11. <value>autoMonitorSyncStatus</value>
  12. </property>
  13. </bean>
  14. <bean id="dataMonitorCronReportTrigger"
  15. class="org.springframework.scheduling.quartz.CronTriggerBean">
  16. <property name="jobDetail">
  17. <ref bean="scheduledDataMonitorDetail" />
  18. </property>
  19. <property name="cronExpression">
  20. <value>0 41 10 * * ?</value>
  21. </property>
  22. </bean>
  23. <!-- 每日数据同步 总数监测任务******************end -->

这里我初始化了一个叫:dataMonitorServiceImpl的bean,这个bean是一个现有Service的实现类。

  1. @Service
  2. public class DataMonitorServiceImpl implements IDataMonitorService {
  3. Logger logger = Logger.getLogger(DataMonitorServiceImpl.class);
  4. @Autowired
  5. private IBaseDao dao;
  6. @Autowired
  7. private IDasBaseDao dasDao;

如上所示,我这里是用了注解@Service,spring会自动加载这个类,

因为上面我对这个bean有定义了一次,理论上来说会报错,因为同一个bean加载了两次,那么当向IDataMonitorService注入的时候就会发现有两个DataMonitorServiceImpl的bean,

但是我想想中的报错场景并没有发生,tomcat正常启动,我就很想知道为什么。

我把xml里面定义bean的名字改了一下,第一个字母改成大写,重启tomcat,ok,我看见了我想看见的错误:

  1. 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配置,那也是一样的,看引入的先后顺序。

第二个问题我做了如下的实验,:

  1. <span style=""><bean id="dataMonitorServiceImpl"
  2. class="com.netqin.function.channel.service.impl.ChannelManagementServiceImpl">
  3. </bean>
  4. <!-- <bean id="dataMonitorServiceImpl"
  5. class="com.netqin.function.dataMonitor.service.impl.DataMonitorServiceImpl">
  6. </bean> --></span>

原先的bean我注释掉了 我换了一个,但是bean名字没变,

  1. <span style="">public DataMonitorServiceImpl(){
  2. System.out.println("DataMonitorServiceImpl is loaded!");
  3. }</span>

我给DataMonitorServiceImpl写了一个空的构造方法,已确定他是否已经被spring装载。

ok,看看tomcat说什么:

  1. <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的更多相关文章

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

  2. 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] ...

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

  4. spring eureka required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found.

    spring在集成第三方过程很容易出现类名相同,且基本作用相同的类.这样给初学者带来一定的困惑. 导致用错类而出现以下问题. required a bean of type 'com.netflix. ...

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

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

  6. No unique bean of type..... Unsatisfied dependency of type

    比如在XXXServiceImpl里面写了aa()方法给别的地方调用 但是自己又调用了自己 在开头写了 @Autowired Private XXX xxx; xxx.aa(); 这样重复调用自己的b ...

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

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

  9. Spring Boot:Consider defining a bean of type '*.*.*' in your configuration解决方案

    果然不看教程直接使用在遇到问题会懵逼,连解决问题都得搜半天还不一定能帮你解决了... ***************************APPLICATION FAILED TO START*** ...

随机推荐

  1. mac系统在控制台中ping网址提示不能解析host

    更换dns就好了 系统偏好设置——网络——dns 换成 8.8.8.8 114.114.114.114 也可以参考v2ex的dns http://www.guomii.com/posts/5315

  2. DB天气app冲刺第十天

    好了 这是第十天了,按照白板任务上的来说的话,今天没有完成,所以等一下还要继续看看今天能不能把他做完,今天出的问题在于我又自己调整了一下UI设计,因为发现以前的设计发面有重复,浪费了屏幕.所以还不如省 ...

  3. C# 写XML文件

    /// <summary>x /// 修改xml文件 /// </summary> /// <param name="dt"></para ...

  4. Contest2037 - CSU Monthly 2013 Oct (Problem J: Scholarship)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2037&pid=9 [题解]: 这题卡了一下,卡在负数的情况,负数输出 0 这题主要找到一 ...

  5. ExtJS4.2学习(11)可拖放的表格(转)

    鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-18/180.html --------------- ...

  6. 原生js获取window高和宽

    视口的宽和高 var pw = window.innerWidth, ph = window.innerHeight; if(typeof pw != "number"){ pw ...

  7. jQuery树结构插件推荐zTree

    JQuery zTree 下载地址http://plugins.jquery.com/zTree.v3/

  8. 团体程序设计天梯赛-练习集L1-002. 打印沙漏

    L1-002. 打印沙漏 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给 ...

  9. 认识OD的两种断点

    OllyDBG从原理上来区分,有两种不同的断点:软件断点和硬件断点. 也许会有朋友说那不是还有内存断点吗? 内存断点严格来说是属于一种特殊的软件断点. 内存断点: 内存断点每次只能设置一个,假如你设置 ...

  10. spoj 24

    大数  #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> ...