所谓的依赖注入,就指给对象的属性赋值,有两种方式可以实现:
public class Person{
private Long pid; private String pname; private Student student; private Set sets; private List lists; private Map map; private Properties properties;
// set get方法
} |
一.使用构造器进行注入:constructor-arg
public
Person(String pname,Student student){
this.pname= pname;
this.student= student;
}
|
2.在配置文件中配置:
<
bean
id="student_con"class="cn.itheima03.spring.di.constructor.Student"></bean>
<beanid="person_con"class="cn.itheima03.spring.di.constructor.Person">
<!--
构造器的参数
index
参数的位置,从0开始计算
type
参数的类型
value
给基本属性赋值
ref
给引用类型赋值
-->
<constructor-argindex="0"value="王二麻子"></constructor-arg>
<constructor-argindex="1"ref="student_con"></constructor-arg>
</bean>
|
3.测试
ApplicationContext context =
newClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person)context.getBean(
"person_con"
);
|
二.利用set方法进行注入:
<
bean
id="student"class="cn.itheima03.spring.di.Student"></bean>
<
bean
id="person"class="cn.itheima03.spring.di.Person">
<!-- property用来描述属性 -->
<!-- 基本属性 -->
<propertyname="pid"value="1"></property>
<propertyname="pname"value="王二麻子"></property>
<!-- 引用属性 -->
<propertyname="student">
<refbean="student"/>
</property>
<!--集合 -->
<propertyname="sets">
<set>
<value>set1</value>
<value>set2</value>
<refbean="student"/>
</set>
</property>
<propertyname="lists">
<list>
<value>list1</value>
<value>list2</value>
<refbean="student"/>
</list>
</property>
<propertyname="map">
<map>
<entrykey="key1">
<value>map1</value>
</entry>
<entrykey="key2">
<refbean="student"/>
</entry>
</map>
</property>
<propertyname="properties">
<props>
<propkey="prop1">
prop1
</prop>
</props>
</property>
</
bean
>
|
三.使用IOC和DI的例子:
1.Document接口即各个实现类:
Document.java:
public
interfaceDocument {
public
voidread();
public
voidwrite();
}
|
WrodDocument.java:
public
classWordDocumentimplementsDocument{
public
voidread() {
System.
out.println("read word");
}
public
voidwrite() {
System.
out.println("write word");
}
}
|
ExcelDocument.java:
public
classExcelDocumentimplementsDocument{
public
voidread() {
System.
out.println("read excel");
}
public
voidwrite() {
System.
out.println("write excel");
}
}
|
DocumentManager.java:
public
classDocumentManager {
private
Documentdocument;
public
DocumentManager(Document document){
this.document= document;
}
public
DocumentManager){}
public
voidsetDocument(Document document){
this.document= document;
}
public
voidread(){
this.document.read();
}
public
voidwrite(){
this.document.write();
}
}
|
2.配置文件:
<
bean
id="documentManager"class="com.oterman.practice.DocumentManager">
<!-- <constructor-argindex="0"ref="excelDocument"></constructor-arg> -->
<propertyname="document"ref="wordDocment"></property>
</
bean
>
<
bean
id="wordDocment"class="com.oterman.practice.WordDocument"></bean>
<
bean
id="excelDocument"class="com.oterman.practice.ExcelDocument"></bean>
|
3.测试:
/**
* 传统方式
*/
@Test
public
voidtest(){
Document document=
newWordDocument();
DocumentManager dm=
newDocumentManager(document);
dm.read();
}
/**
* 使用spring来完成,真正做到了面向接口编程。
*/
@Test
public
voidtest_spring(){
ApplicationContext context=
newClassPathXmlApplicationContext("applicationContext.xml");
DocumentManager documentManager=(DocumentManager) context.getBean(
"documentManager"
);
documentManager.read();
}
|
四.继承机制
spring容器的继承机制:
两个属性:parent,指明父类
abstract,表示不为该类创建对象。
上述的parent:让student_extends拥有person_extends的属性的值 abstract:不让person_extends创建对象
两个属性:parent,指明父类
abstract,表示不为该类创建对象。
<bean id="person_extends" class="cn.itheima03.spring.extend.Person" abstract="true"> <property name="s" value="aaaa"></property> </bean> <bean id="student_extends" class="cn.itheima03.spring.extend.Student" parent="person_extends"> </bean> |