配置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>
同時(shí)配置了單例或多例模式嗅定,延遲初始化,初始化與銷毀對(duì)應(yīng)的方法用踩,同時(shí)渠退,需要明白IOC容器初始化與對(duì)象初始化的先后順序。
單例模式下先初始化對(duì)象脐彩,再初始話容器碎乃,而在多例模式下,先初始化容器惠奸,等需要用到對(duì)象的時(shí)候梅誓,才會(huì)初始化對(duì)象。當(dāng)然佛南,在單例設(shè)計(jì)模式中证九,我們可以更改lazy-init為true,使得在單例模式下共虑,對(duì)象的初始化也放在用到對(duì)象的時(shí)候再來進(jìn)行愧怜。