Day1: spring中的IOC以及基于XML的IOC配置
Day2: spring中的IOC以及基于注解的IOC配置
Day3: spring中的AOP以及基于注解和XML的配置
Day4: spring中的声明式事务以及基于注解和XML的配置 (jdbcTemplate)
IOC和AOP
- 耦合关系
耦合: 计算机代码之间的依赖关系( 紧密耦合|松散耦合)
解耦:
2. 配置文件
3. 反射
1.1 将属性配置到配置文件中
1.2 通过工厂的形式解耦
解耦的目的: 防止你的代码影响到别人的代码
1.3 获取对象
1.4 测试:
BeanFactorySingleton 单例工厂:
通过单例工厂模式对已存在的对象进行解耦
package cn.itcast.utils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 对象工厂: 通过BeanFactory获取目标对象的
* * 单例工厂
* * 多个线程访问同一个已经创建好的对象
* 1.xml配置文件只解析一次
* 2.使用静态代码块的形式解析配置文件
* 3.将解析的内容(id,classname),创建对象
* 4.将对象存到到map集合中.Map<id,Object>
* 5.配置一个方法,根据配置文件获取需要的对象
*/
public class BeanFactorySingleton {
//key = id ,value =对象
private static Map<String,Object> map = new HashMap<>();
static {
try {
//1.加载配置文件
InputStream is = BeanFactorySingleton.class.getClassLoader().getResourceAsStream("bean.xml");
//2.创建SaxReader对象
SAXReader reader = new SAXReader();
//3.创建document对象
Document document = reader.read(is);
//4.获取根节点
Element root = document.getRootElement();
//5.获取其中的每个bean节点
List<Element> beanElements = root.elements("bean");
for (Element beanElement : beanElements) {
String id = beanElement.attributeValue("id");
String className = beanElement.attributeValue("class");
//创建对象
Class<?> aClass = Class.forName(className);
Object object = aClass.newInstance();
map.put(id,object);
}
}catch (Exception e) {
}
}
/**
* 根据id获取目标对象
*/
public static Object getBean(String id) throws Exception { //accountService
return map.get(id);
}
}
单例工厂与多例工厂的区别
2. IOC概述
Ioc : inversion of controll (控制反转),对象的获取方式(控制)发生了变化
传统的形式,程序员自己在java代码中管理 ioc: 对象不需要程序员管,容器帮助我们管理,控制权在容器手中.
传统: 手动实例化(自己new)
举例: 自己主动出击找女朋友
IOC: 从容器(Map集合)中获取
举例: 婚姻中介,把要求告诉中介,中介帮你找.
IOC 的目的: 解耦. 底层实现方式:反射
3. Spring概述
4.Spring中IOC入门
4.1 创建工程导入坐标
4.2 创建工程的基本结构(接口和实现类 )
4.3 配置Spring的配置文件
- 位置:配置到resource的根目录下
- 命名:applicationContext.xml
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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
由于配置了bean标签,spring容器会自动创建AccountServiceImpl对象放入容器.
4.4 在java代码中测试spring容器
4.5 Spring中核心API对象
BeanFactory
ApplicationContext
ClassPathXmlApplicationContext
FileSystemXmlApplicationContex
4.6 第一个案例的执行过程
容器一创建,就创建对象
5. 对象的生命周期
5.1 scope
mybatis中SqlSession | struts2中的控制器类
配置对象的作用域: singleton | prototype
配置成prototype 容器产生的两个对象不一样
配置成singleton 容器产生的对象是同一个
5.2 init-method
Thread myThread = new MyThread();
myThread.start();
从容器中获取对象:thread
执行:thread.start()
5.3 destory-method
6. 依赖注入(重点)
依赖注入: di(Dependency Injection),对存在依赖关系的对象或者属性进行赋值
依赖注入的方式: 构造方法注入 和 set方法注入
在java代码中配置属性以及属性对应的构造或者set方法
在xml中配置数据赋值
6.1 注入基本数据类型
6.1.1 构造方法注入
<1> 实现类:
<2>配置文件 bean.xml
<3> 测试:
思路:
面试问题
IOC : 控制反转
对象的生命周期( Scope,init-method,destory-method )
依赖注入:反射
6.1.1.1 Java代码
6.1.1.2 Xml注入
6.1.2 set方法注入(重要)
6.1.2.1 java代码
6.1.2.2 xml配置文件
6.2 注入集合类型
Set方法注入
6.2.1 数组类型的数据
属性: array数组,list集合,set集合
6.2.1.1 Java代码
6.2.1.2 Xml配置
测试:
6.2.2 Key-value类型的数据
属性: map, properties
6.2.2.1 Java代码
6.2.2.2 Xml配置
测试:
6.3 注入对象
6.3.1 Xml配置
applicationContext.xml内代码:
service层实现类代码:
测试类: