- <? 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 > ??
<?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類,包含這兩個屬性:
?
- 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; ??
- ????} ??
- ???? ??
- } ??
- ???
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信息。
- /** ?
- ?????*?讀取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(); ??
- ????????} ??
- ??
- ????}??
/** * 讀取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對象中。
?
- /** ?
- ?????*?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(); ??
- ????????????} ??
- ????????} ??
- ??
- ????}??
/** * 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中的對象
- /** ?
- ?????*?獲取bean實例 ?
- ?????*?@param?beanName?配置文件中bean的Id ?
- ?????*?@return ?
- ?????*/ ??
- ???? public ?Object?getBean(String?beanName) ??
- ????{ ??
- ???????? return ? this .beanObject.get(beanName); ??
- ????}??
/** * 獲取bean實例 * @param beanName 配置文件中bean的Id * @return */ public Object getBean(String beanName) { return this.beanObject.get(beanName); }
?完整的MyClassPathXMLApplicationContext見附件中的代碼。
?
下面測試MyClassPathXMLApplicationContext類。
- @Test ??
- ???? public ? void ?testMethod()? throws ?Exception ??
- ????{ ??
- ???????? //讀取配置文件 ??
- ????????MyClassPathXMLApplicationContext?ctx= new ?MyClassPathXMLApplicationContext( "applicationContext.xml" ); ??
- ???????? //獲取UserBean的實例 ??
- ????????PersonBean?bean=(PersonBean)ctx.getBean( "userBean" ); ??
- ???????? //調用方法 ??
- ????????bean.show(); ??
- ????}??
@Test public void testMethod() throws Exception { //讀取配置文件 MyClassPathXMLApplicationContext ctx=new MyClassPathXMLApplicationContext("applicationContext.xml"); //獲取UserBean的實例 PersonBean bean=(PersonBean)ctx.getBean("userBean"); //調用方法 bean.show(); }
?
輸出結果
- Hello?Kuka??
Hello Kuka
?
成功。
上面僅是簡單的演示了Spring管理Bean的原理,但是在實際操作中還需要考慮很對其它因素。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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