Example usage for org.springframework.beans.factory.support RootBeanDefinition setAutowireMode

List of usage examples for org.springframework.beans.factory.support RootBeanDefinition setAutowireMode

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support RootBeanDefinition setAutowireMode.

Prototype

public void setAutowireMode(int autowireMode) 

Source Link

Document

Set the autowire mode.

Usage

From source file:com.taobao.itest.spring.context.SpringContextManager.java

public static void registerBeanDefinition(Field field, ApplicationContext applicationContext) {
    @SuppressWarnings("deprecation")
    RootBeanDefinition beanDefinition = new RootBeanDefinition(field.getType(), true);
    beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME);
    DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) (applicationContext
            .getAutowireCapableBeanFactory());
    defaultListableBeanFactory.registerBeanDefinition(field.getName(), beanDefinition);
}

From source file:com.clican.pluto.dataprocess.spring.parser.DeployParser.java

public BeanDefinition parse(Element element, ParserContext parserContext) {
    BeanDefinitionRegistry bdr = parserContext.getRegistry();
    RootBeanDefinition beanDef = new RootBeanDefinition();
    beanDef.setAbstract(false);/*  w  w  w .  java  2s.c  om*/
    beanDef.setBeanClass(Deploy.class);
    beanDef.setLazyInit(false);
    beanDef.setAutowireMode(Autowire.BY_NAME.value());
    String id = element.getAttribute("id");
    if (StringUtils.isEmpty(id)) {
        id = "dplDeploy#" + element.hashCode();
    }
    bdr.registerBeanDefinition(id, beanDef);

    this.setBeanDefinitionStringProperty("name", beanDef, element);
    this.setBeanDefinitionStringProperty("url", beanDef, element);
    this.setBeanDefinitionStringProperty("propertyResources", beanDef, element);

    return beanDef;
}

From source file:org.seasar.dao.spring.autoregister.AbstractBeanAutoRegister.java

protected void register(final String packageName, final String shortClassName) {

    final String className = ClassUtil.concatName(packageName, shortClassName);

    final Class targetClass = ClassUtil.forName(className);
    Class enhancedClass;/*from www .j av  a  2 s . c o  m*/

    final AspectWeaver weaver = new AspectWeaver(targetClass, null);

    final Method[] methods = targetClass.getMethods();
    for (int i = 0; i < methods.length; ++i) {
        final Method method = methods[i];
        if (isBridgeMethod(method)) {
            continue;
        }

        final List interceptorList = new ArrayList();
        for (int j = 0; j < interceptorNames.length; j++) {
            final MethodInterceptor interceptor = (MethodInterceptor) getBeanFactory()
                    .getBean(interceptorNames[j]);
            interceptorList.add(interceptor);
        }
        if (!isApplicableAspect(method)) {
            continue;
        }
        if (interceptorList.size() == 0) {
            weaver.setInterceptors(method, new MethodInterceptor[0]);
        } else {
            weaver.setInterceptors(method, (MethodInterceptor[]) interceptorList
                    .toArray(new MethodInterceptor[interceptorList.size()]));
        }
    }
    enhancedClass = weaver.generateClass();

    final BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(
            new XmlReaderContext(null, null, null, null, null, null));
    final int autowireMode = delegate.getAutowireMode(autowire);

    final RootBeanDefinition bd = new RootBeanDefinition();
    bd.setBeanClass(enhancedClass);
    bd.setAutowireMode(autowireMode);
    bd.setScope(scope);

    String beanName;
    if (autoNaming != null) {
        beanName = autoNaming.defineName(packageName, shortClassName);
    } else {
        beanName = className;
    }
    getBeanFactory().registerBeanDefinition(beanName, bd);

}

From source file:com.clican.pluto.dataprocess.spring.parser.ParamProcessorParser.java

@SuppressWarnings("unchecked")

public void customiseBeanDefinition(BeanDefinition beanDef, Element element, ParserContext parserContext) {
    List paramBeanList = new ManagedList();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String localName = node.getLocalName();
            if ("param".equals(localName)) {
                RootBeanDefinition bean = new RootBeanDefinition();
                bean.setAbstract(false);
                bean.setBeanClass(ParamBean.class);
                bean.setLazyInit(false);
                bean.setAutowireMode(Autowire.BY_NAME.value());
                Element paramElement = (Element) node;
                String paramName = paramElement.getAttribute("paramName");
                String paramValue = paramElement.getAttribute("paramValue");
                String type = paramElement.getAttribute("type");
                String override = paramElement.getAttribute("override");
                String pattern = paramElement.getAttribute("pattern");
                bean.getPropertyValues().addPropertyValue("paramName", paramName);
                bean.getPropertyValues().addPropertyValue("paramValue", paramValue);
                bean.getPropertyValues().addPropertyValue("type", type);
                bean.getPropertyValues().addPropertyValue("pattern", pattern);
                if (StringUtils.isNotEmpty(override)) {
                    RootBeanDefinition over = new RootBeanDefinition();
                    over.setAbstract(false);
                    over.setBeanClass(Boolean.class);
                    over.setLazyInit(false);
                    over.getConstructorArgumentValues().addIndexedArgumentValue(0, override);
                    bean.getPropertyValues().addPropertyValue("override", over);
                }//from w w  w  .j a  v  a  2s  .  c o m
                paramBeanList.add(bean);
            }
        }
    }
    beanDef.getPropertyValues().addPropertyValue("paramBeanList", paramBeanList);
}

From source file:com.clican.pluto.dataprocess.spring.parser.ExcelProcessorParser.java

@SuppressWarnings("unchecked")

public void customiseBeanDefinition(BeanDefinition beanDef, Element element, ParserContext parserContext) {
    List excelBeanList = new ManagedList();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String localName = node.getLocalName();
            if ("write".equals(localName) || "read".equals(localName)) {
                RootBeanDefinition bean = new RootBeanDefinition();
                bean.setAbstract(false);
                bean.setBeanClass(ExcelExecBean.class);
                bean.setLazyInit(false);
                bean.setAutowireMode(Autowire.BY_NAME.value());
                Element paramElement = (Element) node;
                String paramName = paramElement.getAttribute("paramName");
                String resultName = paramElement.getAttribute("resultName");
                String resource = paramElement.getAttribute("resource");
                String sheetName = paramElement.getAttribute("sheetName");
                String columns = paramElement.getAttribute("columns");
                String columnsVarName = paramElement.getAttribute("columnsVarName");
                String sheetVarName = paramElement.getAttribute("sheetVarName");
                String typeMapStr = paramElement.getAttribute("typeMap");
                String resourceVarName = paramElement.getAttribute("resourceVarName");
                Map<String, String> typeMap = new HashMap<String, String>();
                if ("read".equals(localName)) {
                    for (String type : typeMapStr.split(";")) {
                        typeMap.put(type.split("=>")[0], type.split("=>")[1]);
                    }/*from w w  w .  j a  v a 2 s  .  c  o m*/
                }
                bean.getPropertyValues().addPropertyValue("paramName", paramName);
                bean.getPropertyValues().addPropertyValue("resultName", resultName);
                bean.getPropertyValues().addPropertyValue("sheetName", sheetName);
                if (StringUtils.isNotEmpty(columns)) {
                    bean.getPropertyValues().addPropertyValue("columns", columns.split(","));
                }
                bean.getPropertyValues().addPropertyValue("resource", resource);
                bean.getPropertyValues().addPropertyValue("typeMap", typeMap);
                bean.getPropertyValues().addPropertyValue("columnsVarName", columnsVarName);
                bean.getPropertyValues().addPropertyValue("sheetVarName", sheetVarName);
                bean.getPropertyValues().addPropertyValue("resourceVarName", resourceVarName);
                if ("write".equals(localName)) {
                    bean.getPropertyValues().addPropertyValue("read", false);
                }
                excelBeanList.add(bean);
            }
        }
    }
    beanDef.getPropertyValues().addPropertyValue("excelExecBeanList", excelBeanList);

}

From source file:com.clican.pluto.dataprocess.spring.parser.JdbcExecProcessorParser.java

@SuppressWarnings("unchecked")

public void customiseBeanDefinition(BeanDefinition beanDef, Element element, ParserContext parserContext) {
    String jdbcTemplate = element.getAttribute("jdbcTemplate");
    beanDef.getPropertyValues().addPropertyValue("jdbcTemplate", new RuntimeBeanReference(jdbcTemplate));
    List jdbcExecBeanList = new ManagedList();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String localName = node.getLocalName();
            if ("exec".equals(localName)) {
                RootBeanDefinition bean = new RootBeanDefinition();
                bean.setAbstract(false);
                bean.setBeanClass(JdbcExecBean.class);
                bean.setLazyInit(false);
                bean.setAutowireMode(Autowire.BY_NAME.value());

                Element jdbcExecElement = (Element) node;
                String batch = jdbcExecElement.getAttribute("batch");
                String paramName = jdbcExecElement.getAttribute("paramName");
                String resultName = jdbcExecElement.getAttribute("resultName");
                String paramNameMap = jdbcExecElement.getAttribute("paramNameMap");
                String singleRow = jdbcExecElement.getAttribute("singleRow");
                String clazz = jdbcExecElement.getAttribute("clazz");
                String sql = jdbcExecElement.getTextContent();
                if (StringUtils.isNotEmpty(paramNameMap)) {
                    Map<String, String> map = new HashMap<String, String>();
                    for (String pnm : paramNameMap.split(";")) {
                        String contextName = pnm.split("=>")[0].trim();
                        String ibatisName = pnm.split("=>")[1].trim();
                        map.put(contextName, ibatisName);
                    }/*ww w  .ja va 2s  .  c  om*/
                    bean.getPropertyValues().addPropertyValue("paramNameMap", map);
                }
                if (StringUtils.isNotEmpty(batch)) {
                    bean.getPropertyValues().addPropertyValue("batch", Boolean.parseBoolean(batch));
                }
                if (StringUtils.isNotEmpty(singleRow)) {
                    bean.getPropertyValues().addPropertyValue("singleRow", Boolean.parseBoolean(singleRow));
                }
                if (StringUtils.isNotEmpty(clazz)) {
                    try {
                        bean.getPropertyValues().addPropertyValue("clazz", Class.forName(clazz));
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
                bean.getPropertyValues().addPropertyValue("paramName", paramName);
                bean.getPropertyValues().addPropertyValue("resultName", resultName);
                bean.getPropertyValues().addPropertyValue("sql", sql);
                jdbcExecBeanList.add(bean);
            }
        }
    }
    beanDef.getPropertyValues().addPropertyValue("jdbcExecBeanList", jdbcExecBeanList);
}

From source file:com.clican.pluto.dataprocess.spring.parser.AbstractProcessorParser.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public BeanDefinition parse(Element element, ParserContext parserContext) {
    BeanDefinitionRegistry bdr = parserContext.getRegistry();
    RootBeanDefinition beanDef = new RootBeanDefinition();
    beanDef.setDestroyMethodName("destroy");
    beanDef.setAbstract(false);/*from w w  w . ja va  2s .c o m*/
    beanDef.setBeanClass(getDataProcessorClass());
    beanDef.setLazyInit(false);
    beanDef.setAutowireMode(Autowire.BY_NAME.value());
    String id = element.getAttribute("id");
    if (bdr.containsBeanDefinition(id)) {
        throw new RuntimeException("id[" + id + "]??");
    }
    bdr.registerBeanDefinition(id, beanDef);
    beanDef.getPropertyValues().addPropertyValue("id", id);
    String startProcessor = element.getAttribute("startProcessor");
    boolean sp = false;
    if (StringUtils.isNotEmpty(startProcessor)) {
        sp = Boolean.parseBoolean(startProcessor);
    }
    beanDef.getPropertyValues().addPropertyValue("startProcessor", sp);

    String transaction = element.getAttribute("transaction");
    if (StringUtils.isNotEmpty(transaction)) {
        beanDef.getPropertyValues().addPropertyValue("transaction", transaction);
    } else if (sp) {
        beanDef.getPropertyValues().addPropertyValue("transaction", TransactionMode.BEGIN.getMode());
    }
    String cloneContext = element.getAttribute("cloneContext");
    if (StringUtils.isNotEmpty(cloneContext)) {
        beanDef.getPropertyValues().addPropertyValue("cloneContext", Boolean.parseBoolean(cloneContext));
    }
    String propagations = element.getAttribute("propagations");
    if (StringUtils.isNotEmpty(propagations)) {
        List<String> propataionList = new ArrayList<String>();
        for (String propagation : propagations.split(",")) {
            propataionList.add(propagation);
        }
        beanDef.getPropertyValues().addPropertyValue("propagations", propataionList);
    }
    String nextDataProcessors = element.getAttribute("nextDataProcessors");
    if (StringUtils.isNotEmpty(nextDataProcessors)) {
        List nextDataProcessList = new ManagedList();
        for (String nextDataProcess : nextDataProcessors.split(",")) {
            nextDataProcess = nextDataProcess.trim();
            nextDataProcessList.add(new RuntimeBeanReference(nextDataProcess));
        }
        beanDef.getPropertyValues().addPropertyValue("nextDataProcessors", nextDataProcessList);
    }

    customiseBeanDefinition(beanDef, element, parserContext);

    return beanDef;
}

From source file:com.clican.pluto.fsm.spring.parser.AbstractStateParser.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public BeanDefinition parse(Element element, ParserContext parserContext) {
    BeanDefinitionRegistry bdr = parserContext.getRegistry();
    RootBeanDefinition beanDef = new RootBeanDefinition();
    beanDef.setDestroyMethodName("destroy");
    beanDef.setAbstract(false);/*  w  ww  . j  a  v  a  2 s .  c o  m*/
    beanDef.setBeanClass(getStateClass(element));
    beanDef.setLazyInit(false);
    beanDef.setAutowireMode(Autowire.BY_NAME.value());
    String name = element.getAttribute("name");
    if (bdr.containsBeanDefinition(name)) {
        throw new RuntimeException("name[" + name + "]is defined duplicated");
    }
    bdr.registerBeanDefinition(name, beanDef);
    beanDef.getPropertyValues().addPropertyValue("name", name);
    String value = element.getAttribute("value");
    beanDef.getPropertyValues().addPropertyValue("value", value);

    String propagation = element.getAttribute("propagation");
    beanDef.getPropertyValues().addPropertyValue("propagation", propagation);
    String nextStates = element.getAttribute("nextStates");
    if (StringUtils.isNotEmpty(nextStates)) {
        List nextStateList = new ManagedList();
        for (String nextState : nextStates.split(",")) {
            nextStateList.add(new RuntimeBeanReference(nextState.trim()));
        }
        beanDef.getPropertyValues().addPropertyValue("nextStates", nextStateList);
    }

    String previousStates = element.getAttribute("previousStates");
    if (StringUtils.isNotEmpty(previousStates)) {
        List previousStateList = new ManagedList();
        for (String previousState : previousStates.split(",")) {
            previousStateList.add(new RuntimeBeanReference(previousState.trim()));
        }
        beanDef.getPropertyValues().addPropertyValue("previousStates", previousStateList);
    }

    NodeList nodeList = element.getChildNodes();
    Map nextCondStates = new ManagedMap();
    Map params = new ManagedMap();
    List stateListeners = new ManagedList();
    List taskListeners = new ManagedList();
    Map timeoutListeners = new ManagedMap();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            String localName = node.getLocalName();
            Element e = (Element) node;
            if ("nextCondStates".equals(localName)) {
                String expr = e.getAttribute("expr");
                nextStates = e.getAttribute("nextStates");
                List nextStateList = new ManagedList();
                if (StringUtils.isNotEmpty(nextStates)) {
                    for (String nextState : nextStates.split(",")) {
                        nextStateList.add(new RuntimeBeanReference(nextState.trim()));
                    }
                }
                nextCondStates.put(expr, nextStateList);
            } else if ("param".equals(localName)) {
                String paramName = e.getAttribute("name");
                String paramValue = e.getAttribute("value");
                params.put(paramName, paramValue);
            } else if ("stateListener".equals(localName) || "timeoutListener".equals(localName)
                    || "taskListener".equals(localName)) {
                String clazz = e.getAttribute("clazz");
                String listener = e.getAttribute("listener");
                Object obj;
                if (StringUtils.isNotEmpty(clazz)) {
                    try {
                        RootBeanDefinition bean = new RootBeanDefinition();
                        bean.setAbstract(false);
                        bean.setBeanClass(Class.forName(clazz));
                        bean.setLazyInit(false);
                        bean.setAutowireMode(Autowire.BY_NAME.value());
                        if ("timeoutListener".equals(localName)) {
                            String timeoutName = e.getAttribute("name");
                            String startTime = e.getAttribute("startTime");
                            String dueTime = e.getAttribute("dueTime");
                            String repeatTime = e.getAttribute("repeatTime");
                            String repeatDuration = e.getAttribute("repeatDuration");
                            String businessCalendarName = e.getAttribute("businessCalendarName");
                            bean.getPropertyValues().addPropertyValue("name", timeoutName);
                            bean.getPropertyValues().addPropertyValue("startTime", startTime);
                            bean.getPropertyValues().addPropertyValue("dueTime", dueTime);
                            bean.getPropertyValues().addPropertyValue("repeatTime", repeatTime);
                            bean.getPropertyValues().addPropertyValue("repeatDuration", repeatDuration);
                            bean.getPropertyValues().addPropertyValue("businessCalendarName",
                                    businessCalendarName);
                        }
                        NodeList nodeList2 = element.getChildNodes();
                        Map params2 = new ManagedMap();
                        for (int j = 0; j < nodeList2.getLength(); j++) {
                            Node node2 = nodeList2.item(j);
                            if (node2.getNodeType() == Node.ELEMENT_NODE) {
                                String localName2 = node2.getLocalName();
                                if ("param".equals(localName2)) {
                                    String paramName = ((Element) node2).getAttribute("name");
                                    String paramValue = ((Element) node2).getAttribute("value");
                                    params2.put(paramName, paramValue);
                                }
                            }
                        }
                        obj = bean;
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                } else if (StringUtils.isNotEmpty(listener)) {
                    obj = new RuntimeBeanReference(listener.trim());
                } else {
                    throw new RuntimeException("There must be clazz or listener parameter setting");
                }
                if ("stateListener".equals(localName)) {
                    stateListeners.add(obj);
                } else if ("taskListeners".equals(localName)) {
                    taskListeners.add(obj);
                } else {
                    String timeoutName = e.getAttribute("name");
                    timeoutListeners.put(timeoutName, obj);
                }
            }
        }
    }
    beanDef.getPropertyValues().addPropertyValue("nextCondStates", nextCondStates);
    beanDef.getPropertyValues().addPropertyValue("params", params);
    beanDef.getPropertyValues().addPropertyValue("stateListeners", stateListeners);
    if (element.getNodeName().equals("task")) {
        beanDef.getPropertyValues().addPropertyValue("taskListeners", taskListeners);
    }
    beanDef.getPropertyValues().addPropertyValue("timeoutListeners", timeoutListeners);

    customiseBeanDefinition(beanDef, element, parserContext);
    return beanDef;
}

From source file:org.rosenvold.spring.convention.ConventionBeanFactory.java

@Override
protected synchronized RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException {
    if (super.containsBeanDefinition(beanName)) {
        return super.getMergedLocalBeanDefinition(beanName);
    }/*from   w w w .  j a v  a2 s.c o  m*/
    final Class<?> type = getResolvedType(beanName);
    if (type != null) {
        RootBeanDefinition rootBeanDefinition = mergedBeanDefinitions.get(type);
        if (rootBeanDefinition == null) {
            rootBeanDefinition = new RootBeanDefinition(type);
            rootBeanDefinition.overrideFrom(getBeanDefinition(beanName));
            // Are these next two really necessary ???
            rootBeanDefinition.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
            rootBeanDefinition.setScope(getAnnotatedScope(type));
            mergedBeanDefinitions.put(type, rootBeanDefinition);
        }
        return rootBeanDefinition;
    }
    return null;
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@Test
public void testRegisterExistingSingletonWithAutowire() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("name", "Tony");
    pvs.add("age", "48");
    RootBeanDefinition bd = new RootBeanDefinition(DependenciesBean.class);
    bd.setPropertyValues(pvs);//from ww  w  .  j a v a2s . c  om
    bd.setDependencyCheck(RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
    bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
    lbf.registerBeanDefinition("test", bd);
    Object singletonObject = new TestBean();
    lbf.registerSingleton("singletonObject", singletonObject);

    assertTrue(lbf.containsBean("singletonObject"));
    assertTrue(lbf.isSingleton("singletonObject"));
    assertEquals(TestBean.class, lbf.getType("singletonObject"));
    assertEquals(0, lbf.getAliases("singletonObject").length);
    DependenciesBean test = (DependenciesBean) lbf.getBean("test");
    assertEquals(singletonObject, lbf.getBean("singletonObject"));
    assertEquals(singletonObject, test.getSpouse());
}