????Spring ?????????

??????????????????? Spring test framework ?????л??? Junit4 ????????????????????????????????????????????????????Щ????÷???

????@ContextConfiguration ?? @Configuration ?????

????????????????????????? Spring xml ?????λ???Spring test framework ????????? XML ???????? application context????????????? Spring 3.0 ?????????? @Configuration?????????????????? Java ?????????? bean ?????????????

???????????????漲??? Spring-db1.xml ??????????????????е????? bean??initer??accountDao??accountService ??????????????壬?????? XML?????????????????????????

????????????????? @Configuration?????? classpath ?м??? cglib ?? jar ????cglib-nodep-2.2.3.jar????????????

?????嵥 8. SpringDb2Config.Java

    package config; 
      
    import org.Springframework.beans.factory.annotation.Autowired; 
    import org.Springframework.context.annotation.Bean; 
    import org.Springframework.context.annotation.Configuration; 
    import org.Springframework.jdbc.datasource.DriverManagerDataSource; 
      
    import service.AccountService; 
    import service.Initializer; 
    import DAO.AccountDao; 
      
    @Configuration
    public class SpringDb2Config { 
        private @Autowired DriverManagerDataSource datasource; 
        @Bean
        public Initializer initer() { 
            return new Initializer(); 
        } 
      
        @Bean
        public AccountDao accountDao() { 
    AccountDao DAO = new AccountDao(); 
    DAO.setDataSource(datasource); 
    return DAO; 
        } 
      
        @Bean
        public AccountService accountService() { 
    return new AccountService(); 
        } 
    }

???????????????????

?????? @Configuration???????????????? Spring ???????? Spring ?? bean ???壬???Ч??????? XML ???÷???

?????? @Bean?????????????????? bean ????壬??????£??????????? bean ?? Id

?????? @Autowired????? datasource ????????????????

????????????????? XML+config bean ?????????????????????????????????????????? Spring ????????????仯???????嵥?????

?????嵥 9. Spring-db2.xml

    <beans xmlns="http://www.Springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.Springframework.org/schema/context"
     xsi:schemaLocation="http://www.Springframework.org/schema/beans 
      
    http://www.Springframework.org/schema/beans/Spring-beans-3.0.xsd 
      
      
    http://www.Springframework.org/schema/context 
      
      
    http://www.Springframework.org/schema/context/Spring-context-3.0.xsd">
      
     <context:annotation-config/>
     <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>
     <bean id="transactionManager"
             >
             <property name="dataSource" ref="datasource"></property>
         </bean>
      
         <bean/>
     </beans>

???????????? context ??????????壬??????к???????????????????? <context:annotaiton-config/> ????壬????????????????????????????? Spring ????????????????? beans ?????? initer??accountDao ?? accountService ??Щ bean ????壬????????????? SpringDb2Config bean ????壬?????? bean ?????????????????????á?

??????????????Щ??????????????????????????£??????????????????Ч???????£?

@ContextConfiguration("/config/Spring-db2.xml")

??????????????????????????????????? Spring ?????????н????????? green bar??

????@DirtiesContext

??????????£?Spring ????????????? applicationContext ?????????棬???????????

???????? Spring ??????????????? applicationContext ????壬?????????????? applicationContext???????? registerSingleton ???????????????μ? bean????????????£???????????? Spring ???????????? applicationContext????????????????????????????????????????? applicationContext??????????????????????????????????@DirtiesContext?????????£?

?????? ????????????????????????????????????????????? applicationContext

?????? ?????????????????????????????????????????? applicationContext