Results 1 to 3 of 3

Thread: @Qualifier on constructor not working :(

  1. #1
    Join Date
    Aug 2012
    Posts
    3

    Default @Qualifier on constructor not working :(

    Hi

    I have two defined two datasources:

    <beans ... default-autowire="byType">

    ...

    <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource" name="dataSource"> ...

    <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSourceXXX" name="dataSourceXXX"> ...


    and want to inject one of them in an annotated StoredProcedure:


    @Component
    @Scope("prototype")
    public class AddTrialProcedure extends StoredProcedure{

    @Autowired
    public AddTrialProcedure(@Qualifier("dataSource") DataSource dataSource) {

    But Spring ignores @Qualifier and instead throws me an error on startup:

    NoSuchBeanDefinitionException: No unique bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: [dataSource, dataSourceTrialpedia]

    Where is the mistake?

    Spring version is 3.1.2.RELEASE

  2. #2

    Default

    problem might be because of the 'autowire by type':
    Code:
    <beans ...	default-autowire="byType">
    because you have two beans with same type, and 'autowire by type' means that Spring will inject detected constructor parameters (and setters) using 'type lookup', it happens before annotation process, so @Qualifier seems to be ignored in this case.

    So just remove it and try again..

  3. #3
    Join Date
    Aug 2012
    Posts
    3

    Default

    Quote Originally Posted by tannoy View Post
    problem might be because of the 'autowire by type':
    Code:
    <beans ...	default-autowire="byType">
    because you have two beans with same type, and 'autowire by type' means that Spring will inject detected constructor parameters (and setters) using 'type lookup', it happens before annotation process, so @Qualifier seems to be ignored in this case.

    So just remove it and try again..
    Thx! It worked straight like this, and nothing on the rest of application stopped working.

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
  •