Spring Framework是Spring生态圈中最基础的项目,是其他项目的根基
IOC(Inversion of Control)控制反转:
使用对象时,由主动new产生对象转换为由外部提供对象,此过程中对象创建控制权由程序转移到外部,此思想称为控制反转。通俗的讲就是 将new对象的权利交给Spring,我们从Spring中获取对象使用即可
Spring技术对IoC思想进行了实现:
DI(Dependency Injection)依赖注入:
在容器中建立bean与bean之间的依赖关系的整个过程,称为依赖注入。
目标:
最终效果: 使用对象时不仅可以直接从IoC容器中获取,并且获取到的bean已经绑定了所有的依赖关系
Gitee仓库: https://gitee.com/yuan0_0/spring_frame_quick-start.git
实现步骤:
【第一步】导入Spring坐标
【第二步】定义Spring管理的类(接口)
【第三步】创建Spring配置文件,配置对应类作为Spring管理的bean对象
【第四步】初始化IOC容器(Spring核心容器/Spring容器),通过容器获取bean对象
新建 src/main/java/com/it/service/BookService.java
文件:
public interface BookService {
public void save();
}
新建 src/main/java/com/it/service/impl/BookServiceImpl.java
文件:
public class BookServiceImpl implements BookService {
private BookDao bookDao = new BookDaoImpl();
public void save() {
System.out.println("book service save ...");
bookDao.save();
}
}
新建 src/main/java/com/it/dao/BookDao.java
文件:
public interface BookDao {
public void save();
}
新建 src/main/java/com/it/dao/impl/BookDaoImpl.java
文件:
public class BookDaoImpl implements BookDao {
public void save() {
System.out.println("book dao save ...");
}
}
新建 src/main/java/com/it/App.java
文件:
public class App {
public static void main(String[] args) {
BookService bookService = new BookServiceImpl();
bookService.save();
}
}
编辑 pom.xml
文件:
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.10.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
dependencies>
新建 src/main/resources/applicationContext.xml
文件:
<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.it.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.it.service.impl.BookServiceImpl"/>
beans>
新建src/main/java/com/it/App2.java
文件:
public class App2 {
public static void main(String[] args) {
// 1.创建IoC容器对象,加载spring核心配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2.从IOC容器中获取Bean对象(bookDao对象)
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
// 3.调用Bean对象(bookDao对象)的方法
bookDao.save();
System.out.println("-----");
BookService bookService = (BookService) ctx.getBean("bookService");
bookService.save();
}
}
编辑 src/main/java/com/it/service/impl/BookServiceImpl.java
文件:
public class BookServiceImpl implements BookService {
// private BookDao bookDao = new BookDaoImpl();
// 删除业务层中使用new方式创建的Dao对象
private BookDao bookDao;
public void save() {
System.out.println("book service save ...");
bookDao.save();
}
}
编辑 src/main/java/com/it/service/impl/BookServiceImpl.java
文件:
public class BookServiceImpl implements BookService {
// private BookDao bookDao = new BookDaoImpl();
// 删除业务层中使用new方式创建的Dao对象
private BookDao bookDao;
public void save() {
System.out.println("book service save ...");
bookDao.save();
}
// 提供对应的setter方法
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
}
编辑 src/main/resources/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.it.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.it.service.impl.BookServiceImpl">
<!--配置server与dao的关系
property标签:表示配置当前bean的属性
name属性:表示配置哪一个具体的属性
ref属性:表示参照哪一个bean
-->
<property name="bookDao" ref="bookDao" />
</bean>
</beans>
配置说明:
类别 | 描述 |
---|---|
名称 | bean |
类型 | 标签 |
所属 | beans标签 |
功能 | 定义Spring核心容器管理的对象 |
格式 |
|
属性列表 | id:bean的id,使用容器可以通过id值获取对应的bean,在一个容器中id值唯一 class:bean的类型,即配置的bean的全路径类名 |
范例 |
|
类别 | 描述 |
---|---|
名称 | name |
类型 | 属性 |
所属 | bean标签 |
功能 | 定义bean的别名,可定义多个,使用逗号(,)分号(;)空格( )分隔 |
范例 |
|
编辑 src/main/resources/applicationContext.xml
文件:
<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.it.dao.impl.BookDaoImpl"/>
<bean id="bookService" name="service service4 bookEbi" class="com.it.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao" />
bean>
beans>
新建 src/main/java/com/it/AppForName.java
文件:
public class AppForName {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = (BookService) ctx.getBean("service");
bookService.save();
}
}
类别 | 描述 |
---|---|
名称 | scope |
类型 | 属性 |
所属 | bean标签 |
功能 | 定义bean的作用范围,可选范围如下:singleton 单例(默认)、prototype 非单例 |
范例 |
|
编辑 src/main/resources/applicationContext.xml
文件:
<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="bookService" name="service service4 bookEbi" class="com.it.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao" />
bean>
<bean id="bookDao" class="com.it.dao.impl.BookDaoImpl" scope="prototype"/>
beans>
新建 src/main/java/com/it/AppForScope.java
文件:
public class AppForScope {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bookDao1 = (BookDao) ctx.getBean("bookDao");
BookDao bookDao2 = (BookDao) ctx.getBean("bookDao");
System.out.println(bookDao1);
// => com.it.dao.impl.BookDaoImpl@25bbe1b6
System.out.println(bookDao2);
// => com.it.dao.impl.BookDaoImpl@5702b3b1
}
}
在我们的实际开发当中,绝大部分的Bean是单例的,也就是说绝大部分Bean不需要配置scope属性
bean本质上就是对象,创建bean使用构造方法完成
Gitee仓库: https://gitee.com/yuan0_0/spring_frame_bean_instance.git
编辑 pom.xml
文件:
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>spring_01_bean_instanceartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.10.RELEASEversion>
dependency>
dependencies>
project>
新建 src/main/java/com/it/dao/BookDao.java
文件:
public interface BookDao {
public void save();
}
新建 src/main/java/com/it/dao/impl/BookDaoImpl.java
文件:
public class BookDaoImpl implements BookDao {
// 创建无参构造方法
public BookDaoImpl() {
System.out.println("book dao constructor is running ....");
}
public void save() {
System.out.println("book dao save ...");
}
}
新建 src/main/resources/applicationContext.xml
文件:
<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.it.dao.impl.BookDaoImpl"/>
beans>
新建 src/main/java/com/it/AppForInstanceBook.java
文件:
public class AppForInstanceBook {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
}
}
无参构造方法如果不存在,将抛出异常
BeanCreationException
新建 src/main/java/com/it/dao/OrderDao.java
文件:
public interface OrderDao {
public void save();
}
新建 src/main/java/com/it/dao/impl/OrderDaoImpl.java
文件:
public class OrderDaoImpl implements OrderDao {
public void save() {
System.out.println("order dao save ...");
}
}
新建 src/main/java/com/it/factory/OrderDaoFactory.java
文件:
//静态工厂创建对象
public class OrderDaoFactory {
public static OrderDao getOrderDao(){
System.out.println("factory setup....");
return new OrderDaoImpl();
}
}
新建 src/main/resources/applicationContext.xml
文件:
<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="orderDao" class="com.it.factory.OrderDaoFactory" factory-method="getOrderDao"/>
beans>
新建 src/main/java/com/it/AppForInstanceOrder.java
文件:
public class AppForInstanceOrder {
public static void main(String[] args) {
//通过静态工厂创建对象
OrderDao orderDao = OrderDaoFactory.getOrderDao();
orderDao.save();
}
}
新建 src/main/java/com/it/dao/UserDao.java
文件:
public interface UserDao {
public void save();
}
新建 src/main/java/com/it/dao/impl/UserDaoImpl.java
文件:
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("user dao save ...");
}
}
新建 src/main/java/com/it/factory/UserDaoFactory.java
文件:
//实例工厂创建对象
public class UserDaoFactory {
public UserDao getUserDao(){
return new UserDaoImpl();
}
}
新建 src/main/java/com/it/AppForInstanceUser.java
文件:
public class AppForInstanceUser {
public static void main(String[] args) {
//创建实例工厂对象
UserDaoFactory userDaoFactory = new UserDaoFactory();
//通过实例工厂对象创建对象
UserDao userDao = userDaoFactory.getUserDao();
userDao.save();
}
}
新建 src/main/resources/applicationContext.xml
文件:
<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="userFactory" class="com.it.factory.UserDaoFactory"/>
<bean id="userDao" factory-method="getUserDao" factory-bean="userFactory"/>
beans>
新建 src/main/java/com/it/AppForInstanceUser.java
文件:
public class AppForInstanceUser {
public static void main(String[] args) {
// //创建实例工厂对象
// UserDaoFactory userDaoFactory = new UserDaoFactory();
// //通过实例工厂对象创建对象
// UserDao userDao = userDaoFactory.getUserDao();
// userDao.save();
// 实例化工厂
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) ctx.getBean("userDao");
userDao.save();
}
}
新建 src/main/java/com/it/factory/UserDaoFactoryBean.java
文件:
public class UserDaoFactoryBean implements FactoryBean<UserDao> {
//代替原始实例工厂中创建对象的方法
// bean实例
public UserDao getObject() throws Exception {
return new UserDaoImpl();
}
// bean类型
public Class<?> getObjectType() {
return UserDao.class;
}
// bean作用范围: 单例
public boolean isSingleton() {
return true;
}
}
编辑 src/main/resources/applicationContext.xml
文件:
<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="userDao" class="com.it.factory.UserDaoFactoryBean"/>
beans>
编辑 src/main/java/com/it/AppForInstanceUser.java
文件:
public class AppForInstanceUser {
public static void main(String[] args) {
// //创建实例工厂对象
// UserDaoFactory userDaoFactory = new UserDaoFactory();
// //通过实例工厂对象创建对象
// UserDao userDao = userDaoFactory.getUserDao();
// userDao.save();
// 实例化工厂
// ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// UserDao userDao = (UserDao) ctx.getBean("userDao");
// userDao.save();
// 测试bean作用范围: 单例/非单例
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao1 = (UserDao) ctx.getBean("userDao");
UserDao userDao2 = (UserDao) ctx.getBean("userDao");
System.out.println(userDao1);
System.out.println(userDao2);
}
}