二、IOC容器3

简介: 二、IOC容器3

5、把集合注入部分提取出来

(1)在spring牌配置文件引入名称空间util

(2)使用util标签完成list集合注入提取

collectiontype/新建Book
package com.atguigu.spring5.collectiontype;
import java.util.List;
public class Book {
    private List<String> list;
    public void setList(List<String> list) {
        this.list = list;
    }
    public void test(){
        System.out.println(list);
    }
}
src下/bean2.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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <!-- 1.提取list集合类型属性注入   -->
    <util:list id="bookList">
        <value>易筋经</value>
        <value>九阴真经</value>
        <value>九阳神功</value>
    </util:list>
    <!-- 2.提取list集合类型属性注入使用   -->
    <bean id="book" class="com.atguigu.spring5.collectiontype.Book">
        <property name="list" ref="bookList"></property>
    </bean>
</beans>
TestSpring5Demo1
package com.atguigu.spring5.testdemo;
import com.atguigu.spring5.collectiontype.Book;
import com.atguigu.spring5.collectiontype.Stu;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring5Demo1 {
    @Test
    public void testCollection1(){
        ApplicationContext context= new ClassPathXmlApplicationContext("bean1.xml");
        Stu stu = context.getBean("stu", Stu.class);
        stu.test();
    }
    @Test
    public void testCollection2(){
        ApplicationContext context= new ClassPathXmlApplicationContext("bean2.xml");
        Book book = context.getBean("book", Book.class);
        book.test();
    }
}

IOC 操作 Bean 管理(FactoryBean)

1、Spring有两种类型的bean,一种普通bean,另外一种工厂bean(FactoryBean)

2、普通bean:在配置文件中定义bean类型就是返回类型

3、工厂bean:在配置文件定义bean类型可以和返回类型不一样

第一步 创建一个类,让这个类作为工厂bean,实现接口FactoryBean

第二步 实现接口里面的方法,在实现的方法中定义返回的bean类型

spring5/新建factorybean/MyBean类
package com.atguigu.spring5.factorybean;
import com.atguigu.spring5.collectiontype.Course;
import org.springframework.beans.factory.FactoryBean;
public class MyBean implements FactoryBean<Course> {
    //定义返回bean
    @Override
    public Course getObject() throws Exception {
        Course course=new Course();
        course.setCname("abc");
        return course;
    }
    @Override
    public Class<?> getObjectType() {
        return null;
    }
    @Override
    public boolean isSingleton() {
        return FactoryBean.super.isSingleton();
    }
}
bean3.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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="myBean" class="com.atguigu.spring5.factorybean.MyBean">
    </bean>
</beans>
TestSpring5Demo1
 @Test
    public void test3(){
        ApplicationContext context= new ClassPathXmlApplicationContext("bean3.xml");
        Course course = context.getBean("myBean", Course.class);
        System.out.println(course);
    }

IOC 操作 Bean 管理(Bean的作用域)

1、在Spring里面,设置创建bean实例是单实例还是多实例

2、在Spring里面,默认情况下,bean是单实例对象

TestSpring5Demo1

 @Test
    public void testCollection2(){
        ApplicationContext context= new ClassPathXmlApplicationContext("bean2.xml");
        Book book1 = context.getBean("book", Book.class);
        Book book2 = context.getBean("book", Book.class);
//        book.test();
        System.out.println(book1);//com.atguigu.spring5.testdemo.TestSpring5Demo1
        System.out.println(book2);//com.atguigu.spring5.testdemo.TestSpring5Demo1
    }

3、如何设置单实例还是多实例

(1)在spring配置文件bean标签里面有属性(scope)用于设置单实例还是多实例

(2)scope属性值

第一个值 默认值,singleton,表示单实例对象

第二个值 prototype,表示多实例对象

bean2.xml
 <!-- 2.提取list集合类型属性注入使用   -->
    <bean id="book" class="com.atguigu.spring5.collectiontype.Book" scope="prototype">
        <property name="list" ref="bookList"></property>
    </bean>
TestSpring5Demo1
 @Test
    public void testCollection2(){
        ApplicationContext context= new ClassPathXmlApplicationContext("bean2.xml");
        Book book1 = context.getBean("book", Book.class);
        Book book2 = context.getBean("book", Book.class);
//        book.test();
        System.out.println(book1);//com.atguigu.spring5.testdemo.TestSpring5Demo1
        System.out.println(book2);//com.atguigu.spring5.testdemo.TestSpring5Demo1
//        com.atguigu.spring5.collectiontype.Book@38afe297
//        com.atguigu.spring5.collectiontype.Book@2df3b89c
    }

(3)singleton和prototype区别

第一 singleton单实例,prototype多实例
第二 设置scope值是singleton时候,加载spring配置文件时候就会创建单实例对象

设置scope值是prototype时候,不是在加载spring配置文件时候就会创建对象,在调用getBean方法时候创建多实例对象

request 了解

session 了解

IOC 操作 Bean 管理(bean生命周期)

1、生命周期

(1)从对象创建到对象销毁的过程

2、bean生命周期

(1)通过构造器创建bean实例(无参数构造)

(2)为bean的属性设置值和对其他bean引用(调用set方法)
(3)调用bean的初始化的方法(需要进行配置)

(4)bean可以使用了(对象获取到了)

(5)当容器关闭的时候,调用bean的销毁的方法(需要进行配置销毁的方法)

3、演示bean生命周期

bean4.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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="orders" class="com.atguigu.spring5.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
        <property name="oname" value="手机"></property>
    </bean>
</beans>

spring5/bean/新建Orders类

package com.atguigu.spring5.bean;
public class Orders {
    private String oname;
    //无参构造
    public Orders() {
        System.out.println("第一步 执行无参构造器创建bean实例");
    }
    public void setOname(String oname) {
        System.out.println("第二步 调用set方法设置属性值");
        this.oname = oname;
    }
    //创建执行的初始化的方法
    public void initMethod(){
        System.out.println("第三步 执行初始化的方法");
    }
    //创建执行的销毁的方法
    public void destroyMethod(){
        System.out.println("第五步 执行销毁的方法");
    }
}

TestSpring5Demo1

 @Test
    public void testBean3(){
//        ApplicationContext context= new ClassPathXmlApplicationContext("bean4.xml");
        ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("bean4.xml");
        Orders orders = context.getBean("orders", Orders.class);
        System.out.println("第四步 获取创建bean实例对象");
        System.out.println(orders);
        //手动让bean实例销毁
        context.close();
    }
执行testBean3
第一步 执行无参构造器创建bean实例
第二步 调用set方法设置属性值
第三步 执行初始化的方法
第四步 获取创建bean实例对象
com.atguigu.spring5.bean.Orders@3754a4bf
第五步 执行销毁的方法
Process finished with exit code 0

4、bean的后置处理器,bean生命周期有七步

(1)通过构造器创建bean实例(无参数构造)

(2)为bean的属性设置值和对其他bean引用(调用set方法)
(3)把bean实例传递bean后置处理器的方法postProcessBeforeInitialization

(4)调用bean的初始化的方法(需要进行配置)

(5)把bean实例传递bean后置处理器的方法postProcessAfterInitialization

(6)bean可以使用了(对象获取到了)

(7)当容器关闭的时候,调用bean的销毁的方法(需要进行配置销毁的方法)

5、演示添加后置处理器效果

(1)创建类,实例接口 BeanPostProcessor,创建后置处理器

bean下/创建MyBeanPost
package com.atguigu.spring5.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;
public class MyBeanPost implements BeanPostProcessor {//F4 或Ctrl+鼠标左键 BeanPostProcessor
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("在初始化之前执行的方法");
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("在初始化之后执行的方法");
        return bean;
    }
}
bean4.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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="orders" class="com.atguigu.spring5.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
        <property name="oname" value="手机"></property>
    </bean>
    <!-- 配置后置处理器   -->
    <bean id="myBeanPost" class="com.atguigu.spring5.bean.MyBeanPost"></bean>
</beans>
执行testBean3
第一步 执行无参构造器创建bean实例
第二步 调用set方法设置属性值
在初始化之前执行的方法
第三步 执行初始化的方法
在初始化之后执行的方法
第四步 获取创建bean实例对象
com.atguigu.spring5.bean.Orders@1d296da
第五步 执行销毁的方法
Process finished with exit code 0

IOC 操作 Bean 管理(xml自动装配)

1、什么是自动装配

(1)根据指定装配规则(属性名称或属性类型),Spring自动将匹配的属性值进行注入

2、演示自动装配过程

spring/5新建autowire/新建Dept
package com.atguigu.spring5.autowire;
public class Dept {
    @Override
    public String toString() {
        return "Dept{}";
    }
}
autowire/新建Emp
package com.atguigu.spring5.autowire;
public class Emp {
    private Dept dept;
    public void setDept(Dept dept) {
        this.dept = dept;
    }
    @Override
    public String toString() {
        return "Emp{" +
                "dept=" + dept +
                '}';
    }
    public void test(){
        System.out.println(dept);
    }
}
src下/新建bean5.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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="emp" class="com.atguigu.spring5.autowire.Emp">
      <property name="dept" ref="dept"></property>
    </bean>
    <bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean>
</beans>
TestSpring5Demo1
@Test
    public void test4(){
        ApplicationContext context= new ClassPathXmlApplicationContext("bean5.xml");
        Emp emp = context.getBean("emp", Emp.class);
        System.out.println(emp);
    }

(1)根据属性名称自动注入

bean5.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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <!-- 实现自动装配
        bean标签属性autowire,配置自动装配
        autowire属性有两个值:
            byName 根据属性名称注入,注入值bean的id值和类属性名称一样
            byType 根据属性类型注入
    -->
    <bean id="emp" class="com.atguigu.spring5.autowire.Emp" autowire="byName">
<!--        <property name="dept" ref="dept"></property>-->
    </bean>
    <bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean>
</beans>

(2)根据属性类型自动注入

bean5.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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    <!-- 实现自动装配
        bean标签属性autowire,配置自动装配
        autowire属性有两个值:
            byName 根据属性名称注入,注入值bean的id值和类属性名称一样
            byType 根据属性类型注入
    -->
    <bean id="emp" class="com.atguigu.spring5.autowire.Emp" autowire="byType">
<!--        <property name="dept" ref="dept"></property>-->
    </bean>
    <bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean>
    <!--  多个bean  byType会报错 ,一般用注解进行自动装配-->
    <!--    <bean id="dept1" class="com.atguigu.spring5.autowire.Dept"></bean>-->
</beans>
相关文章
|
XML Java 测试技术
《深入理解Spring》:IoC容器核心原理与实战
Spring IoC通过控制反转与依赖注入实现对象间的解耦,由容器统一管理Bean的生命周期与依赖关系。支持XML、注解和Java配置三种方式,结合作用域、条件化配置与循环依赖处理等机制,提升应用的可维护性与可测试性,是现代Java开发的核心基石。
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
XML Java 数据格式
Spring IoC容器的设计与实现
Spring 是一个功能强大且模块化的 Java 开发框架,其核心架构围绕 IoC 容器、AOP、数据访问与集成、Web 层支持等展开。其中,`BeanFactory` 和 `ApplicationContext` 是 Spring 容器的核心组件,分别定位为基础容器和高级容器,前者提供轻量级的 Bean 管理,后者扩展了事件发布、国际化等功能。
549 18
|
XML Java 数据格式
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
|
XML Java 数据格式
Spring5入门到实战------5、IOC容器-Bean管理(三)
这篇文章深入探讨了Spring5框架中IOC容器的高级Bean管理,包括FactoryBean的使用、Bean作用域的设置、Bean生命周期的详细解释以及Bean后置处理器的实现和应用。
Spring5入门到实战------5、IOC容器-Bean管理(三)
|
XML Java 数据格式
Spring5入门到实战------6、IOC容器-Bean管理XML方式(自动装配)
这篇文章是Spring5框架的入门教程,详细讲解了IOC容器中Bean的自动装配机制,包括手动装配、`byName`和`byType`两种自动装配方式,并通过XML配置文件和Java代码示例展示了如何在Spring中实现自动装配。
Spring5入门到实战------6、IOC容器-Bean管理XML方式(自动装配)
|
XML Java 数据格式
Spring5入门到实战------2、IOC容器底层原理
这篇文章深入探讨了Spring5框架中的IOC容器,包括IOC的概念、底层原理、以及BeanFactory接口和ApplicationContext接口的介绍。文章通过图解和实例代码,解释了IOC如何通过工厂模式和反射机制实现对象的创建和管理,以及如何降低代码耦合度,提高开发效率。
Spring5入门到实战------2、IOC容器底层原理
|
XML Java 开发者
经典面试---spring IOC容器的核心实现原理
作为一名拥有十年研发经验的工程师,对Spring框架尤其是其IOC(Inversion of Control,控制反转)容器的核心实现原理有着深入的理解。
1172 3
|
XML Java 数据格式
Spring5入门到实战------8、IOC容器-Bean管理注解方式
这篇文章详细介绍了Spring5框架中使用注解进行Bean管理的方法,包括创建Bean的注解、自动装配和属性注入的注解,以及如何用配置类替代XML配置文件实现完全注解开发。
Spring5入门到实战------8、IOC容器-Bean管理注解方式
|
XML Java 数据格式
Spring IOC容器的深度解析及实战应用
【10月更文挑战第14天】在软件工程中,随着系统规模的扩大,对象间的依赖关系变得越来越复杂,这导致了系统的高耦合度,增加了开发和维护的难度。为解决这一问题,Michael Mattson在1996年提出了IOC(Inversion of Control,控制反转)理论,旨在降低对象间的耦合度,提高系统的灵活性和可维护性。Spring框架正是基于这一理论,通过IOC容器实现了对象间的依赖注入和生命周期管理。
445 0

热门文章

最新文章