Spring笔记一
spring官方文档 https://docs.spring.io/spring/docs/5.1.12.RELEASE/spring-framework-reference/
1.简述IOC(Inversion of Control 控制反转)和DI(dependency injection --依赖注入) 的联系和区别
根据Spring官网描述,IOC也称为DI,DI是一个通过构造函数或者其依赖对象工厂方法设置属性定义依赖的过程,容器在创建bean的时候为其注入这些依赖,IOC即是创建这个bean的逆过程。简言之 DI是一个注入依赖的过程,而IOC是创建bean的方式。
2.Spring配置元数据的方式:基于XML的配置(传统方式)、基于注释的配置(Spring 2.5)、基于JAVA的配置(Spring 3.0)
3.SpringDI注入有几种方式? major 2 type
根据Spring官网可知,DI注入主要有两种形式:(基于构造器) Constructor-based dependency injection and (基于Setter方法)Setter-based dependency injection
//基于构造器
public class SimpleMovieLister {
private MovieFinder movieFinder;
public SimpleMovieLister(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
//构造函数参数解析
package x.y;
public class ThingOne {
public ThingOne(ThingTwo thingTwo, ThingThree thingThree) {
}
}
<beans>
<bean id="beanOne" class="x.y.ThingOne">
<constructor-arg ref="beanTwo"/>
<constructor-arg ref="beanThree"/>
</bean>
<bean id="beanTwo" class="x.y.ThingTwo"/>
<bean id="beanThree" class="x.y.ThingThree"/>
</beans>
4 实例化IOC容器和使用
org.springframework.context.ApplicationContext
接口代表Spring IoC容器,并负责实例化,配置和组装Bean
ApplicationContext context = new ClassPathXmlApplicationContext("spring.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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- spring--> <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl"> <property name="accountDao" ref="accountDao"/> <property name="itemDao" ref="itemDao"/> <!-- additional collaborators and configuration for this bean go here --> </bean>
<!-- more bean definitions for services go here --> </beans>
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List<String> userList = service.getUsernameList();
5 bean的概述
Bean Defination | |
属性 | 解释 |
Class | 创建beans |
Name | Beans的名称 |
Scope | Bean的作用域 |
Constructor args | DI---依赖注入 |
Properties | DI---依赖注入 |
Autowiring mode | 自动装配模式---自动装配协作者 |
Lazy initialization mode | 懒加载创建beans |
Initialization method | 初始化回调 |
Destruction method | 销毁回调 |
Autowiring modes(自动装配模型) using XML-based configuration metadata | |
Mode | Explanation |
no | 默认不自动装配,必须通过ref元素来引用bean,大型系统不建议改变默认设置,因为显式指定协作者可以提供更大的控制和清晰度,在某种程度上,它记录了一个系统的结构。 |
byName | 通过属性名称进行自动装配,spring寻找需要被自动装配并带有相同属性名称的bean。例如,如果一个bean定义为通过属性名称自动装配,它包含master的属性(同时,包含一个setMaster()方法),spring寻找带有master属性定义的bean并用它设置属性。 |
byType | 如果一个属性类型的bean存在于容器中,则允许属性自动进行。如果存在多于一个,就会抛出致命异常,这表明您不能为该bean使用。如果没有匹配的bean,则不会发生任何事情(属性未设置)。 |
constructor | 类似于byType,但适用于构造函数参数。如果容器中没有一个构造函数参数类型的bean,则会引发致命错误 |
@Resource和@Autowired的区别是什么?
6
1.提供者不同 @Resource是jdk提供的,@Autowired是Spring提供的
2.后置处理器不同 @Resource使用的是CommonAnnotationBeanPostProcessor,@Autowired使用的是AutowiredAnnotationBeanPostProcessor