?????嵥 3. AccountService.Java

    package service; 
      
    import org.apache.commons.logging.Log; 
    import org.apache.commons.logging.LogFactory; 
    import org.Springframework.beans.factory.annotation.Autowired; 
      
    import DAO.AccountDao; 
    import domain.Account; 
      
    public class AccountService { 
        private static final Log log = LogFactory.getLog(AccountService.class); 
      
        @Autowired
        private AccountDao accountDao; 
      
        public Account getAccountById(int id) { 
            return accountDao.getAccountById(id); 
        } 
      
        public void insertIfNotExist(Account account) { 
            Account acct = accountDao.getAccountById(account.getId()); 
            if(acct==null) { 
                log.debug("No "+account+" found??would insert it."); 
                accountDao.saveAccount(account); 
            } 
            acct = null; 
        } 
      
    }

????AccountService ???????з?????

?????? getAccountById?????? Id ?????????
?????? insertIfNotExist?????????????????????

???????????? DAO ???? accountDao ????? Spring ????? @Autowired ????????

?????嵥 4. Spring ???????

??????????????????????????? Spring ???й???????????????£?

    <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="initer" init-method="init">
         </bean>
     <bean id="accountDao" depends-on="initer">
             <property name="dataSource" ref="datasource" />
         </bean>
     <bean id="accountService">
         </bean>
     </beans>

??????????е?“<context:annotation-config/>”???????????????????? Spring ?? Annotation ???????????????????????? @Autowired ??????????????????? Spring ???????????????????????????????????????????? accountDao ?????? initer bean?? ??? bean ??????????? log4j ??????????????????

???????????????????????? datasource ????壬?????????????? Spring Jdbc Template??????????????? org.Springframework.jdbc.datasource.DriverManagerDataSource ????? datasource ???ɡ?????????????????????? HSQL??Single Server ??????????? JDBC ???з???????????У?????????? Oracle ???? DB2??Mysql ???

??????????????????????????? Junit4 ?????? accountService ?????????£?

?????嵥 5. AccountServiceOldTest.Java

    package service; 
      
    import static org.Junit.Assert.assertEquals; 
      
    import org.Junit.BeforeClass; 
    import org.Junit.Test; 
    import org.Springframework.context.ApplicationContext; 
    import org.Springframework.context.support.ClassPathXmlApplicationContext; 
      
    import domain.Account; 
      
    public class AccountServiceOldTest { 
        private static AccountService service; 
      
        @BeforeClass
        public static void init() { 
            ApplicationContext 
    context = new ClassPathXmlApplicationContext("config/Spring-db-old.xml"); 
            service = (AccountService)context.getBean("accountService"); 
        }  
      
        @Test
        public void testGetAcccountById() { 
    Account acct = Account.getAccount(1?? "user01"?? 18?? "M"); 
            Account acct2 = null; 
            try { 
    service.insertIfNotExist(acct); 
                acct2 = service.getAccountById(1); 
                assertEquals(acct?? acct2); 
            } catch (Exception ex) { 
                fail(ex.getMessage()); 
            } finally { 
                service.removeAccount(acct); 
            } 
    } 
    }

???????????? Junit4 ????????????????? @BeforeClass???????????????????????????????????????????????? Spring ?? ClassPathXmlApplicationContext ?? XML ????м????????漲??? Spring ??????????????л???? accountService ????????????????? @Test ????????????????

??????????????????????? Account ????????????? service bean ??????????У??????? getAccountById ???????????????????????????????????ж?????????????????????????????????????????????????????????????????????????У????????????? try-catch-finally ??????????????????

??????в???????? Eclipse ?У??????? AccountServiceOldTest ????? Run as Junit test ???????????????£?

??????в??????

?????? Eclipse ?? Junit ????У??????????????μ?????

????? 2. ???????

???????????????? Spring test ?????е????????????????????????Щ??????

?????? ????????????????????? Spring ????????????????????? bean ???

?????? ????????????????????????????????????????????????????μ???????????????????????????