Announcement Announcement Module
Collapse

JavaConfig forum decommissioned in favor of Core Container

As described at

http://static.springsource.org/sprin...fig/README.TXT

key features of the Spring JavaConfig project have been migrated into the core Spring Framework as of version 3.0.

Please see the Spring 3.0 documentation on @Configuration and @Bean support:

http://static.springsource.org/sprin...tml#beans-java

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at

http://forum.springsource.org/forumdisplay.php?f=26
See more
See less
Support for Qualifier metadata Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Support for Qualifier metadata

    I was trying to use the Qualifier metadata based disambiguation with Java Config. I was not able to use the same. Does Java Config support the same (I guess not )? If it is supported can you please explain how I can use it? If it is not supported, then are there any plans for providing support? By when can we expect the support?

    Thanks,
    nacnez

  • #2
    Srinivas,

    Please provide a synopsis in code of what you're trying to accomplish. Depending on what you're doing, @Qualifier may or may not be supported.

    Comment


    • #3
      Providing a rough example of what I am trying to do

      The scenario I am trying to depict is where I have different business components talking to dataprovider classes (DAO). I have a DAO which has both a database (DB) and XML implementation. One business component wants to be autowired with the XML Data provider whereas the other component wants to autowired with DB Data provider. The code example is below:

      Configuration class

      @Configuration
      @AnnotationDrivenConfig
      public class MyAppConfig {

      @Bean(qualifier="xml")
      public DataProvider xmlDataProvider() {
      return new XMLDataProvider();
      }

      @Bean(qualifier="db")
      public DataProvider dbDataProvider() {
      return new DBDataProvider();
      }

      }

      The first business component which uses the XML provider

      public class MyBusinessBean {
      @Autowired
      @Qualifier("xml")
      DataProvider dataProvider;

      public doBusiness() {
      dataProvider.getBusinessData();
      }

      }

      The second business component which uses the DB provider

      public class MyAnotherBusinessBean {

      @Autowired
      @Qualifier("db")
      DataProvider dataProvider;

      public doBusiness() {
      dataProvider.getBusinessData();
      }

      }

      Hope the example is clear. Please let me know if I am missing something.

      The qualifier annotation usage can be used for disambiguation. I am not able to use the disambiguation provided by different bean names in the configuration class (xmlDataProvider(), dbDataProvider()) when I am using @Autowired. If there is way to do that that should also be fine.

      Thanks,

      Best Regards,
      Srini

      Comment


      • #4
        Hi Srinivas,

        The example is clear, thanks.

        You're correct that JavaConfig does not support @Qualifier as you're trying to use it. That will come, but in the meantime, you're not out of luck.

        You can autowire by name by simply providing the bean name as the value attribute to the @Qualifier annotation.

        Drawing from your example:

        Code:
        @Configuration
        @AnnotationDrivenConfig
        public class MyAppConfig {
        
            @Bean
            public DataProvider xmlDataProvider() {
                return new XMLDataProvider();
            }
        
            @Bean
            public DataProvider dbDataProvider() {
                return new DBDataProvider();
            }
        
        }
        You can actually use the xmlDataProvider and dbDataProvider bean names as qualifiers:

        Code:
        public class MyBusinessBean {
            @Autowired
            @Qualifier("xmlDataProvider")
            DataProvider dataProvider;
        
            public doBusiness() {
                dataProvider.getBusinessData();
            }
        }
        Alternatively, this can be done more concisely using the JSR-250 @Resource annotation:


        Code:
        public class MyBusinessBean {
            @Resource("xmlDataProvider")
            DataProvider dataProvider;
        
            public doBusiness() {
                dataProvider.getBusinessData();
            }
        }
        Hope that helps.

        Comment


        • #5
          Cool Thanks

          Cool Chris. If this works, then most of the job is done. Thanks a lot for the help.

          Best Regards,
          Srini

          Comment


          • #6
            Hey, Chris! It the limitation intact in Spring3 @Configuration classes?
            If so, are there any plans to add the qualifier support? Can I help?

            Comment

            Working...
            X