Spring容器初始化及问题解决方案

 更新时间:2020年06月17日 08:28:16   作者:TracyDemo  
这篇文章主要介绍了Spring容器初始化及问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.Spring bean组件 ”默认为单例模式scope=“singleton, 运行JavaApplication容器启动时自动创建对象

scope=“prototype”为多例模式,请求条件下才创建对象

2beans组件 里面default-init-method初始化方法加载,范围比较大,当没有此方法时不会报错,default-destroy-method销毁方法,default-lazy-init=“true/false” 对象延时实例化

3.bean组件里面init-method初始化无此方法,会报错, destroy-method销毁方法,lazy-init=“true/false” 延时实例化

注意:

1.销毁方法对scope=“prototype”多例模式无效

2.AbstractApplicationContext可以关闭容器,可以调用close()方法,关闭容器,调用销毁方法

3.对象延时实例化对多例模式没有意义。

XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xmlns:cache="http://www.springframework.org/schema/cache"
 xsi:schemaLocation=" 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context.xsd 
 http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans.xsd 
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx.xsd 
 http://www.springframework.org/schema/jdbc 
 http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 
 http://www.springframework.org/schema/cache 
 http://www.springframework.org/schema/cache/spring-cache-3.1.xsd 
 http://www.springframework.org/schema/aop 
 http://www.springframework.org/schema/aop/spring-aop.xsd 
 http://www.springframework.org/schema/util 
 http://www.springframework.org/schema/util/spring-util.xsd"
 default-init-method="initEmp" default-destroy-method="destroyEmp" default-lazy-init="true"> 

<bean id="emp" init-method="initEmp" destroy-method="destroyEmp" lazy-init="false" 
class="com.tracy.bean.Emp" scope="singleton" ></bean>
 </beans>

Test方法

//bean组件的默认方式
  @Test
  public void beanInitTest() {
  AbstractApplicationContext appContext=new ClassPathXmlApplicationContext("com/tracy/xml/bean-init-destroy.xml");
    ApplicationContext app=new ClassPathXmlApplicationContext("com/tracy/xml/bean-init-destroy.xml");
    Emp emp=app.getBean("emp",Emp.class);
    System.out.print(emp);
    appContext.close();
  }

通过构造完成初始bean属性,可以通过初始化完成

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot中Elasticsearch的连接配置原理与使用详解

    SpringBoot中Elasticsearch的连接配置原理与使用详解

    Elasticsearch是一种开源的分布式搜索和数据分析引擎,它可用于全文搜索、结构化搜索、分析等应用场景,本文主要介绍了SpringBoot中Elasticsearch的连接配置原理与使用详解,感兴趣的可以了解一下
    2023-09-09
  • 基于MybatisPlus插件TenantLineInnerInterceptor实现多租户功能

    基于MybatisPlus插件TenantLineInnerInterceptor实现多租户功能

    这篇文章主要介绍了基于MybatisPlus插件TenantLineInnerInterceptor实现多租户功能,需要的朋友可以参考下
    2021-11-11
  • SpringBoot解决mysql连接8小时问题

    SpringBoot解决mysql连接8小时问题

    服务连接mysql数据库,8小时没有数据库的操作时候,数据库会主动断开连接释放资源,本文就详细的介绍一下解决方法,感兴趣的可以了解一下
    2023-08-08
  • java如何生成可变表头的excel

    java如何生成可变表头的excel

    这篇文章主要为大家详细介绍了java生成可变表头excel的方法,传入一个表头和数据,将数据导入到excel中,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Java Eclipse中实现快速替换变量

    Java Eclipse中实现快速替换变量

    这篇文章主要介绍了Java Eclipse中实现快速替换变量,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • 详谈jvm线程栈空间内存分配位置

    详谈jvm线程栈空间内存分配位置

    这篇文章主要介绍了jvm线程栈空间内存分配位置,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • springboot断言异常封装与统一异常处理实现代码

    springboot断言异常封装与统一异常处理实现代码

    异常处理其实一直都是项目开发中的大头,但关注异常处理的人一直都特别少,下面这篇文章主要给大家介绍了关于springboot断言异常封装与统一异常处理的相关资料,需要的朋友可以参考下
    2023-01-01
  • Elasticsearch模糊查询详细介绍

    Elasticsearch模糊查询详细介绍

    这篇文章主要给大家介绍了关于Elasticsearch模糊查询的相关资料,在数据库查询中模糊查询是一种强大的技术,可以用来搜索与指定模式匹配的数据,需要的朋友可以参考下
    2023-09-09
  • SpringBoot配置的加载流程详细分析

    SpringBoot配置的加载流程详细分析

    了解内部原理是为了帮助我们做扩展,同时也是验证了一个人的学习能力,如果你想让自己的职业道路更上一层楼,这些底层的东西你是必须要会的,这篇文章主要介绍了SpringBoot配置的加载流程
    2023-01-01
  • Mybatis/Mybatis-Plus驼峰式命名映射的实现

    Mybatis/Mybatis-Plus驼峰式命名映射的实现

    本文主要介绍了Mybatis-Plus驼峰式命名映射的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07

最新评论