Announcement Announcement Module
Collapse
No announcement yet.
Hypothetical Land: Can you use annotations for this situation? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hypothetical Land: Can you use annotations for this situation?

    OK, let's say you have a concrete class called Foo. This class depends on the IBar interface. There are two implementations of the IBar interface (Bar1 and Bar2). Can you create two instances of Foo, one which uses Bar1 and the other which gets a Bar2, WITHOUT explicitly defining the property of each Foo in XML?

    Here's an applicationContext that shows what I'm talking about.

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">    
        <!--instance of Foo-->
        <bean id="foo1" class="Foo">
            <!--Does this HAVE to be configured in XML?  Can I do this with annotations?-->
            <property name="ibar" ref="bar1"/>
        </bean>
        
        <!--instance of Foo-->
        <bean id="foo2" class="Foo">
            <!--Does this HAVE to be configured in XML?  Can I do this with annotations?-->
            <property name="ibar" ref="bar2"/>
        </bean>
        
        <!--instance of IBar-->
        <bean id="bar1" class="Bar1"/>
        <!--instance of IBar-->
        <bean id="bar2" class="Bar2"/>    
    </beans>

  • #2
    sure you can use @Resource or @Autowire annotation. Read more here

    Comment


    • #3
      Originally posted by pedro View Post
      sure you can use @Resource or @Autowire annotation. Read more here
      You sure? example? Note he has two bean definitions of foo using the same class.

      Foo.class<--Bar1.class, Foo.class<--Bar2.class

      foo1<-bar1, foo2<-bar2
      foo1.class = foo2.class
      bar1.class != bar2.class

      Comment


      • #4
        Yep. You can use @Qualifier, read this link I put in previous post.

        Comment


        • #5
          Originally posted by pedro View Post
          Yep. You can use @Qualifier, read this link I put in previous post.
          Qualifier is ok for narrowing down between two dependencies, but not to inject different dependencies into two instances of the same "parent" class. In the link you provided, @Qualifier doesn't allow both actionMovieRecommenderBean and mainMovieRecommenderBean.

          Ex.
          userBean
          guyMovieRecommender=actionMovieRecommenderBean (uses @qualifier "action")
          girlMovieRecommender=mainMovieRecommenderBean (uses @qualifier "main")
          Last edited by hozo; Mar 19th, 2009, 06:10 PM.

          Comment


          • #6
            http://www.liangdianup.com/subpages/airfitting_1.htm

            Now that is a lot of brass air fittings there is just about every type
            of air fitting that you could want. Wholesale prices too. I guess these could be used as small water pipe fitting also. I
            used some of the parts to make my babington wvo burner.

            Comment


            • #7
              Ok. Now I understand. And of course this is a problem because you have annotation with your class so you cannot have two beans with different annotations. The half workaround is two define default Qualifier in class and overwrite it in XML file.

              Code:
                  <!-- default wiring -->
                  <bean id="foo1" class="info.pietrowski.tests.Foo"/>
                  <!--instance of Foo-->
                  <bean id="foo2" class="info.pietrowski.tests.Foo">
                      <property name="ibar" ref="bar2"/>
                  </bean>
              and Foo class
              Code:
              ...
              @Component
              public class Foo {
              
                  @Autowired
                  @Qualifier(value = "bar1")
                  Bar ibar;
              ...

              Comment


              • #8
                Originally posted by pedro View Post
                ...you have annotation with your class so you cannot have two beans with different annotations.
                A general issue with DI annotations. Funny how we moved it out of code into xml, now back into classes (and got back its limitations).

                Comment

                Working...
                X