????????????л??? profile ??????? bean

?????? Spring 3.2 ???Spring ????????? @ActiveProfiles ??????????????????e??????????????????????????????????????????????????ú????????????????????????? profile ?????????? beans?????£?

?????嵥 10. Spring-db.xml

    <beans xmlns="http://www.Springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.Springframework.org/schema/beans 
      
    http://www.Springframework.org/schema/beans/Spring-beans-3.2.xsd">
      
         <beans profile="test">
     <bean id="datasource"
    >
     <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
         <property name="url" value="jdbc:hsqldb:hsql://localhost" />
         <property name="username" value="sa"/>
         <property name="password" value=""/>
         </bean>
    </beans>
      
    <beans profile="production">
     <bean id="datasource"
    >
         <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
         <property name="url" value="jdbc:hsqldb:hsql://localhost/prod" />
         <property name="username" value="sa"/>
         <property name="password" value=""/>
         </bean>
     </beans>
     <beans profile="test??production">
     <bean id="transactionManager"
        >
         <property name="dataSource" ref="datasource"></property>
         </bean>
         <bean id="initer" init-method="init">
         </bean>
     <bean id="accountDao" depends-on="initer">
                 <property name="dataSource" ref="datasource"/>
             </bean>
      
             <bean id="accountService">
             </bean>
             <bean id="envSetter"/>
         </beans>
     </beans>

???????????壬?????????

?????? ?? XML ????????????? Spring 3.2 ?? beans ???壬?????? Spring 3.2+ ???????? profile ?????

?????? ?? <beans> ????????????? <beans> ???壬???? profile ?????????????У?????????????? datasource????????? test profile????????? production profile???????????????????????м??? test profile??????? production ???????

?????? ?????漲?????Щ???????? profile ?? beans???? <beans profile=”test??production”> ?????????????Щ bean ????壬?????Щ bean ?????? profile ?ж????????

?????嵥 11. AccountServiceTest.Java

    @RunWith(SpringJUnit4ClassRunner.class) 
    @ContextConfiguration("/config/Spring-db.xml") 
    @Transactional
    @ActiveProfiles("test") 
    public class AccountServiceTest { 
    ... 
    }

???????????? @ActiveProfiles?????????????????? profile?????????????????????????Щ????? profile ?ж???? bean ?????

?????? TestNG ?????

????Spring 2.5 ????????? TestNG ????????????????

?????? ?????? TestNG ???????? Spring ????????AbstractTransactionalTestNGSpringContextTests ???? AbstractTestNGSpringContextTests?????????? TestNG ???????????????? applicationContext ?????????

?????? ????? Spring ???????????????? @TestExecutionListeners ?????????????????????????

????→ DependencyInjectionTestExecutionListener????ò???????????????????

????→ DirtiesContextTestExecutionListener????ò???????и??? applicationContext ????

????→ TransactionalTestExecutionListener????ò????????????????????????

???????????????????????? Spring ???? TestNG ?????????в????

?????嵥 12. AccountServiceTestNGTest.Java

    package testng; 
      
    import static org.Junit.Assert.assertEquals; 
      
    import org.Springframework.beans.factory.annotation.Autowired; 
    import org.Springframework.test.context.ActiveProfiles; 
    import org.Springframework.test.context.ContextConfiguration; 
    import org.Springframework.test.context.testng. 
    AbstractTransactionalTestNGSpringContextTests; 
    import org.Springframework.transaction.annotation.Transactional; 
      
    import service.AccountService; 
    import domain.Account; 
      
    @ContextConfiguration("/config/Spring-db.xml") 
    @Transactional
    @ActiveProfiles("test") 
    public class AccountServiceTestNGTest extends
    AbstractTransactionalTestNGSpringContextTests { 
        @Autowired
        private AccountService service; 
      
        @org.testng.annotations.Test 
        public void testGetAcccountById() { 
            Account acct = Account.getAccount(1?? "user01"?? 18?? "M"); 
            service.insertIfNotExist(acct); 
            Account acct2 = service.getAccountById(1); 
            assertEquals(acct??acct2); 
        } 
    }