2、Spring的IOC标签介绍以及实例
一、Spring_ioc配置文件bean标签介绍
1、 bean标签
名称:bean
类型:标签
归属:beans标签
作用:定义spring中的资源,受此标签定义的资源将受到spring控制
格式:
<beans>
<bean/>
</beans>
基本属性:
<bean id="bean_id" name="beanName1,beanName2" class="ClassName"></bean>
基本属性解释:
id:bean的名称,通过id值获取bean
class:bean的类型
name:bean的名称,可以通过name值获取bean,用于多人配合时给bean起别名
2、 bean标签下scope属性
名称:scope
类型:属性
归属:bean标签
作用:定义bean的作用范围
格式:
<bean scope="singleton"></bean>
取值:
singleton:设定创建出的对象保存在spring容器中,是一个单例的对象
prototype:设定创建出的对象保存在spring容器中,是一个非单例的对象
request、session、application、websocket 设定创建出的对象放置在web容器对应的位置
演示scope标签
配置文件spring.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">
<!--演示scope的值为singleton是,单例创建对象-->
<bean id="userService" class="com.why.service.impl.UserServiceImpl" scope="singleton"></bean>
<!--演示scope的值为singleton是,单例创建对象-->
<bean id="userService" class="com.why.service.impl.UserServiceImpl" scope="prototype"></bean>
</beans>
测试scope=singleton属性
package com.why.controller;
import com.why.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Laity
* @date 2021年11月16日 9:41
*/
public class UserController {
public static void main(String[] args) {
//获取Spring上下文环境 (加载配置文件)
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
/*
通过getBean方法得到Spring容器中实例化好的Bean对象 (实例化Bean对象)
userService代表的是配置文件中bean标签的id属性值(id标识唯一的bean)
*/
UserService userService = (UserService) context.getBean("userService");
UserService userService1 = (UserService) context.getBean("userService");
System.out.println(userService);
System.out.println(userService1);
}
}
运行scope="singleton"时结果

运行scope="prototype"时结果

3、 bean生命周期
名称:init-method,destroy-method
类型:属性
归属:bean标签
作用:定义bean对象在初始化或销毁时完成的工作
格式:
<bean init-method="init" destroy-method="destroy></bean>
取值:bean对应的类中对应的具体方法名
注意事项:
当scope=“singleton”时,spring容器中有且仅有一个对象,init方法在创建容器时仅执行一次
当scope=“prototype”时,spring容器要创建同一类型的多个对象,init方法在每个对象创建时均执行一次
当scope=“singleton”时,关闭容器会导致bean实例的销毁,调用destroy方法一次
当scope=“prototype”时,对象的销毁由垃圾回收机制gc()控制,destroy方法将不会被执行
测试生命周期
- 在业务层实现类创建两个类
package com.why.service.impl;
import com.why.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public void saveUser() {
System.out.println("Hello,Spring............");
}
public void init(){
System.out.println("bean初始化");
}
public void destroy(){
System.out.println("bean销毁");
}
}
- 配置文件
<?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">
<!--当前scope为singleton单例时-->
<bean id="userService" class="com.why.service.impl.UserServiceImpl"
scope="singleton"
init-method="init"
destroy-method="destroy"
/>
</beans>
当配置文件中scope属性为singleton时:
- 测试代码
public class UserController {
public static void main(String[] args) {
//获取Spring上下文环境 (加载配置文件)
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
/*
通过getBean方法得到Spring容器中实例化好的Bean对象 (实例化Bean对象)
userService代表的是配置文件中bean标签的id属性值(id标识唯一的bean)
*/
UserService userService = (UserService) context.getBean("userService");
UserService userService1 = (UserService) context.getBean("userService");
System.out.println(userService);
System.out.println(userService1);
}
}

总结:当scope=“singleton”时,spring容器中有且仅有一个对象,init方法在创建容器时仅执行一次
当配置文件中scope属性为prototype时:
- 配置文件
<?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">
<!--演示scope的值为singleton是,单例创建对象-->
<!-- <bean id="userService" class="com.why.service.impl.UserServiceImpl" scope="singleton"></bean>-->
<!-- <bean id="userService" class="com.why.service.impl.UserServiceImpl" scope="singleton"></bean>-->
<bean id="userService" class="com.why.service.impl.UserServiceImpl"
scope="prototype"
init-method="init"
destroy-method="destroy"
/>
</beans>
- 测试代码
public class UserController {
public static void main(String[] args) {
//获取Spring上下文环境 (加载配置文件)
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
/*
通过getBean方法得到Spring容器中实例化好的Bean对象 (实例化Bean对象)
userService代表的是配置文件中bean标签的id属性值(id标识唯一的bean)
*/
UserService userService = (UserService) context.getBean("userService");
UserService userService1 = (UserService) context.getBean("userService");
System.out.println(userService);
System.out.println(userService1);
}
}
- 测试结果

总结:当scope=“prototype”时,spring容器要创建同一类型的多个对象,init方法在每个对象创建时均执行一次
思考:
为什么只有init方法运行,destory方法怎么没有运行?
其实destory是有运行的,但是由于程序结束的太快虚拟机以及关闭了,来不及打印,所以就没有显示了。
我们可以通过ClassPathXmlApplicationContext类的close()方法在程序结束之前强制关闭容器,这样就可以看到destroy方法的打印了。
- 修改配置文件(scope = singleton)
<?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="userService" class="com.why.service.impl.UserServiceImpl"
scope="singleton"
init-method="init"
destroy-method="destroy"
/>
</beans>
- 测试
public class UserController {
public static void main(String[] args) {
//获取Spring上下文环境 (加载配置文件)
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
/*
通过getBean方法得到Spring容器中实例化好的Bean对象 (实例化Bean对象)
userService代表的是配置文件中bean标签的id属性值(id标识唯一的bean)
*/
UserService userService = (UserService) context.getBean("userService");
UserService userService1 = (UserService) context.getBean("userService");
System.out.println(userService);
System.out.println(userService1);
context.close();
}
}
- 结果

当scope=“singleton”时,关闭容器会导致bean实例的销毁,调用destroy方法一次
- 修改配置文件(scope = prototype)
<?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="userService" class="com.why.service.impl.UserServiceImpl"
scope="prototype"
init-method="init"
destroy-method="destroy"
/>
</beans>
- 测试代码
public class UserController {
public static void main(String[] args) {
//获取Spring上下文环境 (加载配置文件)
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
/*
通过getBean方法得到Spring容器中实例化好的Bean对象 (实例化Bean对象)
userService代表的是配置文件中bean标签的id属性值(id标识唯一的bean)
*/
UserService userService = (UserService) context.getBean("userService");
UserService userService1 = (UserService) context.getBean("userService");
System.out.println(userService);
System.out.println(userService1);
context.close();
}
}
- 运行结果

当scope=“prototype”时,对象的销毁由垃圾回收机制gc()控制,destroy方法将不会被执行
4、 bean对象创建方式
(1) factory-bean
名称:factory-bean
类型:属性
归属:bean标签
作用:定义bean对象创建方式,使用静态工厂的形式创建bean,兼容早期遗留系统的升级工作
格式:
<bean class="FactoryClassName" factory-method="factoryMethodName"></bean>
取值:工厂bean中用于获取对象的静态方法名
注意事项:
class属性必须配置成静态工厂的类名
(2)factory-bean,factory-method
名称:factory-bean,factory-method
类型:属性
归属:bean标签
作用:定义bean对象创建方式,使用实例工厂的形式创建bean,兼容早期遗留系统的升级工作
格式:
<bean factory-bean="factoryBeanId" factory-method="factoryMethodName"></bean>
取值:工厂bean中用于获取对象的实例方法名
注意事项:
使用实例工厂创建bean首先需要将实例工厂配置bean,交由spring进行管理
factory-bean是实例工厂的beanId
4.1 配置静态工厂创建bean
- 创建工厂类(工厂方法为静态方法)
package com.why.factory;
/**
* @author Laity
* @date 2021年11月17日 9:45
*/
public class UserServiceFactory {
public static UserService getService(){
System.out.println("对象是静态工厂创建的");
//返回需要创建的对象
return new UserServiceImpl();
}
}
- 修改配置文件(spring.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="userService" class="com.why.factory.UserServiceFactory" factory-method="getService"/>
</beans>
- 测试
public class UserController {
public static void main(String[] args) {
//获取Spring上下文环境 (加载配置文件)
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
/*
通过getBean方法得到Spring容器中实例化好的Bean对象 (实例化Bean对象)
userService代表的是配置文件中bean标签的id属性值(id标识唯一的bean)
*/
UserService userService = (UserService) context.getBean("userService");
}
}
- 结果

4.2 实例工厂创建bean
- 创建工厂类(工厂方法非静态方法)
public class UserServiceFactory {
public UserService getService(){
System.out.println("对象是实例化工厂创建的");
//返回需要实例化的对象
return new UserServiceImpl();
}
}
- 修改配置文件(spring.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="factoryBean" class="com.why.factory.UserServiceFactory" />
<bean id="userService" factory-bean="factoryBean" factory-method="getService"/>
</beans>
- 测试
public class UserController {
public static void main(String[] args) {
//获取Spring上下文环境 (加载配置文件)
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
/*
通过getBean方法得到Spring容器中实例化好的Bean对象 (实例化Bean对象)
userService代表的是配置文件中bean标签的id属性值(id标识唯一的bean)
*/
UserService userService = (UserService) context.getBean("userService");
userService.saveUser();
}
}
- 结果

4.3 Spring三种创建bean的方式比较
方式一: bean标签创建,当各个bean的业务逻辑相互比较独立的时候或者和外 界关联较少的时候可以使用。
方式二: 利用静态factory方法创建,可以统一管理各个bean的创建,如各个bean在创建之前需要 相同的初始化处理,则可用这个factory方法险进行统一的处理等等。
方式三: 利用实例化factory方法创建,即将factory方法也作为了业务bean来控制,1可用于集成 其他框架的bean创建管理方法,2能够使bean和factory的角色互换。
开发中,项目一般使用第一种方式实例化bean,交给spring托管,使用时直接拿来使用即可,另外两种了解即可。
2、Spring的IOC标签介绍以及实例的更多相关文章
- 【死磕 Spring】----- IOC 之解析 bean 标签:开启解析进程
原文出自:http://cmsblogs.com import 标签解析完毕了,再看 Spring 中最复杂也是最重要的标签 bean 标签的解析过程. 在方法 parseDefaultElement ...
- Spring框架IOC和AOP介绍
说明:本文部分内容参考其他优秀博客后结合自己实战例子改编如下 Spring框架是个轻量级的Java EE框架.所谓轻量级,是指不依赖于容器就能运行的.Struts.Hibernate也是轻量级的. 轻 ...
- Spring中IoC的入门实例
Spring中IoC的入门实例 Spring的模块化是很强的,各个功能模块都是独立的,我们可以选择的使用.这一章先从Spring的IoC开始.所谓IoC就是一个用XML来定义生成对象的模式,我们看看如 ...
- 死磕Spring之IoC篇 - 解析自定义标签(XML 文件)
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- 【死磕 Spring】—– IOC 之解析Bean:解析 import 标签
原文出自:http://cmsblogs.com 在博客[死磕Spring]----- IOC 之 注册 BeanDefinition中分析到,Spring 中有两种解析 Bean 的方式.如果根节点 ...
- SpringMVC系列(十五)Spring MVC与Spring整合时实例被创建两次的解决方案以及Spring 的 IOC 容器和 SpringMVC 的 IOC 容器的关系
一.Spring MVC与Spring整合时实例被创建两次的解决方案 1.问题产生的原因 Spring MVC的配置文件和Spring的配置文件里面都使用了扫描注解<context:compon ...
- Spring IoC控制反转创建实例
Spring IoC控制反转创建实例写一个配置文件beans.xml,配置文件的约束可以访问:完整链接:https://repo.spring.io/libs-release-local/org/sp ...
- Spring之IoC详解(非原创)
文章大纲 一.Spring介绍二.Spring的IoC实战三.IoC常见注解总结四.项目源码及参考资料下载五.参考文章 一.Spring介绍 1. 什么是Spring Spring是分层的Java ...
- Spring之IOC原理及代码详解
一.什么是IOC 引用 Spring 官方原文:This chapter covers the Spring Framework implementation of the Inversion of ...
随机推荐
- 使用json.net实现复杂对象转换为QueryString
目标:生成复杂对象的QueryString,比如 new { Field1 = 1, Field2 = new { Field3 = "2", Field4 = new[] { n ...
- [atAGC052D]Equal LIS
令$f_{i}$表示以$i$为结尾的最长上升子序列,显然可以快速预处理 令$L=\max_{i=1}^{n}f_{i}$,当$L$为偶数,考虑如下构造-- 将所有$f_{i}\le \frac{L}{ ...
- opencv 视频处理相关
包含视频格式知识(编解码和封装格式):如何获取视频信息及视频编解码格式:opencv读取及保存视频,及opencv fourcc编码格式 一.基础知识 视频的编解码格式和封装格式 参考如山似水 视频编 ...
- 未能加载文件或程序集“Microsoft.CodeDom.Providers.DotNetCompilerPlatform
"/"应用程序中的服务器错误. 未能加载文件或程序集"Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Versio ...
- bzoj4036 / P3175 [HAOI2015]按位或
bzoj4036 / P3175 [HAOI2015]按位或 是一个 min-max容斥 的板子题. min-max容斥 式子: $ \displaystyle max(S) = \sum_{T\su ...
- git冲突
常规操作: # quan @ quandeMacBook-Pro in ~/Desktop/10-01/app_addressing----quan on git:1.0.1.Release o [1 ...
- 除了GO基因本体论,还有PO、TO、CO等各种Ontology?
目录 PO/TO CO 后记 我们最常用最熟悉的功能数据库之一:GO(gene onotology),基因本体论.其实是一套标准词汇术语,目的是从不同角度来描述某个基因的特点和功能,三大本体如生物学进 ...
- R 语言实战-Part 4 笔记
R 语言实战(第二版) part 4 高级方法 -------------第13章 广义线性模型------------------ #前面分析了线性模型中的回归和方差分析,前提都是假设因变量服从正态 ...
- [R]在dplyr函数的基础上编写函数-(3)tidyeval
dplyr的优点很明显,数据框操作简洁,如filter(df, x == 1, y == 2, z == 3)等于df[df$x == 1 & df$y ==2 & df$z == 3 ...
- 【3】蛋白鉴定软件之Mascot
目录 1.简介 2.配置 2.1在线版本 2.2 服务器版本 3.运行 3.1 在线版本 3.2 服务器版本 4.结果 1.简介 Mascot是非常经典的蛋白鉴定软件,被Frost & Sul ...