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 配置DRUID数据源的方法实例分析

    springboot 配置DRUID数据源的方法实例分析

    这篇文章主要介绍了springboot 配置DRUID数据源的方法,结合实例形式分析了springboot 配置阿里DRUID数据源的具体步骤与相关操作技巧,需要的朋友可以参考下
    2019-12-12
  • Jersey实现Restful服务(实例讲解)

    Jersey实现Restful服务(实例讲解)

    下面小编就为大家带来一篇Jersey实现Restful服务(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • 使用SpringMVC在redirect重定向的时候携带参数的问题

    使用SpringMVC在redirect重定向的时候携带参数的问题

    这篇文章主要介绍了使用SpringMVC在redirect重定向的时候携带参数的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • fastJson泛型如何转换的实现

    fastJson泛型如何转换的实现

    这篇文章主要介绍了fastJson泛型如何转换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • 如何通过java实现highcharts导出图片至excel

    如何通过java实现highcharts导出图片至excel

    这篇文章主要介绍了如何通过java实现highcharts导出图片至excel。文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,下面我们就来一起学习一下吧
    2019-06-06
  • java 利用反射机制,获取实体所有属性和方法,并对属性赋值

    java 利用反射机制,获取实体所有属性和方法,并对属性赋值

    这篇文章主要介绍了 java 利用反射机制,获取实体所有属性和方法,并对属性赋值的相关资料,需要的朋友可以参考下
    2017-01-01
  • 详解Java程序读取properties配置文件的方法

    详解Java程序读取properties配置文件的方法

    这篇文章主要介绍了Java读取properties配置文件的方法讲解,properties可以被看作是Java世界的ini,Java中有Properties可以操作它,需要的朋友可以参考下
    2016-04-04
  • Springboot整合junit过程解析

    Springboot整合junit过程解析

    这篇文章主要介绍了Springboot整合junit过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • SpringBoot引入模板引擎实现视图解析

    SpringBoot引入模板引擎实现视图解析

    这篇文章主要介绍了SpringBoot引入模板引擎实现视图解析方法流程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-10-10
  • 关于SpringBoot3.x中spring.factories功能被移除的解决方案

    关于SpringBoot3.x中spring.factories功能被移除的解决方案

    这篇文章主要介绍了SpringBoot3.x中spring.factories功能被移除的解决方案,在配置好相关依赖、最小启动类和配置之后,发现项目无法启动,于是根据启动上下文日志和按行DEBUG找到原因并且在等待组件升级兼容之前进行临时性解决,需要的朋友可以参考下
    2022-12-12

最新评论