Spring DI的3种方式
2015-07-28 11:27
495 查看
前面讲解IOC和DI入门的时候,对表现层依赖的service对象,使用了setter方法进行注入,这里对依赖注入的3中方式进行深入分析。
依赖注入的3中方式:
1、使用构造器注入
2、使用setter方法注入
3、使用接口注入
________________________________________________________________________________________________________
构造器注入
Car类:
applicationContext.xml
Test:
输出:
构造器注入需在bean标签中,设置<constructor-arg>标签,<constructor-arg>标签有以下几个属性:
index 代表参数顺序 ,第一个参数 0
type 代表参数类型
name 代表参数的名称
value 注入参数的值
ref 引用另一个bean元素的id
一般用index和type区分不同的构造器。
________________________________________________________________________________________________________
setter方法注入
Employee类:
applicationContext.xml:
Test
输出:
<property>标签为setter方法注入方法提供实现,<property>标签的属性有:
name 属性名称 (由setter方法获得)
value 注入参数的值
ref 引用另一个Bean元素的id
Spring配置文件支持构造参数属性注入和
setter方法属性注入!
依赖注入的3中方式:
1、使用构造器注入
2、使用setter方法注入
3、使用接口注入
________________________________________________________________________________________________________
构造器注入
Car类:
public class Car { private String name; private int price; public Car(String name, int price) { this.name = name; this.price = price; } @Override public String toString() { return "Car [Name: " + name+ ", Price: "+ price + "]"; } }
applicationContext.xml
<bean id="car" class="cn.itcast.spring.e_di.Car"> <constructor-arg index="0" type="java.lang.String" value="宝马"></constructor-arg> <constructor-arg index="1" type="int" value="10000"></constructor-arg> </bean>
Test:
@Test public void test(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Car car = (Car) applicationContext.getBean("car"); System.out.println(car); }
输出:
构造器注入需在bean标签中,设置<constructor-arg>标签,<constructor-arg>标签有以下几个属性:
index 代表参数顺序 ,第一个参数 0
type 代表参数类型
name 代表参数的名称
value 注入参数的值
ref 引用另一个bean元素的id
一般用index和type区分不同的构造器。
________________________________________________________________________________________________________
setter方法注入
Employee类:
public class Employee { private long id; private String name; public void setId(long id) { this.id = id; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Employee [id: " + id + ", name: "+name + "]"; } }
applicationContext.xml:
<bean id="employee" class="cn.itcast.spring.e_di.Employee"> <property name="id" value="1111"></property> <property name="name" value="啊哈"></property>
</bean>
Test
@Test public void test1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Employee employee = (Employee) applicationContext.getBean("employee"); System.out.println(employee); }
输出:
<property>标签为setter方法注入方法提供实现,<property>标签的属性有:
name 属性名称 (由setter方法获得)
value 注入参数的值
ref 引用另一个Bean元素的id
Spring配置文件支持构造参数属性注入和
setter方法属性注入!
相关文章推荐
- Java IO学习【14】读取键盘录入
- JavaWeb学习——文件上传和下载
- SpringMVC入门学习体验(1)基于xml的MultiActionController
- Spring工作原理
- window下在同一台机器上安装多个版本jdk,修改环境变量不生效问题处理办法
- 读Spring的源代码五:AutoWired是如何实现注入的
- JAVA中类、实例与Class对象
- 深入阅读java api之URL
- 读Spring的源代码四:获取bean实例的过程
- javac和java
- java web项目命令打war包
- eclipse 项目视图问题 关于包与文件夹
- 【java】Windows7 下环境变量设置
- Java解析XML文档方式一—DOM
- Java工程师笔试题
- java判断图片的编码格式
- JAVA正则表达式30分钟入门 && 正则表达式速查表
- myeclipse中连接mysql数据库
- java.math.BigInteger cannot be cast to java.lang.Long
- Birt报表调用java程序