工具準(zhǔn)備
- myeclipse(10.7.1)
創(chuàng)建一個(gè)java web項(xiàng)目并添加spiing支持(或打開一個(gè)項(xiàng)目)
實(shí)現(xiàn)的業(yè)務(wù)邏輯
image.png
- 上圖中InjectionServiceImpl有InjectionDAO的變量到踏,當(dāng)沒有主動(dòng)去創(chuàng)建蝠猬,而是通過配置文件配置抛丽,讓spring幫我們?nèi)ネ瓿?/li>
- 目標(biāo)1通過xml文件配置實(shí)現(xiàn)設(shè)置注入
工程目錄結(jié)構(gòu)展示
image.png
為項(xiàng)目添加spring環(huán)境支持
image.png
此時(shí)myeclipse自動(dòng)為我們引入spring的jar包
并自動(dòng)創(chuàng)建applicationContext.xml(spring的配置文件)
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
</beans>
參考:Eclipse/MyEclipse上配置Spring環(huán)境
創(chuàng)建InjectionDAO接口及其實(shí)現(xiàn)類InjectionDAOImpl
InjectionDAO.java
package com.younghare.interfaces;
public interface InjectionDAO {
public void save(String arg);
}
實(shí)現(xiàn)類InjectionDAOImpl .java
package com.younghare.Impl;
import com.younghare.interfaces.InjectionDAO;
public class InjectionDAOImpl implements InjectionDAO {
@Override
public void save(String arg) {
//模擬保存數(shù)據(jù)到數(shù)據(jù)庫
System.out.println("模擬保存數(shù)據(jù)到數(shù)據(jù)庫:"+arg);
}
}
創(chuàng)建InjectionService接口及其實(shí)現(xiàn)類InjectionServiceImpl
InjectionService.java
package com.younghare.interfaces;
public interface InjectionService {
public void save(String arg);
}
InjectionServiceImpl.java(實(shí)現(xiàn)類)
package com.younghare.Impl;
import com.younghare.interfaces.InjectionDAO;
import com.younghare.interfaces.InjectionService;
public class InjectionServiceImpl implements InjectionService {
private InjectionDAO injectionDAO; //模擬設(shè)值注入時(shí)需要為其生成set方法
//sprint Ioc容器 的設(shè)值注入需要這個(gè)set方法
public void setInjectionDAO(InjectionDAO injectionDAO) {
this.injectionDAO = injectionDAO;
}
@Override
public void save(String arg) {
//模擬業(yè)務(wù)操作
System.out.println("模擬業(yè)務(wù)操作");
//對數(shù)據(jù)進(jìn)行處理---模擬經(jīng)過業(yè)務(wù)邏輯處理
arg =arg +":"+ this.hashCode();
//調(diào)用DAO把數(shù)據(jù)保存到數(shù)據(jù)庫
injectionDAO.save(arg);
}
}
配置applicationContext.xml(對bean進(jìn)行設(shè)置注入配置)
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="injectionService" class ="com.younghare.Impl.InjectionServiceImpl">
<!--屬性name中的injectionDAO對應(yīng)的是InjectionServiceImpl成員變量的 -->
<property name="injectionDAO" ref ="injectionDAO"></property>
</bean>
<bean id="injectionDAO" class ="com.younghare.Impl.InjectionDAOImpl"></bean>
</beans>
image.png
注意:你也可以在其他文件中進(jìn)行配置,比如spring-injection.xml,主要是與ClassPathXmlApplicationContext參數(shù)對應(yīng)
ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
測試--創(chuàng)建SpringMain.java并實(shí)現(xiàn)main方法
測試代碼
package com.younghare.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.younghare.Impl.InjectionServiceImpl;
import com.younghare.interfaces.InjectionService;
public class SpringMain {
public static void main(String[] args) {
// 測試java bean婆咸,配置信息在applicationContext.xml文件中
ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
InjectionService injectionService = (InjectionService) appCtx.getBean("injectionService");
injectionService.save("這是要保持的數(shù)據(jù)");
}
}
運(yùn)行結(jié)果
image.png
現(xiàn)在實(shí)現(xiàn)構(gòu)造器注入
刪除xml配置文件中的設(shè)值注入的配置屯伞,并添加構(gòu)造器部分的注入
image.png
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!--構(gòu)造器注入 -->
<bean id="injectionService" class ="com.younghare.Impl.InjectionServiceImpl">
<constructor-arg name ="injectionDAO" ref ="injectionDAO"></constructor-arg>
</bean>
<bean id="injectionDAO" class ="com.younghare.Impl.InjectionDAOImpl"></bean>
</beans>
InjectionServiceImpl.java中添加InjectionServiceImpl構(gòu)造器injectionDAO的參數(shù)
image.png
再測試運(yùn)行結(jié)果也是
image.png
注意點(diǎn)如果構(gòu)造器參數(shù)的名稱定義不同與xml中饲化,則會(huì)報(bào)錯(cuò)--引用配置錯(cuò)誤
如把xml配置中改為injectionDAO3
xml配置引用與構(gòu)造器參數(shù)不一致
單元測試部分
在工程目錄中創(chuàng)建測試代碼管理目錄
myeclipse/new/Source Folder
image.png
創(chuàng)建單元測試類
在工程目錄中創(chuàng)建一個(gè)test類
myeclipse/new/other
image.png
image.png
SpringTest.java
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.younghare.interfaces.InjectionService;
public class SpringTest {
private static ApplicationContext context =null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//加載Spring 的配置文件
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void test() {
InjectionService injectionService =(InjectionService) context.getBean("injectionService");
injectionService.save("這是要保持的數(shù)據(jù)");
}
}
運(yùn)行測試用例
右鍵/Run as/Junit Test
image.png
單元測試2方法
單元測試基礎(chǔ)類UnitTestBase.java
package com.younghare;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//import org.springframework.util.StringUtils;
import org.apache.commons.lang.StringUtils;
public class UnitTestBase {
private ClassPathXmlApplicationContext context;
private String springXmlpath;
public UnitTestBase() {}
public UnitTestBase(String springXmlpath) {
this.springXmlpath = springXmlpath;
}
@Before
public void before() {
if (StringUtils.isEmpty(springXmlpath)) {
// springXmlpath = "classpath*:spring-*.xml";
springXmlpath = "classpath*:applicationContext*.xml";
}
try {
context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
context.start();
} catch (BeansException e) {
e.printStackTrace();
}
}
@After
public void after() {
context.destroy();
}
@SuppressWarnings("unchecked")
protected <T extends Object> T getBean(String beanId) {
try {
return (T)context.getBean(beanId);
} catch (BeansException e) {
e.printStackTrace();
return null;
}
}
protected <T extends Object> T getBean(Class<T> clazz) {
try {
return context.getBean(clazz);
} catch (BeansException e) {
e.printStackTrace();
return null;
}
}
}
InjectionServiceTest.java (測試測試構(gòu)造器注入)
package com.younghare;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.younghare.interfaces.InjectionService;
@RunWith(BlockJUnit4ClassRunner.class)
public class InjectionServiceTest extends UnitTestBase {
public InjectionServiceTest() {
//super();
super("classpath:applicationContext.xml");
}
//構(gòu)造器注入
@Test
public void testconstructor() {
InjectionService service = super.getBean("injectionService");
service.save("這是要保存的數(shù)據(jù)");
}
}