Results 1 to 6 of 6

Thread: Support for Qualifier metadata

Hybrid View

  1. #1

    Default 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. #2
    Join Date
    Apr 2007
    Posts
    307

    Default

    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.
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

  3. #3

    Default 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

  4. #4
    Join Date
    Apr 2007
    Posts
    307

    Default

    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.
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

  5. #5

    Default Cool Thanks

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

    Best Regards,
    Srini

  6. #6

    Default

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

Tags for this Thread

Posting Permissions

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