赞
踩
核心概念(IoC 和 DI)
Inversion of Control,控制反转:使用对象时,在程序中不要主动使用new产生对象,转换由外部提供对象。此过程中对象创建控制权转移到外部。
IoC容器
负责对象的创建、初始化等一系列工作,被创建或被管理的对象在IoC容器中统称为Bean
。Dependency Injection,依赖注入:在容器中建立bean
与bean
之间的依赖关系的过程。
目标:充分解耦
- 使用IoC容器管理bean(IoC)
- 在IoC容器内将有依赖关系的bean进行关系绑定(DI)
最终效果:
- 使用对象时不仅可以直接从IoC容器中获取,并且获取到的Bean已经绑定了所有的依赖关系。
基础配置、别名配置、作用范围(默认为单例,控制创建bean实例的数量)
三种方式。
BeanCreationException
。默认就是构造方法,因此可以省略不写。bean初始化方法
bean销毁方法
bean声明周期控制
- 配置
- init-method
- destroy-method
- 接口(了解)
- InitializingBean
- DisposableBean
关闭容器:ConfigurAbleapplicationContext
- close()
- registerShutdownHook()
set方法
property标签value属性
注入引用类型对象set方法
property标签ref属性
注入引用类型对象(了解即可)
构造方法
constructor-arg标签value属性
注入引用类型对象构造方法
constructor-arg标签ref属性
注入引用类型对象对于参数适配问题,有以下两种方式
- 配置中使用
constructor-arg标签type属性
设置按形参类型注入- 配置中使用
constructor-arg标签index属性
设置按形参位置注入
依赖注入方式选择
- 强制依赖使用构造器进行,使用setter注入有概率不进行注入导致null对象出现
- 可选依赖使用setter注入进行,灵活性强
- Spring框架倡导使用构造器,第三方框架内部大多数采用构造器注入的形式进行数据初始化,相对严谨
- 如果有必要可以两者同时使用,使用构造器注入完成强制依赖的注入,使用setter注入完成可选依赖的注入
- 实际开发过程中还要根据实际情况分析,如果受控对象没有提供setter方法就必须使用构造器注入
- 自己开发的模块推荐使用setter注入
IoC容器根据bean所依赖的资源在容器中自动查找并注入到bean中的过程。
配置中使用bean标签autowire属性
设置自动装配的类型。
数组 int[] array;
List<String> list;
Set<String> set;
Map<String, String> map;
Properties properties;
pom.xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
applocationContext.xml
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring_db"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
避免把username等属性直接在xml中填写,而是通过properties文件进行管理:applicationContext.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
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
<context:property-placeholder location="jdbc.properties"/>
<property name="username" value="${jdbc.username}"/>
不加载系统属性
<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
- 1
加载多个properties文件
<context:property-placeholder location="jdbc.properties, msg.properties" />
- 1
加载所有properties文件
<context:property-placeholder location="*.properties" />
- 1
加载properties文件标准格式
<context:property-placeholder location="classpath:*.properties" />
- 1
从类路径或jar包中搜索并加载properties文件
<context:property-placeholder location="classpath*:*.properties" />
- 1
即,容器初始化。有两种加载方式
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext ctx = new FileSystemXmlApplicationContext("D:\\applicationContext.xml");
以上两种加载方式都可以同时加载多个配置文件,中间用逗号,
隔开即可。意思是将多个bean的配置合并到一起。
有三种方式:
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
BookDao bookDao = ctx.getBean("bookDao", BoolDao.class);
BookDao bookDao = ctx.getBean(BookDao.class);
BeanFactory初始化
- 类路径加载配置文件
Resource resources = new ClassPathResource("applicationContext.xml"); BeanFactory bf = new XmlBeanFactory(resources); BookDao boolDao = bf.getBean("boolDao", BookDao.class); bookDao.save();
- 1
- 2
- 3
- 4
- BeanFactory创建完毕后,所有的bean均为延迟加载
容器相关
bean相关
依赖注入相关
Spring3.0升级,使用Java类代替配置文件(一点配置文件都不需要写了),开启Spring快速开发
在这里插入图片描述
当有相同类型的bean时,按名称注入
手写新的方法,添加@bean,方法的返回值就是一个bean
数组格式为@import({JdbcConfig.class})
扫描式的隐藏性太强,建议使用@import导入式。
自动装配。按类型注入。
MyBatis程序核心对象分析
最核心的对象是:SqlSessionFactory
整合MyBatis
MyBatis管的是SqlSessionFactory
对象
只有两个Bean要配置
SqlSessionFactoryBean
MapperScannerConfigurer
核心概念:Aspect Oriented Programming,面向切面编程。一种编程范式,指导开发者如何组织程序结构。
作用:在不惊动原始设计的基础上为其进行功能增强(代理模式也能做增强)。即,原始代码不需要修改。
Spring理念:无侵入式。(代码中无Spring的痕迹,但是Spring对其进行了功能增强)
描述 | AOP核心概念 |
---|---|
原始方法(所有方法) | “连接点” Joinpoint |
要追加功能的方法(共性功能匹配的方法) | “切入点” Pointcut |
共性功能/方法(抽取出来) | “通知” Advice,其依赖的类称为“通知类” |
在哪些切入点上执行哪些通知,描述通知与切入点的对应关系 | “切面” Aspect |
切入点表达式标准格式(不重要)
学习过程中慢慢体会,不需要死记硬背
5 种类型指的就是 5 种位置。
一般有三种:获取参数、获取返回值、获取异常(了解)
对接口进行描述,可以降低代码的耦合度。
事务传播行为:事务协调员对事务管理员所携带事务的处理态度:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。