IOC 控制反转
【第一步】导入Spring坐标
<dependencies>
<!--导入spring的坐标spring-context,对应版本是5.2.10.RELEASE-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
</dependencies>
【第二步】定义Spring管理的类(接口)
- BookDao接口和BookDaoImpl实现类
public interface BookDao {
public void save();
}
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("book dao save ...");
}
}
【第三步】创建Spring配置文件,配置对应类作为Spring管理的bean对象
- 定义applicationContext.xml配置文件并配置BookServiceImpl
<?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="bookService" class="com.itheima.service.impl.BookServiceImpl"></bean>
</beans>
==注意事项:bean定义时id属性在同一个上下文中(IOC容器中)不能重复==
【第四步】初始化IOC容器(Spring核心容器/Spring容器),通过容器获取Bean对象
public class App {
public static void main(String[] args) {
//1.创建IoC容器对象,加载spring核心配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//2 从IOC容器中获取Bean对象(BookService对象)
BookService bookService= (BookService)ctx.getBean("bookService");
//3 调用Bean对象(BookService对象)的方法
bookService.save();
}
}
DI 依赖注入
【第一步】删除使用new的形式创建对象的代码
public class BookServiceImpl implements BookService {
private BookDao bookDao; //【第一步】删除使用new的形式创建对象的代码
public void save() {
System.out.println("book service save ...");
bookDao.save();
}
}
【第二步】提供依赖对象对应的setter方法
public class BookServiceImpl implements BookService {
private BookDao bookDao;
public void save() {
System.out.println("book service save ...");
bookDao.save();
}
//【第二步】提供依赖对象对应的setter方法
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
}
【第三步】配置service与dao之间的关系
在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"
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.huang.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.huang.service.impl.BookServiceImpl">
<!--配置server与dao的关系
property标签:表示配置当前bean的属性
name属性:表示配置哪一个具体的属性
ref属性:表示参照哪一个bean
-->
<property name="bookDao" ref="bookDao"/>
</bean>
</beans>
Bean的配置
基本配置
别名配置
Bean作用范围配置【重点】
配置说明:scope的取值不仅仅只有singleton和prototype,还有request、session、application、 websocket ,表示创建出的对象放置在web容器(tomcat)对应的位置。比如:request表示保存到request域中。
Bean的实例化
构造方法实例化
public class BookDaoImpl implements BookDao {
public BookDaoImpl() {
System.out.println("book dao constructor is running ....");
}
@Override
public void saveBook() {
System.out.println("保存图书");
}
}
<!--方式一:构造方法实例化bean-->
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
public class App {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
}
}
静态工厂实例化
public class StaticFactoryBean {
public static BookDao getBookDao(){
System.out.println("StaticFactoryBean");
return new BookDaoImpl();
}
}
<!-- 静态工厂获取实例 -->
<bean id="staticFactoryBean" class="com.huang.factory.StaticFactoryBean" factory-method="getBookDao"></bean>
public class App {
public static void main(String[] args) {
// 获取容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("SpringContext.xml");
BookService bookService = (BookService) applicationContext.getBean("bookService");
bookService.save();
}
}
实例工厂实例化
public class StaticFactoryBean {
public static BookDao getBookDao(){
System.out.println("StaticFactoryBean");
return new BookDaoImpl();
}
}
<!-- 实例工厂获取实例 -->
<bean id="instanceFactoryBean" class="com.huang.factory.InstanceFactoryBean" ></bean>-->
<bean id="bookDao" factory-bean="instanceFactoryBean" factory-method="getBookDao"></bean>
public class App {
public static void main(String[] args) {
// 获取容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("SpringContext.xml");
BookService bookService = (BookService) applicationContext.getBean("bookService");
bookService.save();
}
}
Spring内置工厂实例化
public class MyFactoryBean implements FactoryBean {
@Override
public Object getObject() throws Exception {
System.out.println("MyFactoryBean");
return new BookDaoImpl();
}
@Override
public Class<?> getObjectType() {
return null;
}
}
<bean id="bookDao" class="com.huang.factory.MyFactoryBean"></bean>
public class App {
public static void main(String[] args) {
// 获取容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("SpringContext.xml");
BookService bookService = (BookService) applicationContext.getBean("bookService");
bookService.save();
}
}
依赖注入(DI配置)
依赖注入的两种方式
- setter注入
- 简单类型
- 引用类型(很常用)
<bean id="bookService" class="com.huang.service.impl.BookServiceImpl">-->
<property name="bookDao" ref="bookDao"></property>
<property name="number" value="10"></property>
</bean>
- 构造器注入
- 简单类型
- 引用类型
<bean id="bookService" class="com.huang.service.impl.BookServiceImpl">
<constructor-arg name="bookDao" ref="bookDao"></constructor-arg>
<constructor-arg name="number" value="10"></constructor-arg>
</bean>