美文网首页
Spring学习day-62:IOC和DI

Spring学习day-62:IOC和DI

作者: 开源oo柒 | 来源:发表于2019-10-17 22:29 被阅读0次

一、Spring的简介

1.什么是Spring框架?

Spring框架是一个开放源代码的J2EE应用程序框架,由Rod Johnson发起,是针对bean的生命周期进行管理的轻量级容器。

2.使用Spring需要导入的jar包:

Spring-core.jar: 这个jar文件包含Spring框架基本的核心工具类;
spring-beans.jar:这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。
spring-context.jar:这个jar文件为Spring核心提供了大量扩展;
Spring-expression-4.1.6.RELEASE.jar:
Commons-logging-1.1.3.jar:日志使用sprin框架必须的jar包;

3.Spring学习框架的核心:

IOC:控制反转---帮助我们创建对象的 ;
AOP:面向切面---提升代码的扩展性 ;
TX:声明式事务---事务管理 ;

二、SpringIOC的实现

1.责任链开发的问题:

基于责任链开发模式,我们发现代码层和层之间相互调用,造成了层和层的耦合性太高了 。 我们写代码的时候讲究的原则--低耦合 高内聚 。

2.解决的方案:

使用Spring IOC : 控制反转 。

3.代码实现:

(1)导包:


需要导的包

(2)创建java类:

package com.zlw.spring;

public class Student {
    private int id;
    private int age;
    private String name;

    public Student(int id, int age, String name) {
        super();
        this.id = id;
        this.age = age;
        this.name = name;
    }
    public Student(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public Student( String name,int id) {
        super();
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Student(){
        System.out.println("对象创建");
    }
    
    public void eat(){
        System.out.println("eat()---");
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", age=" + age + ", name=" + name + "]";
    }
    
}

(3)编写applicationContext.xml配置文件:

<?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="stu" class="com.zlw.spring.Student">

    </bean>
    <!-- 使用有参构造 Studnet stu1 = new Student(01,18,"张三") -->
    <bean id="stu1" class="com.zlw.spring.Student">
        <constructor-arg name="id" value="01"></constructor-arg>
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
    </bean>
</beans>  

4.工厂设计模式:

设计模式是为了解决某一类问题的产生工厂模式就是批量生产对象的。

  • 代码实现:
    (1)创建Java类:
package com.zlw.spring2;

public interface Proper {
    
    public void eat();
    
    public void run();
}
package com.zlw.spring2;

public class Student implements Proper{
    
    public Student() {
        System.out.println("Student对象创建");
    }

    public void eat() {
        System.out.println("Student中的eat方法");
        
    }

    public void run() {
        System.out.println("Student中的run方法");
    }

}

package com.zlw.spring2;

public class Teacher implements Proper {
    
    public Teacher() {
        System.out.println("Teacher对象创建");
    }

    public void eat() {
        System.out.println("Teacher中的eat方法");
        
    }

    public void run() {
        System.out.println("Teacher中的run方法");
    }
}
package com.zlw.spring2;

public class Factory {
    public Proper getInstance(String param) {
        if("stu".equals(param)) {
            return new Student();
        }else if("tea".equals(param)) {
            return new Teacher();
        }
        return null;
        
    }
    public static Proper getInstance2(String param) {
        if("stu".equals(param)) {
            return new Student();
        }else if("tea".equals(param)) {
            return new Teacher();
        }
        return null;
    }
}

(2)编写applicationContext配置文件:

<!-- 使用工厂模式 -->
    <!-- Factory.getInstance('stu') -->
    <bean id="factory" class="com.zlw.spring2.Factory">
    </bean>
    <bean id="be" factory-bean="factory" factory-method="getInstance">
        <constructor-arg name="param" value="stu"></constructor-arg>
    </bean>
    <!-- Factory.getInstance2('stu') -->
    <bean id="be2" class="com.zlw.spring2.Factory" factory-method="getInstance2">
        <constructor-arg name="param" value="tea"></constructor-arg>
    </bean>

(3)测试:

public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        Proper proper = app.getBean("be",Proper.class);
        proper.eat();
    }
结果

5.使用构造器:

  • 代码示例:
    (1)创建java类:
package com.zlw.spring3;

public class Student {
    private String name;
    private String sex;
    private int age;
    
    private Clazz clazz;

    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Student(String name, String sex, int age, Clazz clazz) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.clazz = clazz;
    }

    public Student(Clazz clazz) {
        super();
        this.clazz = clazz;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Clazz getClazz() {
        return clazz;
    }

    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", sex=" + sex + ", age=" + age + ", clazz=" + clazz + "]";
    }
    
}

(2)编写applicationContext.xml配置文件:

<!-- 使用无参构造 -->
    <bean id="stu" class="com.zlw.spring.Student">

    </bean>
    <!-- 使用有参构造 Studnet stu1 = new Student(01,18,"张三") -->
    <bean id="stu1" class="com.zlw.spring.Student">
        <constructor-arg name="id" value="01"></constructor-arg>
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
    </bean>

(3)测试:

public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext2.xml");
        Student student = app.getBean("stu",Student.class);
        System.out.println(student);
        
        Student student2 = app.getBean("stu2",Student.class);
        System.out.println(student2);
}
结果.png

三、SpringDI注入

1.为什么使用DI注入?

给创建好的对象中的全局的属性或者对象进行赋值的操作 。

  • 为什么称DI和IoC是同一个事情?

Spring帮助创建对象的过程叫做IoC,创建对象时给对象中全局对象赋值叫做DI,所以认为IoC和DI是同一个事情。

  • DI的意义 :

解除类与类之间高耦合性,给对象中全局对象赋值的操作 ?

2.注入的方式:

  • 使用set方法:
    (1)代码示例(分别生成getter和setter方法及构造方法):
private String name;
    private String sex;
    private int age;
    
    private Clazz clazz;
private String cname;
private int clazzno;

(2)编写applicationContext.xml配置文件:

  <!-- 使用set方法 进行DI注入
       name:对象的属性名        
        value和ref的使用场景        
      value: 如果注入的属性类型是基本数据类型(包含String)使用value        
      ref:如果注入的属性类型是对象这个时候使用ref
-->
     <bean id="stu2" class = "com.zlw.spring3.Student">
     <property name="name" value="王五"></property>
     <property name="age" value="20"></property>
     <property name="sex" value="男"></property>
     <property name="clazz" ref="clazz"></property>
     
     </bean>

(3)测试:

public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext2.xml");
        Student student3 = app.getBean("stu3",Student.class);
        System.out.println(student3);
}
  • 使用构造器:
    (1)代码示例:
private String name;
    private String sex;
    private int age;
    
    private Clazz clazz;
private String cname;
private int clazzno;

(2)编写applicationContext.xml配置文件:

 <!-- 使用构造方法注入 -->
     <bean id="stu" class="com.zlw.spring3.Student">
     <constructor-arg name="name" value="李四"></constructor-arg>
     <constructor-arg name="age" value="18"></constructor-arg>
     <constructor-arg name="sex" value="女"></constructor-arg>
     <constructor-arg name="clazz" ref="clazz"></constructor-arg>
     </bean>
  • 自动注入:
    (1)applicationContext.xml配置文件的编写:
<!--
 底层走的是set方法      
byName:这个时候就会在当前的xml中寻找【bean的ID名称】和需要注入实体中的【属性名】一致,进行匹配注入      
byType:这个时候就会在当前的xml中寻找【bean标签的类型】和需要注入实体中的【属性的类型】一致,进行匹配注入    
 底层走的是构造器     
constructor:这个时候首先会根据[有参构造器的形参名]名称进行查找,如果名称没有一致的,在根据类型[有参构造器的类型]进行查找      
需要注意:*在指定的类中必须提供合适的有参构造器才可以
 -->
<bean id="stu3" class="com.zlw.spring3.Student" autowire="byType">
     </bean>

3.其他类型的DI注入:

  • 数组:
<property name="arr">
            <array>
                <value>A</value>
                <value>B</value>
                <value>C</value>
            </array>
        </property>
  • List:
<property name="list">
            <list>
                <value>a</value>
                <value>b</value>
                <value>c</value>
            </list>
        </property>
  • set:
<property name="set">
            <set>
                <value>1</value>
                <value>2</value>
                <value>1</value>
                <value>3</value>
            </set>
        </property>
  • map:
<property name="map">
            <map>
                <entry>
                    <key><value>A</value></key>
                    <value>1</value>
                </entry>
                <entry>
                    <key><value>B</value></key>
                    <value>2</value>
                </entry>
                <entry>
                    <key><value>C</value></key>
                    <value>3</value>
                </entry>
            </map>
  • Properties:
<property name=" properties">
        <props>
            <prop key="A"> 1</prop>
            <prop key="B">2 </prop>
        </props>
     </property>

相关文章

网友评论

      本文标题:Spring学习day-62:IOC和DI

      本文链接:https://www.haomeiwen.com/subject/hlqbmctx.html