Spring IoC容器与Bean管理(三)

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: Spring IoC容器与Bean管理

6.对象依赖注入


本节,我们来学习如何在spring IoC容器中设置对象的依赖关系。这个过程我们称为依赖注入。依赖注入说白了就是将两个对象关联起来。


依赖注入是指运行时将容器内对象利用反射赋值给其他对象的操作。


利用setter实现静态数值注入


585d1dcf172e4a92a95202475e037ef2.png


这个bean,里面包含的属性一律采用property这个标签来指代。property标签最基础的有2个属性,第一个是name,指属性名,第二个是value,指属性值。在创建好这个bean以后,便会通过反射机制调用各属性的setter方法来动态设置数值。


13d9ef9d66584e329b8f6e8d95815f98.png


基于构造方法注入对象


d0a8357473584bab9cb6a263ecc3bded.png


注入集合对象


前面写的注入对象都是单个对象,这一节我要讲解List或Set等这样的集合对象注入。注意各标签的大小写。


6434c92b8d344f2ea676d1fff974176d.png


可以用value标签来设置具体的数值,或者ref引入具体的bean。


0f7148bc00474ea59a3e7eb07b7541d4.png


list和set的区别是list里面的元素不允许出现重复,而set里面的元素可以出现重复。


046b750fcef84243990dc1ce9c6608e6.png

e0c083b6536a40ae84cf60ce14424d8b.png


properties只允许key和value是字符串类型的。


下面是一个样例:


   <bean id="company" class="com.haiexijun.ioc.entity.Company">
        <property name="rooms">
            <list>
                <value>201-总裁办</value>
                <value>202-总经理办公室</value>
                <value>203-研发部会议室</value>
                <value>203-研发部会议室</value>
            </list>
        </property>
        <property name="computers">
            <map>
                <entry key="dev-112" value-ref="computer1"></entry>
                <entry key="dev-113">
                    <bean class="com.haiexijun.ioc.entity.Computer">
                            <constructor-arg name="brand" value="联想"/>
                            <constructor-arg name="price" value="2222"/>
                            <constructor-arg name="sn" value="12312412"/>
                            <constructor-arg name="type" value="台式机"/>
                    </bean>
                </entry>
            </map>
        </property>
        <property name="info">
            <props>
                <prop key="phone">13214124</prop>
                <prop key="address">zjian</prop>
            </props>
        </property>
    </bean>
    <bean id="computer1" class="com.haiexijun.ioc.entity.Computer">
        <constructor-arg name="brand" value="联想"/>
        <constructor-arg name="price" value="2222"/>
        <constructor-arg name="sn" value="12312412"/>
        <constructor-arg name="type" value="台式机"/>
    </bean>

这里就不作过多的解释了。


查看容器内对象


前面学习了如何在容器内创建对象,以及设计对象的关系。但是所有这些信息都是通过脑补来完成的。如果一个工程越来越大,

对象越来越多,那我们如何知道当前容器中到底有多少对象,这些对象又是什么类型呢?

可以通过ApplicationContext对象的getBeanDefinitionNames()方法获取到bean的名字的String数组。通过类对象.getClass().getName()来获取全类名,如context.getBean(beanName).getClass().getName()。如果要获取实体类具体的内容,用实体类对象.toString() 方法。


7.Bean对象的作用域及生命周期


bean scope属性详解


scope的原意是范围的意思。


bean scope属性用于决定对象何时被创建与作用的范围。通过设置 scope属性会影响到容器内对象的数量。默认情况下bean会在IoC容器创建后自动实例化,全局唯一。


a7b1923bda0b4fd496157b947a77d37a.png

24331c78477c4eeb84cfadee41d73648.png

下面是singleton单例示意图

单例的执行效率高,不用多次创建同一个对象,但也会引发很多线程安全问题。


c97e0ad1285d425b99916e9c41de457b.png


下面是prototype多例示意图:


f97bccf973bf439297594279278e8139.png


singleton 与 prototype对比:


16e4cb024eec4424929fb0ad58ab8f90.png


IoC的生命周期


987dc55002b8449fa798a6ea41f2b733.png


下面写一个案例来演示IoC的生命周期:


配置好了项目后,创建一个entity实体类,名为Order:

package com.haiexijun.ioc.entity;
public class Order {
    private Float price;
    private Integer quantity;
    private Float total;
    public Order(){
        System.out.println("创建Order对象"+this);
    }
    public void init(){
        System.out.println("执行Init方法");
        total=price*quantity;
    }
    public void destroy(){
        System.out.println("销毁容器,释放Order对象相关的资源");
    }
    public void pay(){
        System.out.println("订单金额为:"+total);
    }
    public Float getPrice() {
        return price;
    }
    public void setPrice(Float price) {
        System.out.println("设置price:"+price);
        this.price = price;
    }
    public Integer getQuantity() {
        return quantity;
    }
    public void setQuantity(Integer quantity) {
        System.out.println("设置quantity:"+quantity);
        this.quantity = quantity;
    }
    public Float getTotal() {
        return total;
    }
    public void setTotal(Float total) {
        this.total = total;
    }
}


然后在applicationContext.xml文件中编写bean


<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="order1" class="com.haiexijun.ioc.entity.Order" init-method="init" destroy-method="destroy">
        <property name="price" value="19.8"/>
        <property name="quantity" value="1000"/>
    </bean>
</beans>


会发现,我并没有在bean中为total属性设置属性和属性值,而是通过init-method属性指定order类里面的init方法来计算total来设置total属性的。我们还定义了一个destroy-method的属性,用于指定IoC容器被销毁后执行的方法,一般用于释放与该对象关联的资源。


最后编写代码运行:


package com.haiexijun.ioc;
import com.haiexijun.ioc.entity.Order;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplication {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        System.out.println("====IoC容器以初始化完成====");
        Order order= context.getBean("order1", Order.class);
        order.pay();
        //销毁容器,会自动调用xml中设置的destroy-method方法
        ((ClassPathXmlApplicationContext)context).registerShutdownHook();
    }
}

运行结果如下:


ac1b7ef80b1e4033ae65169089355ad0.png

相关文章
|
4天前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
65 2
|
8月前
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
538 26
|
XML Java 数据格式
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
|
5月前
|
XML Java 数据格式
Spring IoC容器的设计与实现
Spring 是一个功能强大且模块化的 Java 开发框架,其核心架构围绕 IoC 容器、AOP、数据访问与集成、Web 层支持等展开。其中,`BeanFactory` 和 `ApplicationContext` 是 Spring 容器的核心组件,分别定位为基础容器和高级容器,前者提供轻量级的 Bean 管理,后者扩展了事件发布、国际化等功能。
|
7月前
|
Java 容器 Spring
什么是Spring IOC 和DI ?
IOC : 控制翻转 , 它把传统上由程序代码直接操控的对象的调用权交给容 器,通过容器来实现对象组件的装配和管理。所谓的“控制反转”概念就是对组件对象控制权的转 移,从程序代码本身转移到了外部容器。 DI : 依赖注入,在我们创建对象的过程中,把对象依赖的属性注入到我们的类中。
|
8月前
|
XML Java 数据格式
Spring容器的本质
本文主要讨论Spring容器最核心的机制,用最少的代码讲清楚Spring容器的本质。
|
10月前
|
Java Spring 容器
【SpringFramework】Spring IoC-基于注解的实现
本文主要记录基于Spring注解实现IoC容器和DI相关知识。
149 21
|
2月前
|
Kubernetes Docker Python
Docker 与 Kubernetes 容器化部署核心技术及企业级应用实践全方案解析
本文详解Docker与Kubernetes容器化技术,涵盖概念原理、环境搭建、镜像构建、应用部署及监控扩展,助你掌握企业级容器化方案,提升应用开发与运维效率。
506 108
|
7天前
|
Prometheus 监控 Cloud Native
如何使用第三方监控系统监控Docker容器性能?
如何使用第三方监控系统监控Docker容器性能?
246 139
|
7天前
|
Prometheus 监控 Cloud Native
如何在Docker容器中监控和管理应用程序的性能?
如何在Docker容器中监控和管理应用程序的性能?
227 123