美文网首页
Spring详解(二) 简单配置bean

Spring详解(二) 简单配置bean

作者: 秀逼 | 来源:发表于2017-11-08 23:28 被阅读0次

配置applicationContext.xml为单例模式,多例模式,以及延迟初始化等

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd">

        <bean id="user" class="com.iotek.first.User" scope="singleton" lazy-init="false" init-method="init_user" destroy-method="destroy_user"></bean>

</beans>

同时配置了单例或多例模式,延迟初始化,初始化与销毁对应的方法,同时,需要明白IOC容器初始化与对象初始化的先后顺序。
单例模式下先初始化对象,再初始话容器,而在多例模式下,先初始化容器,等需要用到对象的时候,才会初始化对象。当然,在单例设计模式中,我们可以更改lazy-init为true,使得在单例模式下,对象的初始化也放在用到对象的时候再来进行。

相关文章

网友评论

      本文标题:Spring详解(二) 简单配置bean

      本文链接:https://www.haomeiwen.com/subject/xepumxtx.html