1. 功能案例
BookDao:
public interface BookDao {
public void save();
}
BookService:
public interface BookService {
public void save();
}
BookDao实现类BookDaoImpl:
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("book dao save ...");
}
}
BookService实现类BookServiceImpl:
public class BookServiceImpl implements BookService {
private BookDao bookDao = new BookDaoImpl();
public void save() {
System.out.println("book service save ...");
bookDao.save();
}
}
App调用业务层:
public class App {
public static void main(String[] args) {
BookService bookService = new BookServiceImpl();
bookService.save();
}
}
上面程序很简单,就是通过App调业务层的Service,Service再调数据层的BookDao。
运行效果:
book service save ...
book dao save ...
2. 通过Spring实现
前提:创建一个maven项目
1)导包,在pom.xml中添加spring-context jar包
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.33</version>
</dependency>
</dependencies>
2)我们沿用上面使用的几个功能类
public interface BookDao {
public void save();
}
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("book dao save ...");
}
}
public interface BookService {
public void save();
}
public class BookServiceImpl implements BookService {
private BookDao bookDao = new BookDaoImpl();
public void save() {
System.out.println("book service save ...");
bookDao.save();
}
}
3)resources下添加spring配置文件applicationContext.xml,并完成bean的配置。
在配置文件中完成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">
<!--1. 在pom.xml中导入Spring的坐标spring-context-->
<!--2. 配置Bean -->
<!--bean标签标示配置bean
id属性标示给bean起名字
class属性表示给bean定义类型
-->
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl"/>
</beans>
注意事项:bean定义时id属性在同一个上下文中(配置文件)不能重复
4)获取IOC容器,使用Spring提供的接口完成IOC容器的创建,创建App类,编写main方法。
public class App {
public static void main(String[] args) {
//获取IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
5)从容器中获取对象进行方法调用
public class App {
public static void main(String[] args) {
//获取IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = (BookService) ctx.getBean("bookService");
bookService.save();
}
}
6)运行,结果如下
book service save ...
book dao save ...
Spring的IOC入门案例已经完成,但是在BookServiceImpl的类中依然存在BookDaoImpl对象的new操作,它们之间的耦合度还是比较高,这块该如何解决,就需要用到下面的DI:依赖注入。
3. 实现DI--依赖注入
需求:基于上面的案例,在BookServiceImpl类中删除new对象的方式,使用Spring的DI完成Dao层的注入
步骤:
1.删除业务层中使用new的方式创建的dao对象
2.在业务层提供BookDao的setter方法
3.在配置文件中添加依赖注入的配置
4.运行程序调用方法
1)去除代码中的new
在BookServiceImpl类中,删除业务层中使用new的方式创建的dao对象
public class BookServiceImpl implements BookService {
//删除业务层中使用new的方式创建的dao对象
private BookDao bookDao;
public void save() {
System.out.println("book service save ...");
bookDao.save();
}
}
2)为属性提供setter方法
public class BookServiceImpl implements BookService {
//删除业务层中使用new的方式创建的dao对象
private BookDao bookDao;
public void save() {
System.out.println("book service save ...");
bookDao.save();
}
//提供对应的set方法
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
}
3)修改配置完成注入
<?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.itheima.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
<!--配置server与dao的关系-->
<!--property标签表示配置当前bean的属性
name属性表示配置哪一个具体的属性
ref属性表示参照哪一个bean
-->
<property name="bookDao" ref="bookDao"/>
</bean>
</beans>
注意:配置中的两个bookDao的含义是不一样的
- name="bookDao"中
bookDao的作用是让Spring的IOC容器在获取到名称后,将首字母大写,前面加set找对应的setBookDao()方法进行对象注入 - ref="bookDao"中
bookDao的作用是让Spring能在IOC容器中找到id为bookDao的Bean对象给bookService进行注入 - 综上所述,对应关系如下:
4:运行程序
book service save ...
book dao save ...
思路总结:
1)IOC思路总结
(1)Spring是使用容器来管理bean对象的,那么管什么?
- 主要管理项目中所使用到的类对象,比如(Service和Dao)
(2)如何将被管理的对象告知IOC容器?
- 使用配置文件
(3)被管理的对象交给IOC容器,要想从容器中获取对象,就先得思考如何获取到IOC容器?
- Spring框架提供相应的接口。
(4)IOC容器得到后,如何从容器中获取bean?
- 调用Spring框架提供对应接口中的方法,如上面案例的使用的ClassPathXmlApplicationContext,当然还有其它的。
(5)使用Spring导入哪些坐标?
- 用别人的东西,就需要在pom.xml添加对应的依赖
2) DI思路总结
(1)要想实现依赖注入,必须要基于IOC管理Bean
- DI的入门案例要依赖于前面IOC的入门案例
(2)Service中使用new形式创建的Dao对象是否保留?
- 需要删除掉,最终要使用IOC容器中的bean对象
(3)Service中需要的Dao对象如何进入到Service中?
- 在Service中提供方法,让Spring的IOC容器可以通过该方法传入bean对象
(4)Service与Dao间的关系如何描述?
- 使用配置文件