🌻🌻 目录
一、前提介绍
1.1 为什么要学?
1.2 学什么?
1.3 怎么学?
二、Spring相关概念
2.1 初始Spring
2.1.1 Spring家族
说明:
2.1.2 了解 Spring 发展史
2.2 Spring系统架构
2.2.1 系统架构图
2.2.2 课程学习路线
2.3 Spring核心概念
2.3.1 目前项目中的问题
(1)业务层需要调用数据层的方法,就需要在业务层new数据层的对象
(2)如果数据层的实现类发生变化,那么业务层的代码也需要跟着改变,发生变更后,都需要进行编译打包和重部署
(3)所以,现在代码在编写的过程中存在的问题是:耦合度偏高
针对这个问题,该如何解决呢?
2.3.2 IOC、IOC容器、Bean、DI
- IOC(Inversion of Control)控制反转
(1) 什么是控制反转呢?
(2) Spring和IOC之间的关系是什么呢?
(3) IOC容器的作用以及内部存放的是什么?
(4)当IOC容器中创建好service和dao对象后,程序能正确执行么?
像这种在容器中建立对象与对象之间的绑定关系就要用到DI:
- DI(Dependency Injection)依赖注入
(1) 什么是依赖注入呢?
(2) IOC容器中哪些bean之间要建立依赖关系呢?
介绍完Spring的IOC和DI的概念后,我们会发现这两个概念的最终目标就是:充分解耦
,具体实现靠:
2.3.3 核心概念小结
这节比较重要,重点要理解什么是IOC/DI思想、什么是IOC容器和什么是Bean:
(1) 什么是IOC/DI思想?
(2) 什么是IOC容器?
(3)什么是Bean?
三、入门案例
3.1 IOC入门案例
对于入门案例,我们得先分析思路然后再代码实现,
3.1.1 入门案例思路分析
(1)Spring是使用容器来管理bean对象的,那么管什么?
(2)如何将被管理的对象告知IOC容器?
(3)被管理的对象交给IOC容器,要想从容器中获取对象,就先得思考如何获取到IOC容器?
(4)IOC容器得到后,如何从容器中获取bean?
(5)使用Spring导入哪些坐标?
3.1.2 入门案例代码实现
需求分析:将BookServiceImpl和BookDaoImpl交给Spring管理,并从容器中获取对应的bean
对象进行方法调用。
1.创建Maven的java项目
2.pom.xml添加Spring的依赖jar包
3.创建BookService,BookServiceImpl,BookDao和BookDaoImpl四个类
4.resources下添加spring配置文件,并完成bean的配置
5.使用Spring提供的接口完成IOC容器的创建
6.从容器中获取对象进行方法调用
步骤1:创建Maven项目(前提是已经安装了maven,前面文章总结到)
(1)
创建成功默认显示如下:
(2) 创建包创建数据接口
package com.glory.dao;
/**
* @Author:Daniel
* @Version 1.0
*/
public interface BookDao {
public void save();
}
(3) 创建数据接口实现类
package com.glory.dao.impl;
import com.glory.dao.BookDao;
/**
* @Author:Daniel
* @Version 1.0
*/
public class BookDaoImpl implements BookDao {
@Override
public void save() {
System.out.println("book dao save 。。。。");
}
}
(4)创建业务接口及其实现类
package com.glory.service;
/**
* @Author:Daniel
* @Version 1.0
*/
public interface BookService {
public void save();
}
package com.glory.service.impl;
import com.glory.dao.BookDao;
import com.glory.dao.impl.BookDaoImpl;
import com.glory.service.BookService;
/**
* @Author:Daniel
* @Version 1.0
*/
public class BookServiceImpl implements BookService {
private BookDao bookDao = new BookDaoImpl();
@Override
public void save() {
System.out.println("book service save。。。。");
bookDao.save();
}
}
(5)测试
package com.glory.service;
import com.glory.service.impl.BookServiceImpl;
/**
* @Author:Daniel
* @Version 1.0
*/
public class App {
public static void main(String[] args) {
BookService bookService = new BookServiceImpl();
bookService.save();
}
}
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
resources下添加spring配置文件applicationContext.xml,并完成bean的配置
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--bean标签标示配置bean
id属性标示给bean起名字
class属性表示给bean定义类型
-->
<bean id="bookDao" class="com.glory.dao.impl.BookDaoImpl"></bean>
<bean id="bookService" class="com.glory.service.impl.BookServiceImpl"></bean>
</beans>
注意事项:bean定义时id属性在同一个上下文中(配置文件)不能重复
package com.glory.service;
import com.glory.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author:Daniel
* @Version 1.0
*/
public class App2 {
public static void main(String[] args) {
//获取IOC容器
ApplicationContext act =
new ClassPathXmlApplicationContext("spring_applicationContext.xml");
// BookDao bookDao = (BookDao) act.getBean("bookDao");
BookService bookService = (BookService) act.getBean("bookService");
// bookDao.save();
bookService.save();
}
}
3.2 DI入门案例
3.2.1 入门案例思路分析
(1)要想实现依赖注入,必须要基于IOC管理Bean
(2)Service中使用new形式创建的Dao对象是否保留?
(3)Service中需要的Dao对象如何进入到Service中?
(4)Service与Dao间的关系如何描述?
3.2.2 入门案例代码实现
需求:基于IOC入门案例,在BookServiceImpl类中删除new对象的方式,使用Spring的DI完成
Dao层的注入
1.删除业务层中使用new的方式创建的dao对象
2.在业务层提供BookDao的setter方法
3.在配置文件中添加依赖注入的配置
4.运行程序调用方法
package com.glory.service.impl;
import com.glory.dao.BookDao;
import com.glory.dao.impl.BookDaoImpl;
import com.glory.service.BookService;
/**
* @Author:Daniel
* @Version 1.0
*/
public class BookServiceImpl implements BookService {
//删除业务层中使用new的方式创建的dao对象
//private BookDao bookDao = new BookDaoImpl();
private BookDao bookDao;
@Override
public void save() {
System.out.println("book service save。。。。");
bookDao.save();
}
//提供对应的set方法
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
}
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--bean标签标示配置bean
id属性标示给bean起名字
class属性表示给bean定义类型
-->
<bean id="bookDao" class="com.glory.dao.impl.BookDaoImpl"></bean>
<bean id="bookService" class="com.glory.service.impl.BookServiceImpl">
<!--配置server与dao的关系-->
<!--property标签表示配置当前bean的属性
name属性表示配置哪一个具体的属性
ref属性表示参照哪一个bean
-->
<property name="bookDao" ref="bookDao"></property>
</bean>
</beans>
综上所述,对应关系如下: