
🚩 环境准备
💡 maven(3.3.9)
💡 JDK(1.8)
💡 Spring(5.x)
💡 IDEA 2019
🚩 工程搭建
📌 POM文件
<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.o98k</groupId> <artifactId>spring</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- spring 核心组件中的4个依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.0.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.0.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.0.7.RELEASE</version> </dependency> <!-- 单元测试Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> <build> <plugins> <!-- 配置Maven的JDK编译级别 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
📌 Spring配置文件(只编写配置文件头)
<?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"> </beans>
🚩 具体实现
💡 在Spring 的XML配置文件中配置一个bean标签,该标签最终会被加载为一个BeanDefition对象(描述对象信息)
📌 案例演示
🔥 演示思路
编写UserService接口的实现类
将UserService实现类交给Spring IoC容器管理
从Spring IoC容器中获取UserService实现类🔥 编写接口:StudentService
public interface StudentService{ void saveStudent(); }
🔥 编写实现类:StudentServiceImpl
public class StudentServiceImpl implements StudentService{ @Override public void saveStudent(){ System.out.println("保存学生Service实现"); } }
🔥 编写XML文件: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="studentService" class="com.o98k.service.impl.StudentServiceImpl"></bean> </beans>
🔥 编写单元测试代码:TestSpring
public class TestSpring { @Test public void testIoc(){ //创建ApplicationContext(容器) ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); StudentService studentService1 = context.getBean(StudentService.class); StudentService studentService2 = (StudentService) context.getBean("studentService"); studentService1.saveStudent(); studentService2.saveStudent(); } }
📌 bean标签详解
🔥 bean标签作用:
A. 用于配置对象让 spring 来创建的
B. 默认情况下它调用的是类中的无参构造函数。如果没有无参构造函数则不能创建成功
🔥 bean标签属性:
id :给对象在
提供一个唯一标识。用于获取对象。
class :指定类的全限定类名。用于反射创建对象。默认情况下
scope :指定对象的作用范围singleton :给对象在容器中提供一个唯一标识。用于获取对象。
prototype :多例的
request :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 request 域中
session :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 session 域中
global session :WEB 项目中,应用在 Portlet 环境.如果没有 Portlet 环境那么globalSession 相当于 sessioninit-method :指定类中的初始化方法名称
destroy-method :指定类中销毁方法名称。比如DataSource的配置中一般需要指定destroy-method=“close”
🔥 bean的作用范围:
A. 一个应用只有一个对象的实例。它的作用范围就是整个引用。
B. 生命周期:
👉对象出生:当应用加载,创建容器时,对象就被创建了。
👉对象活着:只要容器在,对象一直活着。
👉对象死亡:当应用卸载,销毁容器时,对象就被销毁了。
A. 每次访问对象时,都会重新创建对象实例
B. 生命周期:
👉对象出生:当使用对象时,创建新的对象实例。
👉对象活着:只要对象在使用中,就一直活着。
👉对象死亡:当对象长时间不用时,被 java 的垃圾回收器回收了。
🚩 实例化bean的三种方式
🔥 第一种:使用默认无参构造函数
在默认情况下:它会根据默认无参构造函数来创建类对象。
如果 bean 中没有默认无参构造函数,将会创建失败。<bean id="studentService" class="com.o98k.service.impl.StudentServiceImpl"/>
🔥 第二种:静态工厂
/** * 模拟一个静态工厂,创建业务层实现类 */ public class StaticFactory { public static StudentService createStudentService(){ return new StudentServiceImpl(); } }
使用 StaticFactory 类中的静态方法 createStudentService 创建对象,并存入 spring 容器
👉id 属性:指定 bean 的 id,用于从容器中获取
👉class 属性:指定静态工厂的全限定类名
👉factory-method 属性:指定生产对象的静态方法<bean id="studentService" class="com.o98k.factory.StaticFactory" factory-method="createStudentService"></bean>
🔥 第三种:实例工厂
/** * 模拟一个实例工厂,创建业务层实现类 * 此工厂创建对象,必须现有工厂实例对象,再调用方法 */ public class InstanceFactory { public StudentService createStudentService(){ return new StudentServiceImpl(); } }
先把工厂的创建交给 spring 来管理。
然后在使用工厂的 bean 来调用里面的方法
👉factory-bean 属性:用于指定实例工厂 bean 的 id。
👉factory-method 属性:用于指定实例工厂中创建对象的方法。<bean id="instancFactory" class="com.o98k.factory.InstanceFactory"></bean> <bean id="studentService" factory-bean="instancFactory" factory-method="createStudentService"></bean>
⚡ 本文章非原创
⚡ 仅供参考学习
⚡ 内容来源于某视频网教学课件
网友评论