Spring IOC简单注入例子,本例子使用JUnit进行测试。Spring版本:3.2
项目结构:
Spring所需引用的JAR包:
Spring XML配置:
springContext.xml
<?xml version="1.0" encoding="UTF-8"?> <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.xsd"> <import resource="dao.xml"/> <import resource="service.xml"/> </beans>
dao.xml
<?xml version="1.0" encoding="UTF-8"?> <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.xsd"> <bean name="accountDaoFactory" class="com.my.dao.AccountFactory" abstract="true"></bean> <bean name="accountDAO" class="com.my.dao.mysql.AccountDAO" parent="accountDaoFactory"></bean> </beans>
service.xml
<?xml version="1.0" encoding="UTF-8"?> <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.xsd"> <bean name="accountServiceFactory" class="com.my.service.AccountServiceFactory" abstract="true"></bean> <bean name="accountService" class="com.my.service.mysql.AccountService" parent="accountServiceFactory"> <property name="account" ref="accountDAO"></property> </bean> </beans>
DAO的java class:
package com.my.dao; public abstract class AccountFactory { /* * Fin account by id */ public abstract Integer findAccount(Integer accountID); }
package com.my.dao.mysql; public class AccountDAO extends com.my.dao.AccountFactory { /* * Find account by account id */ @Override public Integer findAccount(Integer accountID) { return accountID; } }
Service的java class:
package com.my.service; public abstract class AccountServiceFactory { protected com.my.dao.AccountFactory account; public com.my.dao.AccountFactory getAccount() { return account; } public void setAccount(com.my.dao.AccountFactory account) { this.account = account; } /* * Find account by id */ public abstract Integer finAccount(Integer accountID); }
package com.my.service.mysql; public class AccountService extends com.my.service.AccountServiceFactory { @Override public Integer finAccount(Integer accountID) { return account.findAccount(accountID); } }
测试类:
package com.my.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.my.service.AccountServiceFactory; public class SpringTest { private ApplicationContext context = new ClassPathXmlApplicationContext("springContext.xml"); public SpringTest(){ } public Integer findAccount(Integer accountID){ AccountServiceFactory account = (AccountServiceFactory)context.getBean("accountService"); Integer id = account.finAccount(100); return id; } }
JUnit测试:
package com.my.test; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; public class SpringTestTest { private SpringTest sp = new SpringTest(); @Before public void setUp() throws Exception { } @Test public void testFindAccount() { Integer accountID = 100; Integer result = sp.findAccount(accountID); assertEquals((Integer)100, result); System.out.println(result); } }
运行JUnit测试结果: