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

网友投稿 505 2023-05-24

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

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

xmlns:tx="http://springframework.org/schema/tx" xmlns:p="http://springframework.org/schema/p" xmlns:util="http://springframework.org/schema/util" xhttp://mlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/jdbc

http://springframework.org/schema/jdbc/spring-jdbc-3.1.xsd

http://springframework.org/schema/cache

http://springframework.org/schema/cache/spring-cache-3.1.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util.xsd"

default-init-method="initEmp" default-destroy-method="destroyEmp" default-lazy-init="true">

class="com.tracy.bean.Emp" scope="singleton" >

xmlns:tx="http://springframework.org/schema/tx" xmlns:p="http://springframework.org/schema/p" xmlns:util="http://springframework.org/schema/util" xhttp://mlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:cache="http://springframework.org/schema/cache"

xsi:schemaLocation="

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/jdbc

http://springframework.org/schema/jdbc/spring-jdbc-3.1.xsd

http://springframework.org/schema/cache

http://springframework.org/schema/cache/spring-cache-3.1.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util.xsd"

default-init-method="initEmp" default-destroy-method="destroyEmp" default-lazy-init="true">

class="com.tracy.bean.Emp" scope="singleton" >

class="com.tracy.bean.Emp" scope="singleton" >

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属性,可以通过初始化完成

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot使用Jwt处理跨域认证问题的教程详解
下一篇:SpringBoot整合Hibernate Validator实现参数验证功能
相关文章

 发表评论

暂时没有评论,来抢沙发吧~