SpringIoC与DI

Spring IoC与DI简介:

  • IoC不是一种技术而是一种设计思想,将原本在程序中手动创建对象的控制权交给Spring框架管理;
  • 正控:若要使用某个对象,需要自己手动的去负责对象的创建;
  • 反控:若要使用某个对象,只需从Spring框架中获取使用的对象,而不用去想它是怎么创建的,即将创建对象的控制权反转给了Spring框架;
  • DI是IoC的一种实现方式,即Spring创建对象的过程中,将将对象依赖属性(简单值,集合,对象)通过配置设置给该对象;

  • DI从注入方式上可以分为属性注入、构造器注入、接口注入;


SpringIoC底层实现:

/**
     * IOC底层实现
     */
    @org.junit.Test
    public void test2() throws Exception{
        //通过反射获取构造器进而获取对象
        String name="com.answer.server.User";
        Class clazz=Class.forName(name);
        Constructor constructor=clazz.getConstructor();
        constructor.setAccessible(true);
        Object object= constructor.newInstance();
        //通过内省机制设置属性值
        BeanInfo beanInfo= Introspector.getBeanInfo(clazz,Object.class);
        PropertyDescriptor[] pds=beanInfo.getPropertyDescriptors();
        for(PropertyDescriptor pd:pds){
            String propertyName=pd.getName();
            if("username".equals(propertyName)){
                pd.getWriteMethod().invoke(object,"www");
            }
        }
        user=(User) object;
        System.out.println(user.getUsername());
    }

 

 


SpringIoC:

SpringIOC(控制反转):把对象的创建,初始化,销毁等工作交给Spring容器来做。由Spring容器控制对象的生命周期。

Spring容器创建对象的方式:

A.默认情况下采用无参构造函数创建对象;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="spring1" class="com.answer.spring1.Spring1"></bean>
</beans>
public class Spring1 {
    public void method(){
        System.out.println("无参构造函数调用对象");
    }
}

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application1.xml");
        Spring1 spring1=(Spring1) applicationContext.getBean("spring1");
        spring1.method();
    }
}

B:Spring容器利用静态工厂方法创建类对象;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="spring2" class="com.answer.spring2.Spring2"></bean>
    <bean id="factory" class="com.answer.spring2.Factory" factory-method="getInstance"></bean>
</beans>
public class Factory {
    public static Spring2 getInstance(){
        return new Spring2();
    }
}

public class Spring2 {
    public Spring2(){
        System.out.println("new Spring2");
    }
    public void method(){
        System.out.println("ok");
    }
}

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application2.xml");
        Spring2 spring2=(Spring2)applicationContext.getBean("factory");
        spring2.method();
    }
}

C:Spring容器利用实例工厂方法创建对象;


SpringDI:

springDI:理解为依赖注入,俗称给属性赋值。

A:调用类的set和get方法:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="com.answer.spring3.Person">
    <property name="name" value="answer"></property>
    <property name="student" ref="student"></property>
</bean>
    <bean id="student" class="com.answer.spring3.Student"></bean>
</beans>
package com.answer.spring3;

public class Student {
    public void method(){
        System.out.println("student");
    }
}


public class Person {
    private String name;
    private Student student;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}



public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application3.xml");
        Person person=(Person) applicationContext.getBean("person");
        System.out.println(person.getName());
        person.getStudent().method();
    }
}

 

B:调用类的构造方法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="com.answer.spring4.Person">
    <constructor-arg index="0" type="java.lang.String" value="answer"></constructor-arg>
</bean>
</beans>

 

package com.answer.spring4;

public class Person {
    private String name;
    public Person(String name){
        this.name=name;
    }
    public void method(){
        System.out.println(this.name);
    }
}

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application4.xml");
        Person person=(Person) applicationContext.getBean("person");
        person.method();
    }
}

SpringIoC与DI的意义:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="documentmanager" class="com.answer.Spring5.DocumentManager">
    <constructor-arg index="0" ref="pdf"></constructor-arg>
</bean>
    <bean id="pdf" class="com.answer.Spring5.PDFDocument"></bean>
    <bean id="excel" class="com.answer.Spring5.ExcelDocument"></bean>
</beans>

 

package com.answer.Spring5;

public interface Document {
    public void read();
    public void write();
}

public class ExcelDocument implements Document {
    @Override
    public void read() {
        System.out.println("read Excel");
    }

    @Override
    public void write() {
        System.out.println("write Excel");
    }
}

public class PDFDocument implements Document {
    @Override
    public void read() {
        System.out.println("read PDF");
    }

    @Override
    public void write() {
        System.out.println("write PDF");
    }
}

public class DocumentManager {
    private Document document;
    public DocumentManager(Document document){
        this.document=document;
    }
    public void read(){
        this.document.read();
    }
    public void write(){
        this.document.write();
    }
}


public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application5.xml");
        DocumentManager manager=(DocumentManager) applicationContext.getBean("documentmanager");
        manager.read();
        manager.write();
    }
}

 

执行结果:

read PDF
write PDF

由此可以得出:Spring的IOC与DI的完美结合,是其做到了真正的面向接口编程。如果我们想读写Excel或者Word,只需要修改配置文件中的传入参数即可。而传统的方式是不完全面向接口的编程,把实现类暴露给客户端了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dream答案

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值