Spring IoC笔记(二)

Spring笔记



前言

Spring学习笔记


一、Spring IoC自动装载 autowire

自动装载是Spring提供的一种更加简便的方式来完成DI,不需要手动配置property。IoC容器会自动选择bean完成注入。

自动转装载有两种方式:

  • byName,通过属性名完成自动装载;
  • byType,通过属性对应的数据类型完成自动装载。

byName方式(是在bean中寻找bean id名与被注入的实体类的属性名一样的bean)

  1. 创建Person和Car实体类
@Data
@AllArgsConstructor
public class Car {
    private Integer num;
    private String brand;
}
@Data
public class Person {
    private Integer id;
    private String name;
    private Car car;
}
  1. 在spring.xml中配置Car和Person对应的bean,并且通过自动装载完成依赖注入。
    <bean id="person" class="com.entity.Person" autowire="byName">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
    </bean>

    <bean id="car" class="com.entity.Car">
        <constructor-arg name="num" value="1"></constructor-arg>
        <constructor-arg name="brand" value="奥迪"></constructor-arg>
    </bean>

byType方式(是在bean里面寻找被注入相同数据类型的bean)

  1. autowire=“byType”
    <bean id="person" class="com.entity.Person" autowire="byType">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
    </bean>

    <bean id="car1" class="com.entity.Car">
        <constructor-arg name="num" value="1"></constructor-arg>
        <constructor-arg name="brand" value="奥迪"></constructor-arg>
    </bean>
    
    <bean id="car" class="com.entity.Student">
        <property name="id" value="1"></property>
        <property name="name" value="Lq"></property>
        <property name="age" value="21"></property>
    </bean>

使用byType进行自动装载时,必须保证IoC中只有一个符合条件的bean,否则会抛出异常。
在这里插入图片描述

二、Spring IoC 基于注解的开发

Spring IoC 的作用是帮助开发者创建项目中所需要的bean,同时完成bean之间的依赖注入关系,DI。
实现该功能有两种方式:

  • 基于XML配置;
  • 基于注解。

基于注解有两步操作,缺一不可:

  1. 配置自动扫包;
<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置自动扫包 -->
    <context:component-scan base-package="com.entity"></context:component-scan>

</beans>
  1. 添加注解。
@Data
@Component(value = "myCar") // 添加注解,默认为实体类名称首字母小写,即person。
public class Person {
    private Integer id;
    private String name;
    private Car car;
}

 public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-annotation.xml");
        //Person person=(Person) applicationContext.getBean("person");//默认为实体类名称首字母小写
        Person person=(Person) applicationContext.getBean("myCar");
        System.out.println(person);
  
    }

DI

@Data
@Component(value = "ds")
public class Car {
    @Value("123")
    private Integer num;
    @Value("奥迪")
    private String brand;
}
@Data
@Component
public class Person {
    private Integer id;
    private String name;
    @Autowired
    private Car car;
}

@Autowired默认是通过byType进行注入的,如果要改为byName,需要配置@Qualifier注解来完成

@Data
@Component
public class Person {
    private Integer id;
    private String name;
    @Autowired
    @Qualifier(value = "ds")
    private Car car;
}

表示将IoC中id为ds的bean注入到person 中,实体类中普通的成员变量(String、包装类等)可以通过@Value注解进行赋值。

三、Spring IoC 底层实现

核心技术点:XML解析+反射
具体的思路:

  1. 根据需求编写XML文件,配置所需创建的bean;
  2. 编写程序读取XML文件,获取bean相关信息,类、属性、id;
  3. 根据第二步获取的信息,结合反射机制动态创建对象,同时完成属性的赋值;
  4. 将创建好的bean存入Map集合,设置key - value映射,key就是bean中id值,value就是bean对象;
  5. 提供方法从Map中通过id获取到对应的value。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梳碧湖的砍柴人.exe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值