2016/1/16 10:02:30
1.Spring注入
- Spring注入是指在启动Spring容器加载bean配置的时候,完成对变量的赋值行为
常用的两种注入方式
-设值注入 调用类的set方法
-构造注入 构造器参数注入的方法
在IOC容器读取XML文档时,初始化bean的时候,为bean的成员变量赋值
设值注入的方式:
XML:spring-injection.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="injectionService" class="com.zjx.service.impl.InjectionServiceImpl">
<property name="injectionDao" ref="injectionDao"></property>
</bean>
<bean id="injectionDao" class="com.zjx.dao.impl.InjectionDaoImpl"></bean>
</beans>
DAO层
package com.zjx.dao.impl;
import com.zjx.dao.InjectionDao;
public class InjectionDaoImpl implements InjectionDao{
@Override
public void sava(String data) {
System.out.println("保存数据:"+data);
}
}
service层
package com.zjx.service.impl;
import com.zjx.dao.InjectionDao;
import com.zjx.service.InjectionService;
public class InjectionServiceImpl implements InjectionService {
private InjectionDao injectionDao;
/**
* @param injectionDao 持久化接口
* 设值注入injectionDao
*/
public void setInjectionDao(InjectionDao injectionDao) {
this.injectionDao = injectionDao;
}
@Override
public void save(String data) {
System.out.println("Service接受参数:"+data);
data = data+":"+this.hashCode();
injectionDao.sava(data);
}
}
测试:
package com.zjx.interfaces.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.zjx.service.InjectionService;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjection extends UnitTestBase {
public TestInjection() {
super("classpath:spring-injection.xml");
}
@Test
public void test(){
InjectionService service = super.getBean("injectionService");
service.save("这是要保存的数据");
}
}
构造器注入
XML:spring-injection.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="injectionService" class="com.zjx.service.impl.InjectionServiceImpl">
<constructor-arg name="injectionDao" ref="injectionDao"></constructor-arg>
</bean>
<bean id="injectionDao" class="com.zjx.dao.impl.InjectionDaoImpl"></bean>
</beans>
DAO层
package com.zjx.dao.impl;
import com.zjx.dao.InjectionDao;
public class InjectionDaoImpl implements InjectionDao{
@Override
public void sava(String data) {
System.out.println("保存数据:"+data);
}
}
service层
package com.zjx.service.impl;
import com.zjx.dao.InjectionDao;
import com.zjx.service.InjectionService;
public class InjectionServiceImpl implements InjectionService {
private InjectionDao injectionDao;
/**
* @param injectionDao 持久化接口
* 构造器注入
*/
public InjectionServiceImpl(InjectionDao injectionDao) {
this.injectionDao = injectionDao;
}
@Override
public void save(String data) {
System.out.println("Service接受参数:"+data);
data = data+":"+this.hashCode();
injectionDao.sava(data);
}
}
测试:
package com.zjx.interfaces.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.zjx.service.InjectionService;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjection extends UnitTestBase {
public TestInjection() {
super("classpath:spring-injection.xml");
}
@Test
public void test(){
InjectionService service = super.getBean("injectionService");
service.save("这是要保存的数据");
}
}