依赖注入的概念
- 给属性赋值,就是依赖注入。
主要包括以下几种赋值方式
- 给基本属性赋值
- 给引用类型赋值
- 给List,Map,Set,Properties赋值
Xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="person" class="com.lzl.test.di.Person">
<!--
property表示对象的属性
long或者string类型等基本类型,用value赋值
引用类型用ref赋值
-->
<property name="puId" value="5"></property>
<property name="pname" value="lisi"></property>
<property name="stu">
<ref bean="stu"/>
</property>
<property name="plist">
<list>
<value>h1</value>
<value>h2</value>
<ref bean="stu"></ref>
</list>
</property>
<property name="pmap">
<map>
<entry key="map1">
<value>hhh</value>
</entry>
<entry key="map2">
<value>xxx</value>
</entry>
<entry key="map3">
<ref bean="stu"/>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="pro1">899</prop>
<prop key="pro2">444</prop>
<prop key="pro3">8555</prop>
</props>
</property>
</bean>
<bean id="stu" class="com.lzl.test.di.Student"></bean>
</beans>
Java类
Person类
/**
*
* @author lzl
* @since 5.0
*/
public class Person {
private Long puId;
private String pname;
private Student stu;
private List plist;
private Map pmap;
private Properties properties;
public List getPlist() {
return plist;
}
public void setPlist(List plist) {
this.plist = plist;
}
public Map getPmap() {
return pmap;
}
public void setPmap(Map pmap) {
this.pmap = pmap;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Long getPuId() {
return puId;
}
public void setPuId(Long puId) {
this.puId = puId;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Student getStu() {
return stu;
}
public void setStu(Student stu) {
this.stu = stu;
}
@Override
public String toString() {
return "Person [puId=" + puId + ", pname=" + pname + ", stu=" + stu + "]";
}
}
Student类
public class Student {
public void sayStudent(){
System.out.println("=我不是一名学生了!=");
}
}
测试类
public class DependenceInTest extends SpringHelper{
static{
path = "org/springframework/jmx/helloWorldForDI.xml";
}
@Test
public void createBean(){
Person p = (Person) this.fileResource.getBean("person");
System.out.println(p.toString());
p.getStu().sayStudent();
System.out.println(p.getPlist());
System.out.println(p.getPmap());
System.out.println(p.getProperties());
}
}
IOC和DI的意义
- 创建对象
- 给对象属性赋值
Xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="person" class="com.lzl.test.diandioc.Person">
<!--
constructor-arg给构造函数赋值
index 传入参数的位置 ,从0开始
type 参数的类型
value 给基本类型赋值
ref 给引用类型赋值
注意
只能指定一个构造函数
-->
<constructor-arg index="0" type="java.lang.String" value="hhh"></constructor-arg>
<constructor-arg index="1" ref="pstu"></constructor-arg>
</bean>
<bean id="pstu" class="com.lzl.test.di.Student"></bean>
</beans>
Person.java文件
public class Person {
private Long pid;
private String pname;
private Student pstu;
public Person(String name,Student stu){
this.pname = name;
this.pstu = stu;
}
public Student getPstu() {
return pstu;
}
public void setPstu(Student pstu) {
this.pstu = pstu;
}
@Override
public String toString() {
return "Person [pid=" + pid + ", pname=" + pname + ", pstu=" + pstu + "]";
}
}
Test文件
public class DIAndIOCTest extends SpringHelper{
static{
path="org/springframework/jmx/personBean.xml";
}
@Test
public void test(){
Person p = (Person) this.fileResource.getBean("person");
p.getPstu().sayStudent();
System.out.println(p.toString());
}
}
面向接口编程
定义一个Document接口,定义一个read和write方法。
分别有WordDocument、PDFDocument、ExecleDocument实现这个接口。
并有DocumentManage类来管理这些实现类的创建。使用SpringIOC和DI来管理这些对象的创建。
Xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="documentManage" class="com.lzl.test.document.DocumentManage">
<!--
document是一个接口
-->
<property name="document">
<!-- 通过ref给接口赋值 -->
<ref bean="wordDocument"/>
</property>
</bean>
<bean id="wordDocument" class="com.lzl.test.document.WordDocument"></bean>
<bean id="pdfDocument" class="com.lzl.test.document.PDFDocument"></bean>
</beans>
Java类
Document接口
public interface Document {
void read();
void write();
}
实现类 这里只列举一个。
public class WordDocument implements Document{
@Override
public void read() {
System.out.println("Word Read");
}
@Override
public void write() {
System.out.println("Word Write");
}
}
管理类
public class DocumentManage{
private Document document;
public void read(){
this.document.read();
}
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}
public void write(){
this.document.write();
}
}
测试类
public class DocumentTest extends SpringHelper{
static{
path="org/springframework/jmx/documentTest.xml";
}
@Test
public void test(){
DocumentManage dm = (DocumentManage) this.fileResource.getBean("documentManage");
dm.read();
}
}