Spring exception--no unique bean of type

Source: Internet
Author: User

Today I have a problem, that is, my existing project needs to add a timer task, my code is as follows:

Java code
  1. <!--daily data Sync Total Monitor Task ******************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 * *?</value>
  21. </property>
  22. </bean>
  23. <!--daily data Sync Total Monitor Task ******************end-

Here I initialize a bean called: Datamonitorserviceimpl, which is an implementation class for an existing service.

Java code
    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;

As shown above, I use annotations here @service,spring will automatically load this class,

Because I have defined this bean once, in theory it will be an error, because the same bean has been loaded two times, then when injected into Idatamonitorservice, there are two datamonitorserviceimpl beans.

But I think the error in the scene did not happen, Tomcat normal startup, I would like to know why.

I changed the name of the definition bean in XML, changed the first letter to uppercase, restarted Tomcat,ok, and I saw the error I wanted to see:

JavaScript code
    1. No unique bean of type [Com.netqin. Function.dataMonitor.service.IDataMonitorService] is defined:expected single matching beans but found 2: [ Datamonitorserviceimpl, Datamonitorserviceimpl]

Obviously, the bean behind that is what I declare in XML, so the first one is that I declare it with annotation's @service, which is the explanation

1. @Service when declaring a bean, if you do not specify a name, it defaults to a lowercase letter in the class name.

2. When spring initializes the bean, it monitors whether the bean to be initialized already has a bean of the same name.

Having made the above two conclusions, I have two more questions,

1. When the bean is initialized, what is the order of the XML declaration and annotation declaration initialization bean?

2. Does the Bean detect only name consistency, or name consistency under each bean's category, when monitoring beans with the same name?

The answer to the first question is that the order in which they are initialized is determined by the XML you specify, for example:

You show in Applicationcontext.xml (here is just an example) that the file directory that annotation needs to scan is included, and then a lot of beans are declared in the following, which is the first initialization of annotation, the bean that initializes the XML life.

Spring initializes everything according to the order you declare, and spring starts the scan from the most primitive spring XML file, scans one for processing, or maybe your spring references a lot of other XML configurations, and that's the same thing, looking at the sequence of introductions.

The second question I did was the following experiment:

Java code
    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>

The original Bean I commented out I changed one, but the bean name did not change,

Java code
    1. <span style="" > PublicDatamonitorserviceimpl () {
    2. System.out.println ("Datamonitorserviceimpl is loaded!");
    3. }</span>

I wrote an empty construction method for Datamonitorserviceimpl and determined if he had been loaded by spring.

OK, look at what Tomcat says:

Java code
    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 says no bean of this type is defined, and this type of bean is expected to have at least one matching bean.

The answer to the second question is that spring initializes the bean with only the consistency of the name, which is the same as when the bean is declared in the XML, and the annotation does not have any effect.

Summarize today's conclusion:

1. @Service when declaring a bean, if you do not specify a name, it defaults to a lowercase letter in the class name.

2. When spring initializes the bean, it monitors whether the bean to be initialized already has a bean of the same name.

3. The order of spring initialization beans depends on the order in which you configure the XML.

4.spring only maintains name consistency when the bean is initialized.

Spring exception--no unique bean of type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.