Announcement Announcement Module
Collapse
No announcement yet.
@Qualifier on constructor not working :( Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @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
    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..

    Comment


    • #3
      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.

      Comment

      Working...
      X