Search in sources :

Example 1 with Injection

use of org.eweb4j.ioc.config.bean.Injection in project eweb4j-framework by laiweiwei.

the class CheckConfigBean method checkIOCJnject.

/**
	 * Check the IOC component Inject part configuration
	 * 
	 * @param inject
	 * @param iocList
	 * @return
	 */
public static String checkIOCJnject(List<Injection> inject, List<IOCConfigBean> iocList, String beanID, String xmlFile) {
    String error = null;
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    if ("true".equalsIgnoreCase(cb.getIoc().getOpen()) || "1".equals(cb.getIoc().getOpen())) {
        StringBuilder sb = new StringBuilder();
        for (Injection inj : inject) {
            if (inj.getRef() != null && !"".equals(inj.getRef())) {
                boolean flag = false;
                for (IOCConfigBean iocBean : iocList) {
                    if (!iocBean.getId().equals(inj.getRef())) {
                        flag = true;
                    } else {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    sb.append("当前您填写的 :( ref=").append(inj.getRef()).append(" )是错误的!它必须是本文件中某个bean的id值 ;\n");
                }
            }
            if (!"".equals(inj.getType()) && inj.getType() != null) {
                if (!"int".equalsIgnoreCase(inj.getType()) && !"String".equalsIgnoreCase(inj.getType()) && !"boolean".equalsIgnoreCase(inj.getType()) && !"long".equalsIgnoreCase(inj.getType()) && !"double".equalsIgnoreCase(inj.getType()) && !"float".equalsIgnoreCase(inj.getType())) {
                    String type = inj.getType();
                    try {
                        Thread.currentThread().getContextClassLoader().loadClass(type);
                    } catch (ClassNotFoundException e) {
                        sb.append("当前您填写的:( type=").append(inj.getType()).append(" )是错误的!它只能填写为:int|String|long|float|boolean|double 中的一种 或者有效的类名 ;").append("\n");
                    }
                }
            }
            if ("int".equalsIgnoreCase(inj.getType())) {
                if (!inj.getValue().matches(RegexList.integer_regexp)) {
                    sb.append("当前您填写的:( value=").append(inj.getValue()).append(" )是错误的!它必须是整数! ;").append("\n");
                }
            }
        }
        if (!"".equals(sb.toString())) {
            error = "\n<br /><b>" + xmlFile + "[bean id=" + beanID + "][inject]:</b>\n" + sb.toString();
        }
    }
    return error;
}
Also used : ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) ResultConfigBean(org.eweb4j.mvc.config.bean.ResultConfigBean) IOCConfigBean(org.eweb4j.ioc.config.bean.IOCConfigBean) LogConfigBean(org.eweb4j.config.bean.LogConfigBean) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean) ValidatorConfigBean(org.eweb4j.mvc.config.bean.ValidatorConfigBean) Injection(org.eweb4j.ioc.config.bean.Injection) IOCConfigBean(org.eweb4j.ioc.config.bean.IOCConfigBean)

Example 2 with Injection

use of org.eweb4j.ioc.config.bean.Injection in project eweb4j-framework by laiweiwei.

the class IOC method getBean.

/**
	 * 生产出符合beanID名字的bean
	 * 
	 * @param <T>
	 * @param beanID
	 * @return
	 * @throws Exception
	 */
public static synchronized <T> T getBean(String beanID) {
    if (!containsBean(beanID)) {
        return null;
    }
    // 声明用来返回的对象
    T t = null;
    try {
        // 声明构造方法参数列表的初始化值 
        Object[] initargs = null;
        // 声明构造方法参数列表
        Class<?>[] args = null;
        List<Object> initargList = new ArrayList<Object>();
        List<Class<?>> argList = new ArrayList<Class<?>>();
        // setters cache
        Map<String, Object> properties = new Hashtable<String, Object>();
        // 遍历配置文件,找出beanID的bean
        if (IOCConfigBeanCache.containsKey(beanID)) {
            IOCConfigBean iocBean = IOCConfigBeanCache.get(beanID);
            // 取出该bean的类型,便于最后使用反射调用构造方法实例化
            Class<T> clazz = (Class<T>) Thread.currentThread().getContextClassLoader().loadClass(iocBean.getClazz());
            // 判断该bean的生命周期
            if (IOCConfigConstant.SINGLETON_SCOPE.equalsIgnoreCase(iocBean.getScope())) {
                // 如果是单件,就从单件缓存池中取
                if (SingleBeanCache.containsKey(beanID)) {
                    t = (T) SingleBeanCache.get(beanID);
                    return t;
                }
            }
            // 遍历每个bean的注入配置
            for (Iterator<Injection> it = iocBean.getInject().iterator(); it.hasNext(); ) {
                Injection inj = it.next();
                if (inj == null)
                    continue;
                String ref = inj.getRef();
                if (ref != null && !"".equals(ref)) {
                    // 如果ref不为空,说明注入的是对象类型,后面需要进入递归
                    String name = inj.getName();
                    if (name != null && !"".equals(name)) {
                        // 如果属性名字不为空,说明使用的是setter注入方式
                        properties.put(name, getBean(ref));
                    } else {
                        // 如果属性名字为空,说明使用的是构造器注入方式
                        // 使用构造器注入的时候,需要按照构造器参数列表顺序实例化
                        Object obj = getBean(ref);
                        initargList.add(obj);
                        String type = inj.getType();
                        if (type != null && type.trim().length() > 0) {
                            Class<?> cls = Thread.currentThread().getContextClassLoader().loadClass(type);
                            argList.add(cls);
                        } else {
                            argList.add(obj.getClass());
                        }
                    }
                } else {
                    // 注入基本类型
                    String type = inj.getType();
                    String value = inj.getValue();
                    if (value == null)
                        value = "";
                    String name = inj.getName();
                    if (name != null && !"".equals(name)) {
                        // 如果属性名字不为空,说明使用的是setter注入方式
                        if (IOCConfigConstant.INT_ARGTYPE.equalsIgnoreCase(type) || "java.lang.Integer".equalsIgnoreCase(type)) {
                            if ("".equals(value.trim()))
                                value = "0";
                            // int
                            properties.put(name, Integer.parseInt(value));
                        } else if (IOCConfigConstant.STRING_ARGTYPE.equalsIgnoreCase(type) || "java.lang.String".equalsIgnoreCase(type)) {
                            // String
                            properties.put(name, value);
                        } else if (IOCConfigConstant.LONG_ARGTYPE.equalsIgnoreCase(type) || "java.lang.Long".equalsIgnoreCase(type)) {
                            // long
                            if ("".equals(value.trim()))
                                value = "0";
                            properties.put(name, Long.parseLong(value));
                        } else if (IOCConfigConstant.FLOAT_ARGTYPE.equalsIgnoreCase(type) || "java.lang.Float".equalsIgnoreCase(type)) {
                            // float
                            if ("".equals(value.trim()))
                                value = "0.0";
                            properties.put(name, Float.parseFloat(value));
                        } else if (IOCConfigConstant.BOOLEAN_ARGTYPE.equalsIgnoreCase(type) || "java.lang.Boolean".equalsIgnoreCase(type)) {
                            // boolean
                            if ("".equals(value.trim()))
                                value = "false";
                            properties.put(name, Boolean.parseBoolean(value));
                        } else if (IOCConfigConstant.DOUBLE_ARGTYPE.equalsIgnoreCase(type) || "java.lang.Double".equalsIgnoreCase(type)) {
                            // double
                            if ("".equals(value.trim()))
                                value = "0.0";
                            properties.put(name, Double.parseDouble(value));
                        }
                    } else {
                        // 使用构造器注入的时候,需要按照构造器参数列表顺序实例化
                        if (IOCConfigConstant.INT_ARGTYPE.equalsIgnoreCase(type) || "java.lang.Integer".equalsIgnoreCase(type)) {
                            // int
                            if ("".equals(value.trim()))
                                value = "0";
                            argList.add(int.class);
                            initargList.add(Integer.parseInt(value));
                        } else if (IOCConfigConstant.LONG_ARGTYPE.equalsIgnoreCase(type) || "java.lang.Long".equalsIgnoreCase(type)) {
                            // long
                            if ("".equals(value.trim()))
                                value = "0";
                            argList.add(long.class);
                            initargList.add(Long.parseLong(value));
                        } else if (IOCConfigConstant.FLOAT_ARGTYPE.equalsIgnoreCase(type) || "java.lang.Float".equalsIgnoreCase(type)) {
                            // float
                            if ("".equals(value.trim()))
                                value = "0.0";
                            argList.add(float.class);
                            initargList.add(Float.parseFloat(value));
                        } else if (IOCConfigConstant.BOOLEAN_ARGTYPE.equalsIgnoreCase(type) || "java.lang.Boolean".equalsIgnoreCase(type)) {
                            // boolean
                            if ("".equals(value.trim()))
                                value = "false";
                            argList.add(boolean.class);
                            initargList.add(Boolean.parseBoolean(value));
                        } else if (IOCConfigConstant.DOUBLE_ARGTYPE.equalsIgnoreCase(type) || "java.lang.Double".equalsIgnoreCase(type)) {
                            // double
                            if ("".equals(value.trim()))
                                value = "0.0";
                            argList.add(double.class);
                            initargList.add(Double.parseDouble(value));
                        } else if (IOCConfigConstant.STRING_ARGTYPE.equalsIgnoreCase(type) || "java.lang.String".equalsIgnoreCase(type)) {
                            // String
                            argList.add(String.class);
                            initargList.add(value);
                        }
                    }
                }
            }
            // 如果构造方法参数列表不为空,说明需要使用构造方法进行注入
            if (argList.size() > 0 && initargList.size() > 0) {
                args = new Class<?>[argList.size()];
                initargs = new Object[initargList.size()];
                for (int i = 0; i < argList.size(); i++) {
                    args[i] = argList.get(i);
                    initargs[i] = initargList.get(i);
                }
                t = clazz.getDeclaredConstructor(args).newInstance(initargs);
            } else if (t == null) {
                t = clazz.newInstance();
                for (Iterator<Entry<String, Object>> it = properties.entrySet().iterator(); it.hasNext(); ) {
                    Entry<String, Object> e = it.next();
                    final String name = e.getKey();
                    ReflectUtil ru = new ReflectUtil(t);
                    Method m = ru.getSetter(name);
                    if (m == null)
                        continue;
                    m.invoke(t, e.getValue());
                }
            }
            // 判断该bean的生命周期
            if (IOCConfigConstant.SINGLETON_SCOPE.equalsIgnoreCase(iocBean.getScope())) {
                // 如果是单件,就从单件缓存池中取
                if (!SingleBeanCache.containsKey(beanID)) {
                    SingleBeanCache.add(beanID, t);
                }
            }
        }
    } catch (Throwable e) {
        log.error("IOC.getBean(" + beanID + ") failed!", e);
    }
    String info = "IOC.getBean(" + beanID + ") -> " + t;
    log.debug(info);
    return t;
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Injection(org.eweb4j.ioc.config.bean.Injection) Method(java.lang.reflect.Method) IOCConfigBean(org.eweb4j.ioc.config.bean.IOCConfigBean) ReflectUtil(org.eweb4j.util.ReflectUtil) Entry(java.util.Map.Entry) Iterator(java.util.Iterator)

Example 3 with Injection

use of org.eweb4j.ioc.config.bean.Injection in project eweb4j-framework by laiweiwei.

the class IOCConfigBeanCreator method getIOCBean.

public static IOCConfigBean getIOCBean() {
    IOCConfigBean iocBean = null;
    iocBean = new IOCConfigBean();
    List<Injection> list = new ArrayList<Injection>();
    Injection depen = new Injection();
    list.add(depen);
    iocBean.setInject(list);
    return iocBean;
}
Also used : ArrayList(java.util.ArrayList) Injection(org.eweb4j.ioc.config.bean.Injection) IOCConfigBean(org.eweb4j.ioc.config.bean.IOCConfigBean)

Aggregations

IOCConfigBean (org.eweb4j.ioc.config.bean.IOCConfigBean)3 Injection (org.eweb4j.ioc.config.bean.Injection)3 ArrayList (java.util.ArrayList)2 Method (java.lang.reflect.Method)1 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1 ConfigBean (org.eweb4j.config.bean.ConfigBean)1 LogConfigBean (org.eweb4j.config.bean.LogConfigBean)1 ActionConfigBean (org.eweb4j.mvc.config.bean.ActionConfigBean)1 FieldConfigBean (org.eweb4j.mvc.config.bean.FieldConfigBean)1 InterConfigBean (org.eweb4j.mvc.config.bean.InterConfigBean)1 ParamConfigBean (org.eweb4j.mvc.config.bean.ParamConfigBean)1 ResultConfigBean (org.eweb4j.mvc.config.bean.ResultConfigBean)1 ValidatorConfigBean (org.eweb4j.mvc.config.bean.ValidatorConfigBean)1 ORMConfigBean (org.eweb4j.orm.config.bean.ORMConfigBean)1 DBInfoConfigBean (org.eweb4j.orm.dao.config.bean.DBInfoConfigBean)1 ReflectUtil (org.eweb4j.util.ReflectUtil)1