亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Spring學習筆記(3)----編碼剖析Spring管理Bea

系統 2235 0
Xml代碼 復制代碼
  1. <? xml ? version = "1.0" ? encoding = "UTF-8" ?> ??
  2. < beans ? xmlns = "http://www.springframework.org/schema/beans" ??
  3. ???????? xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" ??
  4. ???????? xmlns:context = "http://www.springframework.org/schema/context" ??
  5. ???????? xmlns:tx = "http://www.springframework.org/schema/tx" ??
  6. ???????? xsi:schemaLocation ="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ??
  7. ????????????????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-2.5.xsd ??
  8. ????????????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" > ??
  9. ???????? < bean ? id = "userBean" ? class = "com.szy.spring.implbean.UserBean" ? /> ??
  10. </ beans > ??
    <?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"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
		<bean id="userBean" class="com.szy.spring.implbean.UserBean" />
</beans>

  

?

Spring的配置文件中記錄了類的包路徑,因此我們首先是要讀入配置文件。在配置文件中Bean有id和class兩個屬性,我們首先定義一個Bean類,包含這兩個屬性:

?

Java代碼 復制代碼
  1. package ?com.szy.spring.implbean; ??
  2. ??
  3. public ? class ?Bean ??
  4. { ??
  5. ???? private ?String?id; ??
  6. ???? private ?String?className; ??
  7. ???? public ?String?getId() ??
  8. ????{ ??
  9. ???????? return ?id; ??
  10. ????} ??
  11. ???? public ? void ?setId(String?id) ??
  12. ????{ ??
  13. ???????? this .id?=?id; ??
  14. ????} ??
  15. ???? public ?String?getClassName() ??
  16. ????{ ??
  17. ???????? return ?className; ??
  18. ????} ??
  19. ???? public ? void ?setClassName(String?className) ??
  20. ????{ ??
  21. ???????? this .className?=?className; ??
  22. ????} ??
  23. ???? ??
  24. } ??
  25. ???
    package com.szy.spring.implbean;

public class Bean
{
	private String id;
	private String className;
	public String getId()
	{
		return id;
	}
	public void setId(String id)
	{
		this.id = id;
	}
	public String getClassName()
	{
		return className;
	}
	public void setClassName(String className)
	{
		this.className = className;
	}
	
}
?
  

由于配置文件是xml文件,在這里使用Jdom包操作xml文件,讀入配置文件中的Bean信息。

Java代碼 復制代碼
  1. /** ?
  2. ?????*?讀取xml配置文件 ?
  3. ?????*?@param?fileName?配置文件名 ?
  4. ?????*/ ??
  5. ???? private ? void ?readXML(String?fileName) ??
  6. ????{ ??
  7. ???????? //?尋找配置文件 ??
  8. ????????URL?xmlPath?=? this .getClass().getClassLoader().getResource(fileName); ??
  9. ????????Document?doc?=? null ; ??
  10. ????????Element?root?=? null ; ??
  11. ???????? try ??
  12. ????????{ ??
  13. ????????????SAXBuilder?sb?=? new ?SAXBuilder( false ); ??
  14. ????????????doc?=?sb.build( new ?FileInputStream( new ?File(xmlPath.toURI()))); ??
  15. ???????????? //?設置命名空間 ??
  16. ????????????Namespace?xhtml?=?Namespace.getNamespace( "xhtml" , ??
  17. ???????????????????? "http://www.springframework.org/schema/beans" ); ??
  18. ????????????root?=?doc.getRootElement();? //?獲取根元素 ??
  19. ????????????List<Element>?list?=?root.getChildren( "bean" ,?xhtml);? //獲取全部bean節點 ??
  20. ???????????? for ?(Element?element?:?list) //?遍歷節點,取得每個節點的屬性 ??
  21. ????????????{ ??
  22. ????????????????String?id?=?element.getAttributeValue( "id" ); ??
  23. ????????????????String?className?=?element.getAttributeValue( "class" ); ??
  24. ????????????????Bean?bean?=? new ?Bean(); ??
  25. ????????????????bean.setId(id); ??
  26. ????????????????bean.setClassName(className); ??
  27. ????????????????beanList.add(bean); ??
  28. ????????????} ??
  29. ????????}? catch ?(Exception?e) ??
  30. ????????{ ??
  31. ????????????e.printStackTrace(); ??
  32. ????????} ??
  33. ??
  34. ????}??
    /**
	 * 讀取xml配置文件
	 * @param fileName 配置文件名
	 */
	private void readXML(String fileName)
	{
		// 尋找配置文件
		URL xmlPath = this.getClass().getClassLoader().getResource(fileName);
		Document doc = null;
		Element root = null;
		try
		{
			SAXBuilder sb = new SAXBuilder(false);
			doc = sb.build(new FileInputStream(new File(xmlPath.toURI())));
			// 設置命名空間
			Namespace xhtml = Namespace.getNamespace("xhtml",
					"http://www.springframework.org/schema/beans");
			root = doc.getRootElement(); // 獲取根元素
			List<Element> list = root.getChildren("bean", xhtml); //獲取全部bean節點
			for (Element element : list)// 遍歷節點,取得每個節點的屬性
			{
				String id = element.getAttributeValue("id");
				String className = element.getAttributeValue("class");
				Bean bean = new Bean();
				bean.setId(id);
				bean.setClassName(className);
				beanList.add(bean);
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}

	}
  

?

?其中beanList是一個List對象,因為在配置文件中存在很多Bean。

?

得到了所有的Bean對象后,下面就實例化每個Bean對象,結果存放在Map對象中。

?

Java代碼 復制代碼
  1. /** ?
  2. ?????*?bean的實例化 ?
  3. ?????*/ ??
  4. ???? private ? void ?instanceBeans() ??
  5. ????{ ??
  6. ???????? for ?(Bean?bean?:?beanList) ??
  7. ????????{ ??
  8. ???????????? try ??
  9. ????????????{ ??
  10. ???????????????? if ?(bean.getClassName()?!=? null ?&&?! "" .equals(bean.getClassName().trim())) ??
  11. ????????????????????beanObject.put(bean.getId(),?Class.forName(bean.getClassName()).newInstance()); ??
  12. ????????????}? catch ?(Exception?e) ??
  13. ????????????{ ??
  14. ????????????????e.printStackTrace(); ??
  15. ????????????} ??
  16. ????????} ??
  17. ??
  18. ????}??
    /**
	 * bean的實例化
	 */
	private void instanceBeans()
	{
		for (Bean bean : beanList)
		{
			try
			{
				if (bean.getClassName() != null && !"".equals(bean.getClassName().trim()))
					beanObject.put(bean.getId(), Class.forName(bean.getClassName()).newInstance());
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}

	}
  

?其中beanObject為Map對象。

?

最后再加入一個方法,方便外部能獲取Map中的對象

Java代碼 復制代碼
  1. /** ?
  2. ?????*?獲取bean實例 ?
  3. ?????*?@param?beanName?配置文件中bean的Id ?
  4. ?????*?@return ?
  5. ?????*/ ??
  6. ???? public ?Object?getBean(String?beanName) ??
  7. ????{ ??
  8. ???????? return ? this .beanObject.get(beanName); ??
  9. ????}??
    /**
	 * 獲取bean實例
	 * @param beanName 配置文件中bean的Id
	 * @return
	 */
	public Object getBean(String beanName)
	{
		return this.beanObject.get(beanName);
	}
  

?完整的MyClassPathXMLApplicationContext見附件中的代碼。

?

下面測試MyClassPathXMLApplicationContext類。

Java代碼 復制代碼
  1. @Test ??
  2. ???? public ? void ?testMethod()? throws ?Exception ??
  3. ????{ ??
  4. ???????? //讀取配置文件 ??
  5. ????????MyClassPathXMLApplicationContext?ctx= new ?MyClassPathXMLApplicationContext( "applicationContext.xml" ); ??
  6. ???????? //獲取UserBean的實例 ??
  7. ????????PersonBean?bean=(PersonBean)ctx.getBean( "userBean" ); ??
  8. ???????? //調用方法 ??
  9. ????????bean.show(); ??
  10. ????}??
    @Test
	public void testMethod() throws Exception
	{
		//讀取配置文件
		MyClassPathXMLApplicationContext ctx=new MyClassPathXMLApplicationContext("applicationContext.xml");
		//獲取UserBean的實例
		PersonBean bean=(PersonBean)ctx.getBean("userBean");
		//調用方法
		bean.show();
	}
  

?

輸出結果

結果代碼 復制代碼
  1. Hello?Kuka??
    Hello Kuka
  

?

成功。

上面僅是簡單的演示了Spring管理Bean的原理,但是在實際操作中還需要考慮很對其它因素。

Spring學習筆記(3)----編碼剖析Spring管理Bean的原理


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产成人影院一区二区 | 天天操天天干天天射 | snh48欧洲大片在线观看 | 国产精品永久免费视频观看 | 欧美亚洲国产激情一区二区 | 91久久夜色精品国产九色 | 日韩视频 中文字幕 | 成人黄18免费视频 | 九九热视频在线播放 | 亚洲欧美日韩精品高清 | 欧美高清亚洲欧美一区h | 日韩亚洲欧美在线爱色 | 亚洲精品99久久久久久 | 成人18免费网 | 国产成人精品综合久久久软件 | 国产精品免费久久久免费 | 奇米777视频国产 | 国产成人亚洲综合91精品555 | 欧美一区二区三区久久久 | 26uuu精品一区二区 | 亚洲一区二区三区在线视频 | 在线观看 一区二区 麻豆 | 亚洲精品第一页中文字幕 | 免费性网站| 亚洲精品香蕉一区二区 | 911视频免费| 九九精品视频一区在线 | 狠狠色噜噜综合社区 | 毛片大全高清免费 | 免费高清一级欧美片在线观看 | 亚洲国产人成中文幕一级二级 | 久久精品中文字幕 | 香蕉视频精品 | 免费一级毛片麻豆精品 | 四虎影院在线免费播放 | 久久成人在线视频 | 亚洲aa视频 | 成人欧美 | 日本中文字幕一区二区高清在线 | 久久精品夜夜夜夜夜久久 | 一及毛片 |