日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

004_Bean标签

發(fā)布時(shí)間:2025/4/17 编程问答 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 004_Bean标签 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.?<bean>標(biāo)簽的id和name的配置

1.1. id:?使用了約束中的唯一約束。里面不能出現(xiàn)特殊字符的。

1.2. name:?沒有使用約束中的唯一約束(理論上可以出現(xiàn)重復(fù)的,但是實(shí)際開發(fā)不能出現(xiàn)的)。里面可以出現(xiàn)特殊字符。

2. Bean的生命周期的配置

2.1. init-method:?Bean被初始化的時(shí)候執(zhí)行的方法。

2.2. destroy-method:?Bean被銷毀的時(shí)候執(zhí)行的方法(Bean是單例創(chuàng)建, 工廠關(guān)閉)。

3. Bean的作用范圍scope的配置

3.1. singleton: 默認(rèn)的作用范圍, Spring會(huì)采用單例模式創(chuàng)建這個(gè)對象。

3.2. prototype: 多例模式。

3.3. request: 應(yīng)用在web項(xiàng)目中, Spring創(chuàng)建這個(gè)類的對象以后, 將這個(gè)對象存入到request范圍中。

3.4. session: 應(yīng)用在web項(xiàng)目中, Spring創(chuàng)建這個(gè)類的對象以后, 將這個(gè)對象存入到session范圍中。

3.5. globalsession: 應(yīng)用在web項(xiàng)目中, 必須在porlet環(huán)境下使用。但是如果沒有這種環(huán)境, 相當(dāng)于session。

4. Bean標(biāo)簽例子

4.1. 新建一個(gè)名為SpringBeanTag的Java項(xiàng)目, 拷入Spring相關(guān)包

4.2. 新建UserDaoImpl.java

package com.lywgames.dao.impl;public class UserDaoImpl {public UserDaoImpl() {System.out.println("UserDaoImpl實(shí)例化構(gòu)造函數(shù)。");}public void init() {System.out.println("UserDaoImpl初始化了。");}public void login() {System.out.println("用戶登錄成功。");}public void destroy() {System.out.println("UserDaoImpl銷毀了。");} }

4.3. 新建UserAddressDaoImpl.java

package com.lywgames.dao.impl;public class UserAddressDaoImpl {public UserAddressDaoImpl() {System.out.println("UserAddressDaoImpl實(shí)例化構(gòu)造函數(shù)。");}public void init() {System.out.println("UserAddressDaoImpl初始化了。");}public void add() {System.out.println("添加用戶收獲地址成功。");}public void destroy() {System.out.println("UserAddressDaoImpl銷毀了。");} }

4.4. 新建Test.java

package com.lywgames.beantag;import java.util.Timer; import java.util.TimerTask; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lywgames.dao.impl.UserAddressDaoImpl; import com.lywgames.dao.impl.UserDaoImpl;public class Test {public static void main(String[] args) {// 類路徑加載配置文件ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Timer timer = new Timer();// 設(shè)定指定的時(shí)間time, 此處為2000毫秒timer.schedule(new TimerTask() {public void run() {System.out.println("----------2秒后----------");UserDaoImpl userDao1 = (UserDaoImpl) context.getBean("userDao");UserDaoImpl userDao2 = (UserDaoImpl) context.getBean("userDao");userDao1.login();userDao2.login();}}, 2000);// 設(shè)定指定的時(shí)間time, 此處為5000毫秒timer.schedule(new TimerTask() {public void run() {System.out.println("----------3秒后----------");UserAddressDaoImpl userAddressDao1 = (UserAddressDaoImpl) context.getBean("userAddressDao");UserAddressDaoImpl userAddressDao2 = (UserAddressDaoImpl) context.getBean("userAddressDao");userAddressDao1.add();userAddressDao2.add();}}, 5000);// 設(shè)定指定的時(shí)間time, 此處為10000毫秒timer.schedule(new TimerTask() {public void run() {System.out.println("----------5秒后----------");context.close();System.exit(-1);}}, 10000);while (true) {}} }

4.5. 在src目錄下創(chuàng)建applicationContext.xml

4.6. 運(yùn)行項(xiàng)目, 一加載applicationContext.xml文件, 就創(chuàng)建了和初始化了UserDaoImpl這個(gè)單例模式的類實(shí)例。2秒后, 我們獲取了2次id為userDao這個(gè)Bean, 并沒有再次給我們創(chuàng)建實(shí)例, 說明單例模式類, 在加載applicationContext.xml文件時(shí)就會(huì)創(chuàng)建實(shí)例, 之后獲取的都是同一實(shí)例。再過3秒, 我們獲取了2次id為userAddressDao這個(gè)Bean, 給我們創(chuàng)建了2實(shí)例, 并調(diào)用了初始化接口, 說明多例模式的類, 每獲取一次Bean, 就會(huì)創(chuàng)建一個(gè)實(shí)例。再過5秒, 我們銷毀了應(yīng)用程序上下文, 只有單例模式的類調(diào)用了銷毀方法。

總結(jié)

以上是生活随笔為你收集整理的004_Bean标签的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。