Results 1 to 3 of 3

Thread: Would there be any conflict between @Compnent and @Autowired

Hybrid View

  1. #1

    Default Would there be any conflict between @Compnent and @Autowired

    Hi All,

    I know that using xml configuration, we can define several bean elements, with different bean ids, but each of those beans are actually instance of same class. But how does this work in autowiring world. I will try to explain my question with an ex.

    Say, I have my classes defined as below

    Code:
    public class Employee{
    
    @Autowired
    @Qualifer("hr")
     private Work hr;
    
    @Autowired
    @Qualifier("finance")
     private Work finance;
    
    @Autowired
    @Qualifer("callcenter")
     private Work callcenter;
    
     public void doWork(Work work){
             work.doWork();
     }
    
    }

    Code:
    public interface Work{
       public void doWork();
    }
    
    @Component("component")
    public class WorkType implements Work{
        private String workName;
         ..........
         ..........
    }
    Now in context file
    Code:
    <bean name="hr" class = "WorkType">
       <property name="workName" value ="recruit"/>
    </bean>
    
    <bean name="finance" class = "WorkType">
       <property name="workName" value ="funds"/>
    </bean>
    
    <bean name="callcenter" class = "WorkType">
       <property name="workName" value ="business"/>
    </bean>
    Now as you see, I used @Component annotation as @Component("component") for WorkType class, but I autowired three instances with different Qualifiers in Employee class.

    My question here is, I'm mixing annotations with xml configuration here and as far as I know, xml overwrites annotations. But since I declared a class with @Component("component") and then autowired with different names, would it cause the application to blow up under any situation. Does the autowiring conflict with the value defined under @Component.

    Please advise.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    There shouldn't be any issue... The thing that can happen is that you end up with an extra instance if you use component-scanning.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Thank you Marten.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •