码迷,mamicode.com
首页 > 编程语言 > 详细

spring

时间:2017-07-27 17:02:52      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:print   reflect   source   exce   except   pack   set   document   cte   

1.创建bean.xml

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <bean id="userController" class="com.augmentum.oes.controller.UserController" scope="singleton"></bean>
    <bean id="questionServiceImpl" class="com.augmentum.oes.service.impl.QuestionServiceImpl" scope="singleton"></bean>
    <bean id="userDaoImpl" class="com.augmentum.oes.dao.impl.UserDaoImpl" scope="singleton"></bean>
    
    
    <bean id="questionController" class="com.augmentum.oes.controller.QuestionController" scope="singleton">
        <property name="questionServiceImpl" ref="questionServiceImpl" value=""/>
    </bean>
    <bean id="questionServiceImpl" class="com.augmentum.oes.service.impl.QuestionServiceImpl" scope="singleton">
        <property name="questionDaoImpl" ref="questionDaoImpl"/>
    </bean>
    <bean id="questionDaoImpl" class="com.augmentum.oes.dao.impl.QuestionDaoImpl" scope="singleton">
        <property name="jdbcTemplete" ref="jdbcTemplete"/>
    </bean>
    <bean id="jdbcTemplete" class="com.augmentum.oes.common.JDBCTemplete" scope="singleton" ></bean>
</beans>
bean.xml

2.存储model

技术分享
package com.augmentum.oes.common;

import java.util.ArrayList;
import java.util.List;

public class BeanConfig {
    private String id;
    private String className;
    private String scope;

    private List<BeanProperty> properties = new ArrayList<BeanProperty>();

    public void addProperty(BeanProperty property) {
        properties.add(property);
    }

    public List<BeanProperty> getProperties() {
        return properties;
    }

    public void setProperties(List<BeanProperty> properties) {
        if (properties == null) {
            properties = new ArrayList<BeanProperty>();
        }
        this.properties = properties;
    }

    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;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

}
BeanConfig
技术分享
package com.augmentum.oes.common;

public class BeanProperty {
    private String name;
    private String value;
    private String ref;

    public String getName() {
        return name;
    }

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

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getRef() {
        return ref;
    }

    public void setRef(String ref) {
        this.ref = ref;
    }

}
BeanProperty

3.创建工厂

技术分享
package com.augmentum.oes.common;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.augmentum.oes.servlet.DispatcherServlet;

public class BeanFactory {
    private static Map<String, BeanConfig> beans = new HashMap<String, BeanConfig>();

    private static Map<String, Object> objects = new HashMap<String, Object>();

    private static BeanFactory beanFactory;

    public Object getBean(String id) {
        if (beans.containsKey(id)) {
            BeanConfig bean = beans.get(id);
            String scope = bean.getScope();
            if (scope == null || scope.equals("")) {
                scope = "singleton";
            }
            if (scope.equalsIgnoreCase("singleton")) {
                if (objects.containsKey(id)) {
                    return objects.get(id);
                }
            }

            String className = bean.getClassName();
            Class<?> clz = null;
            try {
                clz = Class.forName(className);
                Object object = clz.newInstance();

                if (scope.equalsIgnoreCase("singleton")) {
                    objects.put(id, object);
                }
                List<BeanProperty> beanProperties = bean.getProperties();

                if (beanProperties != null || !beanProperties.isEmpty()) {
                    for (BeanProperty beanProperty : beanProperties) {
                        String name = beanProperty.getName();
                        String fistChar = name.substring(0,1);
                        String leaveChar = name.substring(1);
                        String methodName ="set"+ fistChar.toUpperCase()+leaveChar;

                        Method method = null;
                        Method[] methods = clz.getMethods();
                        //find in object method
                        for (Method methodInClass : methods) {
                            String methodNameInClass = methodInClass.getName();
                            if (methodNameInClass.equals(methodName)) {
                                method = methodInClass;
                                break;
                            }
                        }

                        String ref = beanProperty.getRef();
                        String value = beanProperty.getValue();
                        if (ref != null && !ref.trim().equals("")) {
                            //digui  get this bean.xml have bean set Object
                            Object refObj = this.getBean(ref);
                            method.invoke(object, refObj);
                        } else if (value != null && !value.trim().equals("")) {
                            Class<?>[] parmts = method.getParameterTypes();
                            String propertyValue = beanProperty.getValue();
                            if (parmts[0]==String.class) {
                                method.invoke(object, propertyValue);
                            }
                            if (parmts[0]==int.class) {
                                method.invoke(object, Integer.parseInt(propertyValue));
                            }
                            if (parmts[0]==boolean.class) {
                                method.invoke(object, Boolean.parseBoolean(propertyValue));
                            }
                        }
                    }
                }

                return object;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private BeanFactory(){}
    //thread  join
    public static BeanFactory getInstance() {
        if (beanFactory == null) {
            beanFactory = new BeanFactory();
            beanFactory.init();
        }
        return beanFactory;
    }

    private void init() {
        InputStream inputStream = null;
        try {
            inputStream = DispatcherServlet.class.getClassLoader().getResourceAsStream("bean.xml");

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(inputStream);
            Element element = document.getDocumentElement();

            NodeList beanNodes = element.getElementsByTagName("bean");
            if (beanNodes == null) {
                return;
            }
            int beanLength = beanNodes.getLength();
            for (int i = 0; i < beanLength; i++) {
                Element beanElement = (Element) beanNodes.item(i);
                BeanConfig bean = new BeanConfig();
                String id = beanElement.getAttribute("id");
                bean.setId(id);
                String className = beanElement.getAttribute("class");
                bean.setClassName(className);
                String scope = beanElement.getAttribute("scope");
                bean.setScope(scope);

                beans.put(id, bean);

                NodeList beanPropertyNodes = beanElement.getElementsByTagName("property");

                if (beanPropertyNodes != null|| !beanPropertyNodes.equals("")) {
                    int beanPropertyLength = beanPropertyNodes.getLength();
                    for (int j = 0; j < beanPropertyLength; j++) {
                        Element beanPropertyElement = (Element) beanPropertyNodes.item(j);
                        BeanProperty beanProperty = new BeanProperty();
                        beanProperty.setName(beanPropertyElement.getAttribute("name"));
                        beanProperty.setRef(beanPropertyElement.getAttribute("ref"));
                        beanProperty.setValue(beanPropertyElement.getAttribute("value"));

                        bean.addProperty(beanProperty);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
BeanFactory

4.set注入

spring

标签:print   reflect   source   exce   except   pack   set   document   cte   

原文地址:http://www.cnblogs.com/mxz1994/p/7245750.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!