spring框架的学习-DI依赖注入

依赖注入的概念

  • 给属性赋值,就是依赖注入。

主要包括以下几种赋值方式

  1. 给基本属性赋值
  2. 给引用类型赋值
  3. 给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的意义

  1. 创建对象
  2. 给对象属性赋值

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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值