关于Spring 中的 IoC

本文深入探讨了Spring框架作为Java领域第一框架的地位及其发展历程,详细解释了Spring框架的两大核心组件Ioc控制反转和Aop面向切面编程的概念,以及如何在实际开发中应用Spring Ioc进行对象管理和依赖注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 Spring

Java领域的第一框架,是行业标准和规范。Java初期使用EJB开发,但是这种方式非常繁重,不灵活,不便于维护和升级。经过20年的发展,Spring框架已经从最初取代EJB构建企业级开发的方式,发展成一套自有的生态体系。

Spring框架是指Spring Framework,Spring Framework是整套Spring全家桶所以产品的基石,所有组件产品都是基于Spring Framework,IoC容器。

Spring 有两大核心组件:

  1. Ioc 控制反转
  2. Aop 面向切面编程
1.1 SSH框架

structs2(实现MVC,客户端和服务器的交互)、Spring(构建项目)、Hibernate(持久层)
在这里插入图片描述

1.2 SSM框架

Spring MVC、Spring、Mybatis

在这里插入图片描述

2 Spring Ioc

控制反转(Inversion of Control),反转的是Java程序中对象的创建方式,传统开发是开发者需要的时候new一个对象出来,而控制反转不需要开发者手动new,而是交给IoC容器来创建,开发者可直接使用
在这里插入图片描述
1、传统开发方式

public class test01 {
    public static void main(String[] args) {
        //传统方式创建对象
        Card  card = new Card();
        card.setCNO("sx010");
        card.setNAME("宝乐");
        card.setCLASS("c03");
        System.out.println(card);

2、IoC

    <bean id="card1" class="com.eleven.entity.Card">
        <property name="CNO" value="sx011"></property>
        <property name="NAME" value="历飞雨"></property>
        <property name="CLASS" value="c03"></property>
    </bean>
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Card card1 = (Card) applicationContext.getBean("card1");
        System.out.println(card1);

2.1 IoC使用流程

  1. 将程序需要的对象在spring.xml配置文件中配置(两种配置方式)

无参构造( property ):通过setter方法完成赋值,实体类必须有无参构造)

 <bean id="card1" class="com.eleven.entity.Card">
        <property name="CNO" value="sx011"></property>
        <property name="NAME" value="历飞雨"></property>
        <property name="CLASS" value="c03"></property>
    </bean>

有参构造( constructor-arg ):实体类必须有有参构造

    <bean id="card1" class="com.eleven.entity.Card">
        <constructor-arg name="CLASS" value="C05"></constructor-arg>
        <constructor-arg name="CNO" value="sx004"></constructor-arg>
        <constructor-arg name="NAME" value="大宝乐"></constructor-arg>
    </bean>
  1. 读取 spring.xml 文件 生成 ApplicationContext对象(IoC容器
 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");

3.从ApplicationContext对象中获取需要的bean(两种获取方式)

  • 通过id获取 (spring.xml中配置的id必须唯一)
Card card1 = (Card) applicationContext.getBean("card");
  • 通过Class类型获取(必须保证配置文件中只有一个该类的bean)
Card card1 = (Card) applicationContext.getBean("Card.class");

2.1 IoC的DI

bean之间的依赖注入叫做DI,IoC包含DI,IoC可以自动完成依赖注入,把一个bean注入到另一个bean中

1.传统开发方式

    public static void main(String[] args) {

        Card card = new Card();
        card.setNAME("王宝乐");
        card.setCNO("sx001");
        card.setCLASS("c04");

        Student student = new Student();
        student.setId(1);
        student.setName("土匪");
        
        //传统依赖注入 DI
        card.setStudent(student);

2.ioc方式 :使用 ref 完成依赖注入

  <bean id="card" class="com.eleven.entity.Card">
        <constructor-arg name="CLASS" value="C05"></constructor-arg>
        <constructor-arg name="CNO" value="sx004"></constructor-arg>
        <constructor-arg name="NAME" value="大宝乐"></constructor-arg>
        <constructor-arg name="student" ref="stu1"></constructor-arg>
    </bean>

    <bean id="stu1" class="com.eleven.entity.Student">
        <property name="id" value="1"></property>
        <property name="name" value="吴亦凡"></property>
    </bean>

3.多bean依赖注入 使用 list 完成依赖注入

 <bean id="stu1" class="com.eleven.entity.Student">
        <property name="id" value="1"></property>
        <property name="name" value="吴亦凡"></property>
    </bean>
    <bean id="stu2" class="com.eleven.entity.Student">
        <property name="id" value="2"></property>
        <property name="name" value="鹿晗"></property>
    </bean>
    <bean id="stu3" class="com.eleven.entity.Student">
        <property name="id" value="3"></property>
        <property name="name" value="张艺兴"></property>
    </bean>
    
   <bean id="cla" class="com.eleven.entity.Class">
        <property name="id" value="1"></property>
        <property name="name" value="一班"></property>
        <property name="students">
            <list>
                <ref bean="stu1"></ref>
                <ref bean="stu2"></ref>
                <ref bean="stu3"></ref>
            </list>
        </property>
    </bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值