008Spring之IOC容器及基于xml的bean管理

1、IOC

1、什么是IOC

(1)控制反转,把对象创建和对象之间的调用过程,交给Spring进行管理
(2)使用IOC目的:为了耦合度降低
(3)做入门案例就是IOC

2、IOC底层原理

xml解析、工厂模式、反射
首先来看看原始开发存在的弊端
在这里插入图片描述
可以采用工厂模式来解决UserService与UserDao之间的耦合
在这里插入图片描述
但这样依然存在弊端,我们的目的是为了将耦合度降低到最低限度,IOC思想就可以通过xml解析、工厂模式、反射实现这一目的。
在这里插入图片描述

2、IOC接口

1、BeanFactory

IOC容器基本实现,是spring内部使用的接口,不提供开发人员进行使用。
加载配置文件时不会创建对象,在获取对象(使用)时,才会创建对象

2、ApplicationContext

BeanFactory的子接口,提供更多更强大的功能,一般由开发人员进行使用。
加载配置文件时就会把配置文件中的对象进行创建
二者关系如下:
查看BeanFactory的子类结构图
在这里插入图片描述

查看ApplicationContext的父类结构图
在这里插入图片描述
(1)ApplicationContext的实现类
常用的有两个①FileSystemXmlApplicationContext 在获取配置文件的名字时使用系统的全路径②ClassPathXmlApplicationContext加载配置文件时从src算起
在这里插入图片描述
经验之谈
从节约资源的角度来说,BeanFacoty接口是在使用的时候才会创建对象,可以为我们节约内存,但是Spring一般来说要结合web工程使用,我们会考虑将对象的创建放在服务器启动时,而不是在使用的时候才去创建,所以通常会采用ApplicationContext接口。

3、IOC操作bean管理

所谓的bean管理,一是指spring创建对象,二是指spring给创建的对象注入属性

1、bean管理操作的两种方式
1、基于xml配置文件实现bean管理
1、创建对象

在这里插入图片描述

bean标签有很多属性,其中,id属性表示这个对象的唯一标识,class属性的值是该对象所对应的类的全路径,name属性的作用与id作用类似,区别在于name的值可以包含特殊字符。通过这种方式创建对象,默认是使用类的无参构造来创建。

2、注入属性

(1)使用set方法注入属性
在这里插入图片描述
注意,使用set方法注入属性,必须要提供无参构造器
(2)使用构造器注入属性
在这里插入图片描述
构造器方法还支持index属性来指定参数的索引,但一般不建议这样做。

package com.francis.spring5.pojo;

/**
 * @author francis
 * @create 2021-06-06-19:36
 */
public class Book {
    private String name;
    private String author;

    public void setAuthor(String author) {
        this.author = author;
    }

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

    public Book() {
    }

    public Book(String name, String author) {
        this.name = name;
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}
package com.francis.spring5.test;

import com.francis.spring5.pojo.Book;
import com.francis.spring5.pojo.User;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * @author ZQ
 * @create 2021-05-30-22:58
 */
public class UserTest {
  @Test
  public void add(){
    //1、加载spring配置文件
    BeanFactory beanFactory;
    FileSystemXmlApplicationContext fileSystemXmlApplicationContext;
    ClassPathXmlApplicationContext classPathXmlApplicationContext;
    ApplicationContext context=
    new ClassPathXmlApplicationContext("bean1.xml");
    //2、获取容器中的对象
    User user = context.getBean("user", User.class);
    //3、调用user的add方法
    user.add();
  }
  @Test
  public void bookTest(){
    //1、加载spring配置文件
    BeanFactory beanFactory;
    FileSystemXmlApplicationContext fileSystemXmlApplicationContext;
    ClassPathXmlApplicationContext classPathXmlApplicationContext;
    ApplicationContext context=
    new ClassPathXmlApplicationContext("bean1.xml");
    //2、获取容器中的对象
    Book book = context.getBean("book", Book.class);
    System.out.println(book);
  }
  @Test
  public void bookTest1(){
    //1、加载spring配置文件
    BeanFactory beanFactory;
    FileSystemXmlApplicationContext fileSystemXmlApplicationContext;
    ClassPathXmlApplicationContext classPathXmlApplicationContext;
    ApplicationContext context=
    new ClassPathXmlApplicationContext("bean1.xml");
    //2、获取容器中的对象
    Book book = context.getBean("book1", Book.class);
    System.out.println(book);
  }
<?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">
  <!--将对象注入到spring容器-->
<!--    <bean id="user" class="com.francis.spring5.pojo.User"></bean>-->

  <bean id="book" class="com.francis.spring5.pojo.Book">
      <!--使用property标签,用set方法注入属性
      name:类里面属性的名称
      value:给这个属性赋的值-->
      <property name="name" value="天龙八部"/>
      <property name="author" value="金庸"/>
  </bean>
<bean id="book2" class="com.francis.spring5.pojo.Book" p:name="神雕侠侣" p:author="金庸">
  </bean>
  <bean id="book1" class="com.francis.spring5.pojo.Book">
      <constructor-arg name="name" value="红楼梦"/>
      <constructor-arg name="author" value="曹雪芹"/>
<!--        <constructor-arg index="0" value="红楼梦"/>-->
<!--        <constructor-arg index="1"  value="曹雪芹"/>-->
  </bean>
</beans>

上面这两种方式注入属性直观,但是配置比较繁琐,我们可以引入p命名空间来简化配置。
在这里插入图片描述
(3)给属性注入特殊值
①空值null
在这里插入图片描述
②属性值包含特殊符号
在这里插入图片描述
(4)注入外部bean
在这里插入图片描述
(5)级联赋值
在这里插入图片描述

package com.francis.spring5.pojo;

/**
 * @author francis
 * @create 2021-06-06-19:36
 */
public class Book {
    private String name;
   private Person author;

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

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author=" + author +
                '}';
    }

    public void setAuthor(Person author) {
        this.author = author;
    }
}
package com.francis.spring5.pojo;

/**
 * @author Francis
 * @create 2021-06-06-21:19
 */
public class Person {
    private String name;
    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
```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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="book" class="com.francis.spring5.pojo.Book">
        <property name="name" value="西游记"/>
        <!--        <property name="author" ref="person"/>-->
        <property name="author" >
            <bean class="com.francis.spring5.pojo.Person">
                <constructor-arg name="name" value="吴承恩"/>
                <constructor-arg name="age" >
                    <value>100</value>
                </constructor-arg>
            </bean>
        </property>
    </bean>

    <bean id="person" class="com.francis.spring5.pojo.Person">
        <constructor-arg name="name" value="吴承恩"/>
        <constructor-arg name="age" >
            <value>100</value>
        </constructor-arg>
    </bean>
</beans>

在这里插入图片描述

(6)级联注入一对多的属性
在这里插入图片描述

package com.francis.spring5.collection;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Francis
 * @create 2021-06-06-23:54
 */
public class Student {
        private String[] courses;
        private List<String> list;
        private Map<String,String> maps;
        private Set<String> sets;

    public void setCourses(String[] courses) {
        this.courses = courses;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setMpas(Map<String, String> mpas) {
        this.maps = mpas;
    }

    public void setSets(Set<String> sets) {
        this.sets = sets;
    }

    @Override
    public String toString() {
        return "Student{" +
                "courses=" + Arrays.toString(courses) +
                ", list=" + list +
                ", maps=" + maps +
                ", sets=" + sets +
                '}';
    }
}

package com.francis.spring5.test;

import com.francis.spring5.collection.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Francis
 * @create 2021-06-07-0:06
 */
public class CollectionTest {
    @Test
    public void collectionTest(){
        ApplicationContext context=
                new ClassPathXmlApplicationContext("bean1.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student);
    }
}
<?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="student" class="com.francis.spring5.collection.Student">
        <!--数组类型属性注入-->
        <property name="courses">
            <array>
                <value>Java</value>
                <value>mysql</value>
                <value>linux</value>
            </array>

        </property>
        <!--list类型属性注入-->
        <property name="list">
            <list>
                <value>张三</value>
                <value>三儿</value>
            </list>

        </property>
        <!--map类型属性注入-->
        <property name="mpas">
            <map>
                <entry key="JAVA" value="java"/>
                <entry key="MYSQL" value="mysql"/>
            </map>
        </property>
        <!--set类型属性注入-->
        <property name="sets">
            <set>
                <value>Docker</value>
                <value>php</value>
            </set>
        </property>
    </bean>
</beans>

在这里插入图片描述
(7)5公共部分的抽取
在这里插入图片描述
(8)FactoryBean管理
spring中有两种类型的bean:
①返回值类型即是在spring的配置文件中bean标签里class属性的类型,叫做普通bean。例如:book对象的类型就是Book.在这里插入图片描述

②返回值类型不一定是spring配置文件中bean标签里class属性的类型,叫做工厂bean。工厂bean的创建如下:
第一步,创建一个实现接口FactoryBean的类;
第二步,实现接口中的方法,在实现的方法中定义返回的bean类型。
在这里插入图片描述
(9)bean的作用域(scope)
在spring中,默认情况下的bean是单例的。在加载配置文件时进行对象的创建,如果指定为多实例的,则在加载配置文件时不会创建对象,而是在调用getBean方法时才会创建对象。其作用于可以在创建对象时进行指定。
单实例:只创建一个对象,在加载配置文件时就创建对象
在这里插入图片描述
多实例:获取一次创建一个新的对象,在调用getBean方法的时候才创建对象,二者的区别类似ApplicationContext与BeanFactory
在这里插入图片描述
scope还有另外两个值,request表示创建对象放到请求域中,session表示将对象放到会话域中,但一般不会使用。
(10)bean的生命周期
生命周期即是对象从创建到销毁的过程。
①通过构造器创建bean实例(无参构造)
②为bean的属性设置值或对其他bean引用(调用set方法)
③调用bean的初始化方法(需要进行配置)
④bean可以使用了(获取到对象)
⑤当容器关闭的时候,调用bean的销毁方法(需要进行配置)
在这里插入图片描述
除了这五步之外,在第三步之前还会把bean实例传递给bean后置处理器的方法(postProcessBeforeInitialization),在第三步之后也会把bean实例传递给bean后置处理器的方法(postProcessAfterInitialization)。要达到这个效果,需要实现BeanPostProcessor接口。
在这里插入图片描述

package com.francis.spring5.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @author Francis
 * @create 2021-06-07-15:31
 */
public class Orders {
    private String name;
    public Orders() {
        System.out.println("第一步,执行无参构造,order创建了"); }
    public void setName(String name) {
        System.out.println("第二步,调用set方法,给name属性赋值");
        this.name = name; }
    //声明一个方法,在配置文件中配置为初始化函数(方法名可以任意)
    public void initMethod(){
        System.out.println("第三步,执行初始化方法 "); }
    //定义销毁方法
    public void destoryMethod(){
        System.out.println("第五步,执行销毁方法");
    }
}
 @Test
    public void BeanTest(){
        ApplicationContext context=
                new ClassPathXmlApplicationContext("bean4.xml");
        Orders order = context.getBean("order", Orders.class);
        System.out.println("第四步,获取到的bean实例:"+order);
        //关闭容器时会调用order的销毁方法
       ( (ClassPathXmlApplicationContext)context).close();
    }
<?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="order" class="com.francis.spring5.bean.Orders"
          init-method="initMethod" destroy-method="destoryMethod">
        <property name="name"   value="英雄联盟手办"></property>
    </bean>
    <bean id="myPostProcessor" class="com.francis.spring5.bean.MyPostProcessor"></bean>
</beans>
package com.francis.spring5.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @author Francis
 * @create 2021-06-07-16:13
 */
public class MyPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("在初始化之前输出");
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("在初始化之后输出");
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}

(11)自动装配
在这里插入图片描述

<?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="emp" class="com.francis.spring5.autowired.Emp" autowire="byType">
        <property name="name" value="张三"></property>
        <!--传统方式注入-->
<!--        <property name="dept" ref="dept"></property>-->
    </bean>
    <bean id="dept" class="com.francis.spring5.autowired.Dept">
        <property name="name" value="财务部"></property>
    </bean>
</beans>

byName要求bean标签中id的值要与引用类的属性名称相同。byType要求容器中只能存在一个相同类型的bean。
(12)外部属性文件
在这里插入图片描述

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/book
username=root
password=root
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driverClass}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
    </bean>
</beans>
2、基于注解方式实现bean管理

由于篇幅过长,请看下篇。009Spring之基于注解实现bean管理

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值