Results 1 to 4 of 4

Thread: How I may do autowiring byName in TestContext Framework ?

  1. #1
    Join Date
    Jun 2005
    Location
    Russia
    Posts
    11

    Question 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. #2
    Join Date
    Mar 2007
    Posts
    128

    Default

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

  3. #3
    Join Date
    Jun 2005
    Location
    Russia
    Posts
    11

    Default

    Quote 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);
    }
    ---------------------
    )

  4. #4

    Default 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

Posting Permissions

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