日期:2016-9-5
内容:Spring的注入方式
一、什么Spring的注入?
1、Spring注入:
2、设值注入:
3、构造注入:
二、设值注入的代码演示:
1、testInjectionDao.java:
package com.springIoc.dao;
/**
* @author Administrator:
* @version 创建时间:2016-9-5 下午2:57:10
* 类说明
*/
public interface TestInjectionDao {
//保存数据
public void save(String data);
}
2、testInjectionDaoImpl.java:
package com.springIoc.dao.Impl;
import com.springIoc.dao.TestInjectionDao;
/**
* @author Administrator:
* @version 创建时间:2016-9-5 下午2:58:06
* 类说明
*/
public class TestInjectionDaoImpl implements TestInjectionDao{
@Override
public void save(String data) {
System.out.println("保存的数据: "+data);
}
}
3、testInjectionService.java:
package com.springIoc.service;
/**
* @author Administrator:
* @version 创建时间:2016-9-5 下午3:00:49
* 类说明
*/
public interface TestInjectionService {
//模拟保存数据
public void save(String data);
}
4、testInjectionServiceImpl.java:
package com.springIoc.service.Impl;
import com.springIoc.dao.TestInjectionDao;
import com.springIoc.service.TestInjectionService;
/**
* @author Administrator:
* @version 创建时间:2016-9-5 下午3:01:48
* 类说明
*/
public class TestInjectionServiceImpl implements TestInjectionService {
<span style="color:#ff0000;">//引入dao
private TestInjectionDao testInjectionDao;
//提供setter方法用于配置设值注入
public void setTestInjectionDao(TestInjectionDao testInjectionDao) {
this.testInjectionDao = testInjectionDao;
}</span>
@Override
public void save(String data) {
System.out.println("接收的参数为: "+data);
//处理业务逻辑
data = data+":"+data.hashCode();
//调用DAO层保存数据
testInjectionDao.save(data);//--->配置applicationContext.xml配置文件
}
}
如上代码中的红色部分为设值注入。
5、applicationContext.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="testInjectionDao" class="com.springIoc.dao.Impl.TestInjectionDaoImpl"></bean>
<bean id="testInjectionService" class="com.springIoc.service.Impl.TestInjectionServiceImpl">
<!-- 设值注入,必须在对应的class张提供需要注入对象的setter方法 -->
<property name="testInjectionDao" ref="testInjectionDao"></property>
</bean>
</beans>
6、单元测试类TestSpringIoc.java:
package com.spring.test;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.opensymphony.xwork2.interceptor.annotations.After;
import com.springIoc.service.TestInjectionService;
/**
* @author Administrator:
* @version 创建时间:2016-9-5 下午3:11:23
* 类说明
*/
public class TestSpringIoc {
private ApplicationContext ac;
private TestInjectionService testInjectionService;
@Before
public void before()
{
//加载Spring的配置文件
ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//获得Bean
testInjectionService = (TestInjectionService) ac.getBean("testInjectionService");
System.out.println("初始化完成......");
}
@Test
public void testSpringIoc()
{
String data = "我是测试数据...";
//测试数据
testInjectionService.save(data);
}
@After
public void after()
{
System.out.println("执行完成....");
}
}
①、需要注入哪一个类的对象,就在被注入的这个类中设值这个类的setter方法。
②、在Spring配置文件中添加<property name="" ref=""/>来注入。
三,构造注入的代码演示:
构造注入相对于设值注入的不同点在于,设值注入实在被注册的类中提供注入类的setter函数,而构造注入是在被注入类的构造器中提供注入类的构造。大体代码相同,区别在于如下的两个改动:
1、TestInjectionService.java中添加构造器。
package com.springIoc.service.Impl;
import com.springIoc.dao.TestInjectionDao;
import com.springIoc.service.TestInjectionService;
/**
* @author Administrator:
* @version 创建时间:2016-9-5 下午3:01:48
* 类说明
*/
public class TestInjectionServiceImpl implements TestInjectionService {
//设值注入开始...............................................
//引入dao
private TestInjectionDao testInjectionDao;
//提供setter方法用于配置设值注入
// public void setTestInjectionDao(TestInjectionDao testInjectionDao) {
// this.testInjectionDao = testInjectionDao;
// }
//设值注入结束...............................................
//构造注入开始...............................................
public TestInjectionServiceImpl(TestInjectionDao testInjectionDao)
{
this.testInjectionDao = testInjectionDao;
}
//构造注入结束...............................................
@Override
public void save(String data) {
System.out.println("接收的参数为: "+data);
//处理业务逻辑
data = data+":"+data.hashCode();
//调用DAO层保存数据
testInjectionDao.save(data);//--->配置applicationContext.xml配置文件
}
}
2、applicationContext.xml配置文件中添加构造注入:
<!-- 演示的构造注入 -->
<bean id="testInjectionDao" class="com.springIoc.dao.Impl.TestInjectionDaoImpl"></bean>
<bean id="testInjectionService" class="com.springIoc.service.Impl.TestInjectionServiceImpl">
<constructor-arg name="testInjectionDao" ref="testInjectionDao"></constructor-arg>
</bean>