spring框架之IoC学习与梳理(1)

发布于:2025-02-10 ⋅ 阅读:(35) ⋅ 点赞:(0)

目录

一、spring-IoC的基本解释。

二、spring-IoC的简单demo(案例)。

(1)maven-repository官网中找依赖坐标。

(2).pom文件中通过标签引入。

(3)使用lombok帮助快速开发。

(4)通常情况。手动new对象,setter方法完成对象初始化。

(5)基于XML配置使用IoC。(本demo使用IoC容器创建bean对象)

<1>新建spring的主配置文件。

<2>XML中配置DataConfig类对象。

<3>测试类中使用ApplicationContext容器获取bean对象。


一、spring-IoC的基本解释。

  • IoC是Inversion of Control的缩写,意为“控制反转”。

  • 在通常,类的对象都由开发者手动创建。当企业级开发量很大时,需要频繁的创建对象,这很浪费时间。将更多的精力放到业务代码上的编写是很重要的!
  • 当使用IoC将对象的创建操作进行反转,让spring提供的IoC容器根据需要自动的创建对象。(如使用.xml配置文件管理bean对象)

二、spring-IoC的简单demo(案例)。

(1)maven-repository官网中找依赖坐标。

https://mvnrepository.com/


  • 添加spring-context(上下文)依赖。这样可以通过该依赖传递导入其他所需的坐标。


(2).pom文件中通过标签<dependence>引入。
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.18</version>
</dependency>


(3)使用lombok帮助快速开发。
  • 无需手动提供构造器、getter、setter、toString()等等方法。
  • 记得安装这个插件(高版本idea自动绑定安装)。


  • lombok依赖的坐标。
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.30</version>
    <scope>provided</scope>
</dependency>

  • 只需在需要提供这些基础方法的类上添加注解@Data即可。


  • 使用注解@Data。


(4)通常情况。手动new对象,setter方法完成对象初始化。
package com.fs.test;

import com.fs.demo.DataConfig;

public class Main {
    public static void main(String[] args) {
        //不要IoC——所有对象自己创建
        DataConfig dataConfig = new DataConfig();
        dataConfig.setUrl("jdbc:mysql://localhost:3306/test");
        dataConfig.setDriverName("com.mysql.jdbc.Driver");
        dataConfig.setUserName("root");
        dataConfig.setPassword("root123");
        System.out.println(dataConfig);
    }
}


(5)基于XML配置使用IoC。(本demo使用IoC容器创建bean对象)
  • 以后学习基于注解使用IoC更多。(springboot框架)
  • 除了使用ApplicationContext容器(简称"IoC"容器)外。还可以使用BeanFactory创建bean对象。(IoC思想)
  • 简单说:开发者把需要交给IoC容器管理的对象在XML文件中进行配置。spring框架读取指定配置文件,根据配置文件中的配置信息来自动帮开发者创建对象。

<1>新建spring的主配置文件。
  • resources目录下新建即可。
  • 注意:需要提前导入spring-context的依赖坐标。


<2>XML中配置DataConfig类对象。
  • 使用<bean>标签配置需要指定生产的"DataConfig"类的bean对象。
  • 使用<property>标签配置"DataConfig"类的所有成员属性初始值。
<?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 class="com.fs.demo.DataConfig" id="config">
        <property name="driverName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="userName" value="root"></property>
        <property name="password" value="root123"></property>
    </bean>
</beans>

<3>测试类中使用ApplicationContext容器获取bean对象。
  • 注意getBean()方法中的参数需要与XML配置文件中配置的bean对象的id保持一致!
package com.fs.test;

import com.fs.demo.DataConfig;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        //从类路径下加载bean配置文件,得到ApplicationContext容器对象
        ClassPathXmlApplicationContext ApplicationContext = new ClassPathXmlApplicationContext("springConfig.xml");
        //使用容器对象的getBean方法,通过配置文件里面的唯一标识id得到该bean对象
        Object dataConfigObj = ApplicationContext.getBean("dataConfig");
        //强制类型转换
        DataConfig dataConfig = (DataConfig) dataConfigObj;
        System.out.println(dataConfig);

    }
}
  • 测试类MainApp运行结果。


网站公告

今日签到

点亮在社区的每一天
去签到