Spring Framework是Spring生态圈中最基础的项目,是其他项目的根基
详细介绍:
IOC
:控制反转
对象的创建控制权由程序转变为外部
,这样就被称为控制反转
外部
被管理或者被创建
的对象在IOC当中就叫做Bean
1. 导入Spring坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
2. 定义Spring管理类(接口)
public class BookServiceImpl implements BookService {
private final BookDao bookDao = new BookDaoImpl();
@Override
public void save() {
System.out.println("save Service !!!!!");
bookDao.save();
}
}
package com.huanglei.Service;
public interface BookService {
public void save();
}
3. 创建Spring配置文件 ,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 id="BookDao" class="com.huanglei.dao.impl.BookDaoImpl"/>
<bean id="BookService" class="com.huanglei.Service.impl.BookServiceImpl" />
</beans>
4. 初始化IOC容器(获取容器),通过容器获取到Bean
public class App1 {
public static void main(String[] args) {
//首先获取到IOC容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取到Bean
BookDao bookDao = (BookDao) applicationContext.getBean("BookDao");
bookDao.save();
BookService bookService = (BookService) applicationContext.getBean("BookService");
bookService.save();
}
}
将上述的全部利用new的方式进行替换
public class BookServiceImpl implements BookService {
private BookDao bookDao ;
@Override
public void save() {
System.out.println("save Service !!!!!");
bookDao.save();
}
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 id="BookDao" class="com.huanglei.dao.impl.BookDaoImpl"/>
<!--
property标签表示配置当前Bean的属性
name:表示配置的哪一个具体的属性
ref:表示参照的哪一个Bean,表示的为哪一个Bean的id
-->
<bean id="BookService" class="com.huanglei.Service.impl.BookServiceImpl">
<property name="bookDao" ref="BookDao"/>
</bean>
</beans>
创建的是单一Bean,还是多个Bean
类别 | 描述 |
---|---|
名称 | scope |
类型 | 属性 |
所属 | bean标签 |
功能 | 定义bean的作用范围,可选范围如下: singleton:单例(默认) ;prototype :非单例 |
范例 |
|
bean本质上就是对象,创建bean使用构造方法完成
提供可访问的构造方法:
public BookDaoImpl(){
System.out.println("BookDao servicing !!!!");
}
配置条件:
<bean id="BookDao" class="com.huanglei.dao.impl.BookDaoImpl"/>
Bean创建的方法是通过其调用该类的误餐构造方法来达到创建效果的,若没有无参构造就会报错BeanCreationException
静态工厂:
public class OrderDaoFactory {
public static OrderFactory getOrderFactory(){
return new OrderFactoryImpl();
}
}
public interface OrderFactory {
public void save();
}
public class OrderFactoryImpl implements OrderFactory {
@Override
public void save() {
System.out.println("orderFactory save!!!");
}
}
配置方法:
<bean id="OrderFactory" class="com.huanglei.Factory.OrderDaoFactory" factory-method="getOrderFactory"/>
静态工厂:
public class OrderDaoFactory {
public OrderFactory getOrderFactory(){
return new OrderFactoryImpl();
}
}
public interface OrderFactory {
public void save();
}
public class OrderFactoryImpl implements OrderFactory {
@Override
public void save() {
System.out.println("orderFactory save!!!");
}
}
调用接口:
public class OrderDaoFactoryBean implements FactoryBean<OrderFactory> {
/*
* 代替实例化对象的创建对象
* */
@Override
public OrderFactory getObject() throws Exception {
return new OrderFactoryImpl();
}
/*
* 创建的类型
* */
@Override
public Class<?> getObjectType() {
return OrderFactory.class;
}
/*
* 这里来设置是否为单例对象
* true:单
* false:非单
* */
@Override
public boolean isSingleton() {
return false;
}
配置文件:
<bean id="OrderFactory" class="com.huanglei.Factory.OrderDaoFactoryBean"/>
生命周期控制方法:
<bean id="OrderFactory" class="com.huanglei.Factory.OrderDaoFactoryBean" init-method="init" destroy-method="destroy"/>
文件方法:
public class OrderDaoFactoryBean implements FactoryBean<OrderFactory>, InitializingBean, DisposableBean {
/*
* 代替实例化对象的创建对象
* */
@Override
public OrderFactory getObject() throws Exception {
return new OrderFactoryImpl();
}
/*
* 创建的类型
* */
@Override
public Class<?> getObjectType() {
return OrderFactory.class;
}
/*
* 这里来设置是否为单例对象
* true:单
* false:非单
* */
@Override
public boolean isSingleton() {
return false;
}
public void init(){
System.out.println("init…………");
}
public void destroy(){
System.out.println("destroy…………");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("等到属性设置设置完后才执行这个方法");
}
}
关闭容器的方法:
//通过close进行关闭
classPathXmlApplicationContext.close();
//调用钩子的方法关闭bean
classPathXmlApplicationContext.registerShutdownHook();