Spring容器初始化及问题解决方案
作者:TracyDemo 发布时间:2023-09-14 08:19:32
标签: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属性,可以通过初始化完成
来源:https://www.cnblogs.com/tracyDemo/p/13127552.html


猜你喜欢
- 通常情况下,Android实现自定义控件无非三种方式。Ⅰ、继承现有控件,对其控件的功能进行拓展。Ⅱ、将现有控件进行组合,实现功能更加强大控件
- 用Stopwatch分段监控了一下,发现耗时最多的函数是SaveToExcel此函数中遍列所有数据行,通过Replace替换标签生成Exce
- Android自定义实现图片加文字功能分四步来写: 1,组合控件的xml; 2,自定义组合控件的属性; 3,自定义继承组合布局的class类
- 1、加载节点SpringBoot启动时,会执行这个方法:SpringApplication#run,这个方法中会调prepareContex
- 之前写了一个WPF的圆形环绕的Loading动画,现在写一个Winform的圆形环绕的Loading动画。1.新建Winform项目,添加一
- 单例类保证一个类全局仅有一个实例,并提供一个全局访问点,由于只能生成一个实例,因此我们必须把构造函数设为私有函数以禁止他人创建实例。实现1:
- 先上结论RPC请求的效率是HTTP请求的1.6倍左右,性能明显比HTTP请求要高很多。原因分析RESTful是基于HTTP协议进行交互的,H
- ProgressDialog(精度条对话框):1.直接调用ProgressDialog提供的静态方法show()显示2.创建Progress
- 本文实例讲述了C#实现简单屏幕监控的方法。分享给大家供大家参考。具体如下:这是一段C#编写的屏幕监控代码,可以自动对屏幕进行截图,软件自身隐
- Java是一门面向对象的语言,即一切皆是对象!那么为何数据类型中还分为:基本类型和对象?Java中有8种基本数据类型boolean、byte
- 本文介绍了ListView给每个Item上面的按钮添加事件,具体如下:1.先看下效果图:在这里仅供测试,我把数据都写死了,根据需要可以自己进
- 思路:首先进入登录界面,输入账号和密码后登陆到主界面,在主界面通过点击按钮发送一条强制下线的广播,广播接收者收到广播后重新进入登陆界面。新建
- 在之前的博客使用SpringMVC创建Web工程并使用SpringSecurity进行权限控制的详细配置方法 中,我们描述了如何配置一个基于
- 本文实例讲述了Spring实战之Bean的作用域singleton和prototype用法。分享给大家供大家参考,具体如下:一 配置<
- boolean isGBK(String s) throws UnsupportedEncodingException { if(s.equ
- 本文实例讲述了C#中委托用法。分享给大家供大家参考。具体分析如下:这里演示了如何使用匿名委托来计算员工的薪水奖金。使用匿名委托简化了程序,因
- 微信登录的实现与qq登录类似。不过微信登录比较麻烦,需要拿到开发者资质认证,花300块钱,然后应用的话还得有官网之类的,就是比较繁琐的前期准
- 问题在讨论原子性操作时,我们经常会听到一个说法:任意单个volatile变量的读写具有原子性,但是volatile++这种操作除外。所以问题
- 本文实例为大家分享了Android RxJava创建操作符Timer的具体代码,供大家参考,具体内容如下之前有写过Android实现倒计时之
- 目录第一点——加载网页结构第二点——解析网页结构,转变为符合需求的数据实体第三点——保存数据实体,转变为符合需求的数据实体第四点——main