cn.fql.springcontainer.ioc.BeanContext.java Source code

Java tutorial

Introduction

Here is the source code for cn.fql.springcontainer.ioc.BeanContext.java

Source

/*
 *
 * Copyright (c) 2009 Wincor Nixdorf International GmbH,
 * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of Wincor Nixdorf ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with Wincor Nixdorf.
 */
package cn.fql.springcontainer.ioc;

import org.dom4j.Document;

import java.util.HashMap;
import java.util.Map;

/**
 * cn.fql.springcontainer.ioc
 *
 * @author Fu, quanlin, WN ASP SSD
 * @version $Revision$
 */
public class BeanContext implements BeanFactory {

    Map beanMap = new HashMap();

    public BeanContext() throws Exception {
        SAXBuilder builder = new SAXBuilder();
        Document document = builder.build(this.getClass().getClassLoader().getResource("resource/beans.xml"));

        Element root = document.getRootElement();

        List<Element> list = root.getChildren("bean"); // beanElement

        for (int i = 0; i < list.size(); i++) {
            Element element = (Element) list.get(i);

            String id = element.getAttributeValue("id");
            String clazz = element.getAttributeValue("class");

            System.out.println(id + " : " + clazz);
            Object obj = Class.forName(clazz).newInstance(); // 1th.Bean
            beans.put(id, obj);

            /**
             *  <bean id="u" class="com.hzy.dao.impl.UserDAOImpl"/>
                
             <bean id="userService" class="com.hzy.service.UserService">
             <property name="userDAO" bean="u"/>
             </bean>
             */
            // 2th.?
            // property
            for (Element propertyElement : (List<Element>) element.getChildren("property")) {
                String name = propertyElement.getAttributeValue("name"); // userDAO
                String injectBean = propertyElement.getAttributeValue("bean"); // u;
                Object propertyObj = beans.get(injectBean);

                // 3th.userService userDAO  setter
                // name.substring( 0, 1 ).toUpperCase()  u ??
                String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);

                System.out.println("method name = " + methodName);

                /**
                 * getMethod ..??     ??
                 * Returns a Method object that reflects the specified
                 * public member method of the class or interface represented by this Class object.
                 */
                Method m = obj.getClass().getMethod(methodName, propertyObj.getClass().getInterfaces());
                // 4th.
                m.invoke(obj, propertyObj);
            }
        }
    }

    @Override
    public Object getBean(String name) {
        return beanMap.get(name);
    }
}
/**
 * History:
 *
 *  : cn.fql.springcontainer.ioc  $
 */