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 ...
随机推荐
- scrapy的安装,scrapy创建项目
简要: scrapy的安装 # 1)pip install scrapy -i https://pypi.douban.com/simple(国内源) 一步到位 # 2) 报错1: building ...
- logstash插件配置-codec插件说明json和multiline
编码插件(Codec) Codec 是 logstash 从 1.3.0 版开始新引入的概念(Codec 来自 Coder/decoder 两个单词的首字母缩写). 在此之前,logstash 只支持 ...
- jdbc pool java连接池技术
1 ConnectPool .java: 2 3 package pool; 4 5 /** 6 * Title: ConnectPool.Java 7 * Description: 连接池治理器 8 ...
- [bzoj1109]堆积木
用f[i]表示前i个数,i必须被贡献的答案,考虑转移,枚举下一个被贡献的数j,那么j需要满足:1.$j<i$:2.$a[j]<a[i]$:3.$a[i]-(i-j+1)\le a[j]$, ...
- 第03章_基本的SELECT语句
第03章_基本的SELECT语句 1. SQL概述 1.1 SQL背景知识 1946 年,世界上第一台电脑诞生,如今,借由这台电脑发展起来的互联网已经自成江湖.在这几十年里,无数的技术.产业在这片江湖 ...
- 第04章_MySQL运算符详解
第04章_运算符 1. 算术运算符 算术运算符主要用于数学运算,其可以连接运算符前后的两个数值或表达式,对数值或表达式进行加(+).减(-).乘(*).除(/)和取模(%)运算. 1.加法与减法运算符 ...
- 洛谷 P7620 - CF1431J Zero-XOR Array(状压 dp)
洛谷题面传送门 首先显然题目等价于求有多少个长度 \(n-1\) 的序列 \(b\) 满足 \(a_i\le b_i\le a_{i+1}\),满足 \(b_1\oplus b_2\oplus\cdo ...
- R之dplyr::select/mutate函数扩展
select函数 dplyr包select函数用的很多,不过我们一般也是通过正反选列名或数字来选择列. 常见用法如: select(iris,c(1,3)) select(iris,1,3) #同上 ...
- 【R】爬虫案例
爬取豆瓣相册 library(RCurl) library(XML) myHttpheader <- c("User-Agent"="Mozilla/5.0 (Wi ...
- Nginx在make时报错[objs/Makefile:469: objs/src/core/ngx_murmurhash.o] Error
Nginx在make时报错[objs/Makefile:469: objs/src/core/ngx_murmurhash.o] Error 在安装目录下执行 vim obj/Markfile 把 ...