Ioc(Inversion of control)控制反转
- 使用对象时,由主动new产生对象转化为外部提供对象,此过程对象控制权由对象转移到外部,此思想称为控制反转。
spring技术对Ioc思想进行了实现
- spring提供了一个容器称为Ioc容器,用来充当Ioc思想的“外部”
- Ioc容器负责对象的创建,初始化等一系列工作,被创建或者被管理的对象在Ioc容器中统称为Bean
DI(Dependency Injection)依赖注入
- 在容器中建立Bean与Bean之间的依赖关系的整个过程,被称为依赖注入
Ioc入门案例
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>spring_01_quickstart</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
</project>
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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!--1.导入spring的坐标spring-context,对应版本是5.2.10.RELEASE-->
<!--2.配置bean-->
<!--bean标签标示配置bean
id属性标示给bean起名字
class属性表示给bean定义类型-->
<bean id="bookDao" class="dang.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="dang.service.BookServiceImpl"/>
<!--7.配置server与dao的关系-->
<!--property标签表示配置当前bean的属性
name属性表示配置哪一个具体的属性
ref属性表示参照哪一个bean-->
</beans>
app.java
package dang;
import dang.dao.impl.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class app {
public static void main(String[] args) {
//3.获取Ioc容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
// 4.获取bean(根据bean配置id获取)
BookDao bookDao=(BookDao)ctx.getBean("bookDao");
bookDao.save();
// BookService bookService = (BookService) ctx.getBean("bookService");
// bookService.save();
}
}
BookServiceImpl.java
package dang;
import dang.dao.impl.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class app {
public static void main(String[] args) {
//3.获取Ioc容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
// 4.获取bean(根据bean配置id获取)
BookDao bookDao=(BookDao)ctx.getBean("bookDao");
bookDao.save();
// BookService bookService = (BookService) ctx.getBean("bookService");
// bookService.save();
}
}
------------
<details>
<summary>BookDaoImpl.java</summary>
package dang.dao.impl;
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("Book dao save...");
}
}
</details>