码迷,mamicode.com
首页 > 编程语言 > 详细

Spring容器初始化方法的应用及注意对象

时间:2020-06-14 23:19:39      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:context   构造   cache   方法   abs   模式   print   oca   ring   

beans组件 里面default-init-method初始化方法加载,范围比较大,当没有此方法时不会报错,default-destroy-method销毁方法

bean组件里面init-method初始化无此方法,会报错,  destroy-method销毁方法,

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

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

 

            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">

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

测试

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

Spring容器初始化方法的应用及注意对象

标签:context   构造   cache   方法   abs   模式   print   oca   ring   

原文地址:https://www.cnblogs.com/tracyDemo/p/13127552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!