一、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方法的打印了。

  1. 修改配置文件(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>
  1. 测试
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();
}
}
  1. 结果

当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

  1. 创建工厂类(工厂方法为静态方法)
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();
}
}
  1. 修改配置文件(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>
  1. 测试
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");
}
}
  1. 结果

4.2 实例工厂创建bean

  1. 创建工厂类(工厂方法非静态方法)
public class UserServiceFactory {

    public UserService getService(){

        System.out.println("对象是实例化工厂创建的");

        //返回需要实例化的对象
return new UserServiceImpl();
}
}
  1. 修改配置文件(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>
  1. 测试
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();
}
}
  1. 结果

4.3 Spring三种创建bean的方式比较

方式一: bean标签创建,当各个bean的业务逻辑相互比较独立的时候或者和外 界关联较少的时候可以使用。

方式二: 利用静态factory方法创建,可以统一管理各个bean的创建,如各个bean在创建之前需要 相同的初始化处理,则可用这个factory方法险进行统一的处理等等。

方式三: 利用实例化factory方法创建,即将factory方法也作为了业务bean来控制,1可用于集成 其他框架的bean创建管理方法,2能够使bean和factory的角色互换。

开发中,项目一般使用第一种方式实例化bean,交给spring托管,使用时直接拿来使用即可,另外两种了解即可。

2、Spring的IOC标签介绍以及实例的更多相关文章

  1. 【死磕 Spring】----- IOC 之解析 bean 标签:开启解析进程

    原文出自:http://cmsblogs.com import 标签解析完毕了,再看 Spring 中最复杂也是最重要的标签 bean 标签的解析过程. 在方法 parseDefaultElement ...

  2. Spring框架IOC和AOP介绍

    说明:本文部分内容参考其他优秀博客后结合自己实战例子改编如下 Spring框架是个轻量级的Java EE框架.所谓轻量级,是指不依赖于容器就能运行的.Struts.Hibernate也是轻量级的. 轻 ...

  3. Spring中IoC的入门实例

    Spring中IoC的入门实例 Spring的模块化是很强的,各个功能模块都是独立的,我们可以选择的使用.这一章先从Spring的IoC开始.所谓IoC就是一个用XML来定义生成对象的模式,我们看看如 ...

  4. 死磕Spring之IoC篇 - 解析自定义标签(XML 文件)

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...

  5. 【死磕 Spring】—– IOC 之解析Bean:解析 import 标签

    原文出自:http://cmsblogs.com 在博客[死磕Spring]----- IOC 之 注册 BeanDefinition中分析到,Spring 中有两种解析 Bean 的方式.如果根节点 ...

  6. SpringMVC系列(十五)Spring MVC与Spring整合时实例被创建两次的解决方案以及Spring 的 IOC 容器和 SpringMVC 的 IOC 容器的关系

    一.Spring MVC与Spring整合时实例被创建两次的解决方案 1.问题产生的原因 Spring MVC的配置文件和Spring的配置文件里面都使用了扫描注解<context:compon ...

  7. Spring IoC控制反转创建实例

    Spring IoC控制反转创建实例写一个配置文件beans.xml,配置文件的约束可以访问:完整链接:https://repo.spring.io/libs-release-local/org/sp ...

  8. Spring之IoC详解(非原创)

    文章大纲 一.Spring介绍二.Spring的IoC实战三.IoC常见注解总结四.项目源码及参考资料下载五.参考文章 一.Spring介绍 1. 什么是Spring   Spring是分层的Java ...

  9. Spring之IOC原理及代码详解

    一.什么是IOC 引用 Spring 官方原文:This chapter covers the Spring Framework implementation of the Inversion of ...

随机推荐

  1. 来了!公开揭密团队成员开发鸿蒙 OpenHarmony 的完整过程(收获官方7000奖金和开发板等,1w字用心总结)

    背景 随着 OpenHarmony 组件开发大赛结果公布,我们的团队成员被告知获得了二等奖,在开心之余也想将我们这段时间宝贵的开发经验写下来与大家分享,当我们看到参赛通知的时候已经是 9 月中旬的时候 ...

  2. 国内首家!腾讯云正式成为 FinOps 基金会顶级会员

    11月24日,腾讯云正式宣布加入FinOps基金会,作为国内首家FinOps基金会顶级会员,腾讯云将联合FinOps基金会,全面推进对FinOps标准和最佳实践的贡献,为企业提供云财务管理的最佳解决方 ...

  3. namp相关命令大全

    常用功能: -探测主机存活- 扫描端口- 探测主机操作系统信息- 检测漏洞 nmap 常用的几个参数 nmap -v ip 显示详细的扫描过程 nmap -p  ip  扫描指定端口 nmap -A ...

  4. Codeforces 671C - Ultimate Weirdness of an Array(线段树维护+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 *2800 的 DS,不过还是被我自己想出来了 u1s1 这个 D1C 比某些 D1D 不知道难到什么地方去了 首先碰到这类问题我们肯定考 ...

  5. Vue 中 $on $once $off $emit 详细分析,以及使用

    vue的 $on,$emit,$off,$once Api 中的解释: $on(eventName:string|Array, callback) 监听事件 监听当前实例上的自定义事件.事件可以由 v ...

  6. doxygen相关命令

    主要配置修改 整个程序配置分几个部分 Project related configuration options 项目相关,包括: 项目名 输出目录 输出语言 是否显示继承属性 是否对C.Java.F ...

  7. 7本Python必读的入门书籍,你看过吗?(附福利)

    Python入门书籍不用看太多,看一本就够.重要的是你要学习Python的哪个方向,或者说你对什么方向感兴趣,因为Python这门语言的应用领域比较广泛,比如说可以用来做数据分析.机器学习,也可以用来 ...

  8. 解决CentOS7 docker容器映射端口只监听ipv6的问题

    问题现象 docker容器起来以后,查看9100端口监听情况,如下图: $ ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:P ...

  9. 学习java的第十天

    一.今日收获 1.java完全学习手册第二章2.9程序流程控制中的选择结构与顺序结构的例题 2.观看哔哩哔哩上的教学视频 二.今日问题 1.例题的问题不大,需要注意大小写,新的语句记忆不牢 2.哔哩哔 ...

  10. [C++] vptr, where are you?

    Search(c++在线运行). 有的网站很慢--不是下面的程序有问题. #include <string.h> #include <stdio.h> #include < ...