<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Never give up!

    如果說軟件開發是一條布滿荊棘的坎坷之路,那么我會每天在這道路兩旁書上“火焰舞者,到此一游!”。

     

    Spring IoC的簡單實現1

    先定義一個BeanDefinition
    public class BeanDefinition {

        
    private String id;
        
    private String className;
        
        
    public BeanDefinition(String id, String className) {
            
    this.id = id;
            
    this.className = 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;
        }

    }

    接著寫IoC的實現容器,流程如下:
    讀取xml配置文件—>把配置文件內所有的bean節點信息添加到BeanDefinition容器內—>構造和BeanDefinition相應的BeanClass—>查找bean
    public class MySpringApplicationContext {

        
    /** bean的信息集合 */
        
    private List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();
        
        
    /** bean的class集合 */
        
    private Map<String, Class> beanClass = new HashMap<String, Class>();
        
        
    /**
         * 容器初始化
         * 
         * 
    @param fileName 配置文件(path和name)
         
    */

        
    public MySpringApplicationContext(String fileName) {
            readXML(fileName);
            initBeanClass(beanDefinitions);
        }

        
        
    /**
         * 獲取MySpringApplicationContext容器內的bean實例
         * 
         * 
    @param id bean id
         * 
    @return bean實例
         
    */

        
    public Object getBean(String id) {
            
    try {
                
    return beanClass.get(id).newInstance();
            }
     catch (InstantiationException e) {
                
    throw new RuntimeException(e);
            }
     catch (IllegalAccessException e) {
                
    throw new RuntimeException(e);
            }

        }

        
        
    /**
         * 初始化所有bean的class
         * 
         * 
    @param beanDefinitions bean定義信息
         
    */

        
    private void initBeanClass(List<BeanDefinition> beanDefinitions) {
            
    for (BeanDefinition beanDefinition : beanDefinitions) {
                
    if (beanDefinition.getClassName() != null && beanDefinition.getClassName().length() != 0{
                    beanClass.put(beanDefinition.getId(), getClass(beanDefinition));
                }
     else {
                    
    throw new RuntimeException("class is empty");
                }

            }

        }

        
        
    /**
         * 讀取XML文件(利用dom4j,加dom4j-1.6.1.jar、jaxen-1.1-beta-7.jar文件)
         * 
         * 
    @param fileName 配置文件(path和name)
         
    */

        
    private void readXML(String fileName) {
            SAXReader reader 
    = new SAXReader();
            Document document 
    = null;
            URL url 
    = getClass().getClassLoader().getResource(fileName);
            
            
    try {
                document 
    = reader.read(url);
                
                Map
    <String, String> nameSpaceMap = new HashMap<String, String>();
                nameSpaceMap.put(
    "namespace""http://www.springframework.org/schema/beans"); // 加入命名空間
                
                XPath xpath 
    = document.createXPath("//namespace:beans/namespace:bean"); // 創建查詢路徑(beans根目錄下的bean元素)
                xpath.setNamespaceURIs(nameSpaceMap); // 設置命名空間
                
                List
    <Element> beans = xpath.selectNodes(document); // 獲取beans根目錄下的所有bean節點
                for (Element element : beans) {
                     String id 
    = element.attributeValue("id"); // 獲取id屬性值
                     String className = element.attributeValue("class"); // 獲取class屬性值
                     BeanDefinition definition = new BeanDefinition(id, className);
                     beanDefinitions.add(definition);
                }

                
            }
     catch (DocumentException e) {
                
    throw new RuntimeException("failed to read xml file!", e);
            }

        }

        
        
    /**
         * 獲取class
         * 
         * 
    @param beanDefinition bean定義信息
         * 
    @return class
         
    */

        
    private Class getClass(BeanDefinition beanDefinition) {
            
            
    try {
                
    return Class.forName(beanDefinition.getClassName());
            }
     catch (ClassNotFoundException e) {
                
    throw new RuntimeException("class not found!", e);
            }

        }

        
    }

    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"
        xsi:schemaLocation
    ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

        
    <bean id="productDao" class="ProductDao">
        
    </bean>
        
    </beans>

    Bean:
    public class ProductDao {

        
    private String name;
        
        
    public String getName() {
            
    return name;
        }


        
    public void setName(String name) {
            
    this.name = name;
        }


        
    public void save() {
            System.out.println(
    "產品插入數據庫");
        }

    }

    測試類: 
    public class SpringTest {

        
    public static void main(String[] args) {
            MySpringApplicationContext applicationContext 
    = new MySpringApplicationContext("applicationContext.xml");
            ProductDao productDao 
    = (ProductDao)applicationContext.getBean("productDao");
            productDao.save();
            
        }

    }


    測試結果:產品插入數據庫

    posted on 2009-03-26 14:41 永遠的火焰舞者 閱讀(326) 評論(0)  編輯  收藏 所屬分類: spring


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆分類(10)

    隨筆檔案(9)

    文章檔案(1)

    搜索

    最新評論

    • 1.?re: JForum安裝
    • 我就是想研究下sso,哈哈!再在JForum的基礎上二次開發玩玩 呵呵
    • --Jlg
    • 2.?re: JForum安裝
    • JForum的代碼還比較清晰,但談不上強大,雖然一般也足夠用了。
    • --一農

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲国产精品免费观看 | 成年女人毛片免费播放视频m| 亚洲情A成黄在线观看动漫软件| 又爽又黄无遮挡高清免费视频| 三年片在线观看免费| 99999久久久久久亚洲| 亚洲天堂在线视频| 波多野结衣免费在线| 一级毛片高清免费播放| 亚洲高清美女一区二区三区| 国产午夜无码视频免费网站| 99久久免费观看| 美女被暴羞羞免费视频| 亚洲色成人网一二三区| 亚洲国产综合无码一区二区二三区 | 精品日韩亚洲AV无码一区二区三区 | 亚洲一级Av无码毛片久久精品| 男女作爱在线播放免费网站| 亚洲啪AV永久无码精品放毛片 | 美女裸免费观看网站| 亚洲成a人片毛片在线| 国产亚洲?V无码?V男人的天堂| 搡女人真爽免费视频大全| 玖玖在线免费视频| 免费人人潮人人爽一区二区 | 久久国产一片免费观看| 中文日韩亚洲欧美制服| 亚洲色图在线观看| 一本久久a久久精品亚洲| 青青草国产免费久久久下载| 亚洲成人免费网站| 东方aⅴ免费观看久久av| 麻豆69堂免费视频| 亚洲无人区码一二三码区别图片| 亚洲丝袜美腿视频| 亚洲人成色7777在线观看| 四虎影视免费永久在线观看| 欧美日韩国产免费一区二区三区 | 在线免费一区二区| 99国产精品永久免费视频| 好紧我太爽了视频免费国产|