Spring5中IOC创建对象的方式(有参与无参)与时机(附三类无参创建代码供参考)

Spring5中IOC创建对象的方式(有参与无参)附三类无参创建代码供参考

1. IOC容器

IOC是Spring框架的核心内容,Spring容器使用多种方式完美的实现了IOC,可以使用XML配置,也可以使用注解,新版本的Spring也可以零配置实现IOC。

Spring容器在初始化时先读取配置文件,根据配置文件或元数据创建与组织对象存入容器中,程序使用时再从IOC容器中取出需要的对象。

2. IOC容器创建对象过程

(1) 先通过createBeanFactory创建出一个bean工厂(利用顶级接口BeanFactory下的DefaultListableBeanFactory接口)。
(2) 循环创建对象,由于容器中bean默认都是单例,则优先通过getBean,doGetBean从容器中查找。
(3) 如果找不到则通过createBean,doCreateBean方法,以反射形式创建对象。一般情况下使用的是无参构造方法(getDeclaredConstructor,newInscance)。
(4) 进行对象的属性填充(populateBean)。
(5) 进行其它初始化操作(initializingBean)。

3. 无参构造

pojo下的User实体类创建

image-20240616101946723

package com.lanyy.pojo;

public class User {
    private String name;
    // 无参构造
//    public User(){
//        System.out.println("User的无参构造");
//    }

    // 有参构造
    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void show() {
        System.out.println("name:" + name);
    }
}

beans.xml配置文件参考

Spring的配置文件beans.xml中有一个属性lazy-init=“default/true/false”
(1)如果lazy-init为"default/false"在启动spring容器时创建对象
(2)如果lazy-init为"true",在context.getBean时才要创建对象

image-20240616101507905

<?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="userDaoImpl" class="com.lanyy.dao.UserDaoImpl"/>
    <bean id="mysqlImpl" class="com.lanyy.dao.UserDaoMysqlImpl"/>
    <bean id="oracleImpl" class="com.lanyy.dao.UserDaoOracleImpl"/>

    <!--
      beans.xml 本质是一个Spring容器
      ref : 引用Spring容器中创建好的对象
      value : 具体的值,基本数据类型!
    -->

    <bean id="userServiceImpl" class="com.lanyy.service.UserServiceImpl">
            <property name="userDao" ref="userDaoImpl"/>
    </bean>
        <!-- pojo实体类配置引入 -->
    <!--  无参构造  -->
    <bean id="user" class="com.lanyy.pojo.User">
        <property name="name" value="lanyy"/>
    </bean>
</beans>

4. 有参构造

<?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="userDaoImpl" class="com.lanyy.dao.UserDaoImpl"/>
    <bean id="mysqlImpl" class="com.lanyy.dao.UserDaoMysqlImpl"/>
    <bean id="oracleImpl" class="com.lanyy.dao.UserDaoOracleImpl"/>

    <!--
      beans.xml 本质是一个Spring容器
      ref : 引用Spring容器中创建好的对象
      value : 具体的值,基本数据类型!
    -->

    <bean id="userServiceImpl" class="com.lanyy.service.UserServiceImpl">
            <property name="userDao" ref="userDaoImpl"/>
    </bean>

<!--  IOC有参构造创建对象的三种方式  -->

    <!--  有参构造  -->
    <!--  第一种:下标赋值  -->
<!--    <bean id="user" class="com.lanyy.pojo.User">-->
<!--        <constructor-arg index="0" value="懒羊羊滚雪球"/>-->
<!--    </bean>-->

    <!--  第二种:参数类型匹配  单参数String可用 多参不可用 -->
<!--    <bean id="user" class="com.lanyy.pojo.User">-->
<!--        <constructor-arg type="java.lang.String" value="懒羊羊滚雪球"/>-->
<!--    </bean>-->
<!--  第三种:参数名匹配(直接通过参数名来设置)  -->
    <bean id="user" class="com.lanyy.pojo.User">
        <constructor-arg name="name" value="懒羊羊滚雪球"/>
    </bean>
</beans>

5. Test类编写

import com.lanyy.dao.UserDaoHiveImpl;
import com.lanyy.dao.UserDaoMysqlImpl;
import com.lanyy.pojo.User;
import com.lanyy.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
//        // 用户实际调用的是业务层,dao层不需要接触
//        UserServiceImpl userService = new UserServiceImpl();
//        // 用户通过业务层去调用dao层
        userService.setUserDao(new UserDaoMysqlImpl());
//        userService.setUserDao(new UserDaoHiveImpl());
//        userService.getUser();

        // 容器练习
//        // 获取ApplicationContext对象,拿到配置文件(Spring容器)
//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//        // 容器在手 天下我有 需要什么 get什么   name 名字 用户可以自定义,此处userServiceImpl
//        UserServiceImpl userServiceImpl = context.getBean("userServiceImpl", UserServiceImpl.class);
//        userServiceImpl.getUser();

        // pojo实体类练习
        // 走无参构造方法
//        User user = new User();
// 有参通用写法:由Object强转成我需要的对象User        
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        // 由Object强转成我需要的对象User
        User user = context.getBean("user", User.class);
        user.show();
    }
}

总结:在配置文件加载时,容器中管理的对象就已经被初始化

了解更多知识请戳下:

@Author:懒羊羊

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-17 02:10:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-17 02:10:06       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-17 02:10:06       87 阅读
  4. Python语言-面向对象

    2024-06-17 02:10:06       96 阅读

热门阅读

  1. 2024.06.03 校招 实习 内推 面经

    2024-06-17 02:10:06       34 阅读
  2. linux信息查询

    2024-06-17 02:10:06       33 阅读
  3. 从ibd文件恢复MySQL数据

    2024-06-17 02:10:06       31 阅读
  4. Linux基础指令(二)(文件、权限等)

    2024-06-17 02:10:06       30 阅读
  5. C++中的责任链模式

    2024-06-17 02:10:06       23 阅读
  6. C# 下载文件2

    2024-06-17 02:10:06       39 阅读
  7. React 使用 Zustand 详细教程

    2024-06-17 02:10:06       31 阅读
  8. 速盾:cdn影响seo吗?

    2024-06-17 02:10:06       25 阅读
  9. 后端防接口被刷

    2024-06-17 02:10:06       34 阅读
  10. 敏捷=996/007?现实是……

    2024-06-17 02:10:06       27 阅读
  11. Python3 笔记:字符串的 encode() 和 bytes.decode()

    2024-06-17 02:10:06       31 阅读
  12. 等保测评的概念和流程

    2024-06-17 02:10:06       31 阅读