Announcement Announcement Module
Collapse
No announcement yet.
How I may do autowiring byName in TestContext Framework ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How I may do autowiring byName in TestContext Framework ?

    Morning!
    I have a issue during use TestContext Framework.
    ------------------------------
    My environment:
    OS: Gentoo Linux x86 and x86-64
    - kernel: 2.6.23-gentoo-r6
    JVM: Sun JDK 1.6.0_04-b12 and BEA JRockIt R27.4.0-jdk1.6.0_02
    Build engines:
    - Apache Ant version 1.7.0 compiled on November 30 2007
    - Apache Maven version: 2.0.8
    AS: JBoss-4.2.2.GA.
    ORM: Hibernate 3.2.5.GA
    IDE: Eclipse-3.3.1.1
    Test Engine: TestNG 5.7
    Spring Framework 2.5.1
    --------------------------------
    I have two datasource in my configuration file:
    --------------------------------
    ....
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource"
    p:driverClassName="org.postgresql.Driver" p:url="jdbcostgresql://localhost:5432/myDB"
    p:username="johnbat26" password="" />

    <bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverM anagerDataSource"
    p:driverClassName="org.postgresql.Driver" p:url="jdbcostgresql://localhost:5432/myDB2"
    p:username="johnbat26" password="" />
    ...
    ----------------------------
    So I use TestNG 5.7, than extend AbstractTransactionalTestNGSpringContextTests class.

    But this class use autowire byType (@Autowired annotation) for dataSource property:
    ------------------------
    .....
    @Autowired
    public void setDataSource(final DataSource dataSource) {
    this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }
    ......
    ------------------------

    But I need inject dataSource2 in my bean too !
    During start of test appear next exception:
    -------------
    Autowiring of methods failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire method: public void org.springframework.test.context.testng.AbstractTr ansactionalTestNGSpringContextTests.setDataSource( javax.sql.DataSource); nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: [dataSource, dataSource2]
    -------------
    I understand that need use @Resourse annotation for autowire byName.
    But this don't help (((
    I don't overide annotation in AbstractTransactionalTestNGSpringContextTests class at dataSource property.

    What I must do for resolve this problem ?
    Thanks in advance!

  • #2
    Try using the @Qualifier annotation under the @Autowired to specify a specific bean name.

    Comment


    • #3
      Originally posted by sbirnie View Post
      Try using the @Qualifier annotation under the @Autowired to specify a specific bean name.
      Thanks !

      I have done this:
      ---------------------
      .....
      @Autowired
      public void setDataSource(@Qualifier("dataSource") final DataSource dataSource) {
      this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
      }
      ---------------------
      )

      Comment


      • #4
        working with multi datasource

        public void setDataSource1(@Qualifier("dataSource1")DataSource dataSource1) {
        this.template1 = new JdbcTemplate(dataSource1);
        }
        @Autowired
        public void setDataSource2(@Qualifier("dataSource2")DataSource dataSource2) {
        this.template2 = new JdbcTemplate(dataSource2);
        }


        this one works...thanks

        Comment

        Working...
        X