Spring详解(二) 简单配置bean

配置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,使得在单例模式下,对象的初始化也放在用到对象的时候再来进行。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容