Spring笔记
前言
Spring学习笔记
一、Spring IoC自动装载 autowire
自动装载是Spring提供的一种更加简便的方式来完成DI,不需要手动配置property。IoC容器会自动选择bean完成注入。
自动转装载有两种方式:
- byName,通过属性名完成自动装载;
- byType,通过属性对应的数据类型完成自动装载。
byName方式(是在bean中寻找bean id名与被注入的实体类的属性名一样的bean)
- 创建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;
}
- 在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)
- 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配置;
- 基于注解。
基于注解有两步操作,缺一不可:
- 配置自动扫包;
<?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>
- 添加注解。
@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解析+反射
具体的思路:
- 根据需求编写XML文件,配置所需创建的bean;
- 编写程序读取XML文件,获取bean相关信息,类、属性、id;
- 根据第二步获取的信息,结合反射机制动态创建对象,同时完成属性的赋值;
- 将创建好的bean存入Map集合,设置key - value映射,key就是bean中id值,value就是bean对象;
- 提供方法从Map中通过id获取到对应的value。