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

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

Introduction

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

Prototype

public void setBeanClass(@Nullable Class<?> beanClass) 

Source Link

Document

Specify the class for this bean.

Usage

From source file:com.weibo.api.motan.config.springsupport.MotanBeanDefinitionParser.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass,
        boolean required) throws ClassNotFoundException {
    RootBeanDefinition bd = new RootBeanDefinition();
    bd.setBeanClass(beanClass);
    // ??lazy init
    bd.setLazyInit(false);//from   ww  w .j  av  a  2 s .  com

    // id?id,idcontext
    String id = element.getAttribute("id");
    if ((id == null || id.length() == 0) && required) {
        String generatedBeanName = element.getAttribute("name");
        if (generatedBeanName == null || generatedBeanName.length() == 0) {
            generatedBeanName = element.getAttribute("class");
        }
        if (generatedBeanName == null || generatedBeanName.length() == 0) {
            generatedBeanName = beanClass.getName();
        }
        id = generatedBeanName;
        int counter = 2;
        while (parserContext.getRegistry().containsBeanDefinition(id)) {
            id = generatedBeanName + (counter++);
        }
    }
    if (id != null && id.length() > 0) {
        if (parserContext.getRegistry().containsBeanDefinition(id)) {
            throw new IllegalStateException("Duplicate spring bean id " + id);
        }
        parserContext.getRegistry().registerBeanDefinition(id, bd);
    }
    bd.getPropertyValues().addPropertyValue("id", id);
    if (ProtocolConfig.class.equals(beanClass)) {
        MotanNamespaceHandler.protocolDefineNames.add(id);

    } else if (RegistryConfig.class.equals(beanClass)) {
        MotanNamespaceHandler.registryDefineNames.add(id);

    } else if (BasicServiceInterfaceConfig.class.equals(beanClass)) {
        MotanNamespaceHandler.basicServiceConfigDefineNames.add(id);

    } else if (BasicRefererInterfaceConfig.class.equals(beanClass)) {
        MotanNamespaceHandler.basicRefererConfigDefineNames.add(id);

    } else if (ServiceConfigBean.class.equals(beanClass)) {
        String className = element.getAttribute("class");
        if (className != null && className.length() > 0) {
            RootBeanDefinition classDefinition = new RootBeanDefinition();
            classDefinition.setBeanClass(
                    Class.forName(className, true, Thread.currentThread().getContextClassLoader()));
            classDefinition.setLazyInit(false);
            parseProperties(element.getChildNodes(), classDefinition);
            bd.getPropertyValues().addPropertyValue("ref",
                    new BeanDefinitionHolder(classDefinition, id + "Impl"));
        }
    }

    Set<String> props = new HashSet<String>();
    ManagedMap parameters = null;
    // ??setbd
    for (Method setter : beanClass.getMethods()) {
        String name = setter.getName();
        // setXXX
        if (name.length() <= 3 || !name.startsWith("set") || !Modifier.isPublic(setter.getModifiers())
                || setter.getParameterTypes().length != 1) {
            continue;
        }
        String property = (name.substring(3, 4).toLowerCase() + name.substring(4)).replaceAll("_", "-");
        props.add(property);
        if ("id".equals(property)) {
            bd.getPropertyValues().addPropertyValue("id", id);
            continue;
        }
        String value = element.getAttribute(property);
        if ("methods".equals(property)) {
            parseMethods(id, element.getChildNodes(), bd, parserContext);
        }
        if (StringUtils.isBlank(value)) {
            continue;
        }
        value = value.trim();
        if (value.length() == 0) {
            continue;
        }
        Object reference;
        if ("ref".equals(property)) {
            if (parserContext.getRegistry().containsBeanDefinition(value)) {
                BeanDefinition refBean = parserContext.getRegistry().getBeanDefinition(value);
                if (!refBean.isSingleton()) {
                    throw new IllegalStateException(
                            "The exported service ref " + value + " must be singleton! Please set the " + value
                                    + " bean scope to singleton, eg: <bean id=\"" + value
                                    + "\" scope=\"singleton\" ...>");
                }
            }
            reference = new RuntimeBeanReference(value);
        } else if ("protocol".equals(property) && !StringUtils.isBlank(value)) {
            if (!value.contains(",")) {
                reference = new RuntimeBeanReference(value);
            } else {
                parseMultiRef("protocols", value, bd, parserContext);
                reference = null;
            }
        } else if ("registry".equals(property)) {
            parseMultiRef("registries", value, bd, parserContext);
            reference = null;
        } else if ("basicService".equals(property)) {
            reference = new RuntimeBeanReference(value);

        } else if ("basicReferer".equals(property)) {
            reference = new RuntimeBeanReference(value);

        } else if ("extConfig".equals(property)) {
            reference = new RuntimeBeanReference(value);
        } else {
            reference = new TypedStringValue(value);
        }

        if (reference != null) {
            bd.getPropertyValues().addPropertyValue(property, reference);
        }
    }
    if (ProtocolConfig.class.equals(beanClass)) {
        // protocolparameters?
        NamedNodeMap attributes = element.getAttributes();
        int len = attributes.getLength();
        for (int i = 0; i < len; i++) {
            Node node = attributes.item(i);
            String name = node.getLocalName();
            if (!props.contains(name)) {
                if (parameters == null) {
                    parameters = new ManagedMap();
                }
                String value = node.getNodeValue();
                parameters.put(name, new TypedStringValue(value, String.class));
            }
        }
        bd.getPropertyValues().addPropertyValue("parameters", parameters);
    }
    return bd;
}

From source file:org.apache.zeppelin.lens.LensBootstrap.java

public LensJLineShellComponent getLensJLineShellComponent() {
    GenericApplicationContext ctx = (GenericApplicationContext) getApplicationContext();
    RootBeanDefinition rbd = new RootBeanDefinition();
    rbd.setBeanClass(LensJLineShellComponent.class);
    DefaultListableBeanFactory bf = (DefaultListableBeanFactory) ctx.getBeanFactory();
    bf.registerBeanDefinition(LensJLineShellComponent.class.getSimpleName(), rbd);
    return ctx.getBean(LensJLineShellComponent.class);
}

From source file:com.newlandframework.rpc.spring.NettyRpcRegisteryParser.java

public BeanDefinition parse(Element element, ParserContext parserContext) {
    String id = element.getAttribute("id");
    String ipAddr = element.getAttribute("ipAddr");
    String protocolType = element.getAttribute("protocol");

    RootBeanDefinition beanDefinition = new RootBeanDefinition();
    beanDefinition.setBeanClass(NettyRpcRegistery.class);
    beanDefinition.getPropertyValues().addPropertyValue("ipAddr", ipAddr);
    beanDefinition.getPropertyValues().addPropertyValue("protocol", protocolType);
    parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);

    return beanDefinition;
}

From source file:com.newlandframework.rpc.spring.NettyRpcServiceParser.java

public BeanDefinition parse(Element element, ParserContext parserContext) {
    String interfaceName = element.getAttribute("interfaceName");
    String ref = element.getAttribute("ref");

    RootBeanDefinition beanDefinition = new RootBeanDefinition();
    beanDefinition.setBeanClass(NettyRpcService.class);
    beanDefinition.setLazyInit(false);//from ww w.j  av  a 2s .c  om
    beanDefinition.getPropertyValues().addPropertyValue("interfaceName", interfaceName);
    beanDefinition.getPropertyValues().addPropertyValue("ref", ref);

    parserContext.getRegistry().registerBeanDefinition(interfaceName, beanDefinition);

    return beanDefinition;
}

From source file:com.newlandframework.rpc.spring.NettyRpcReferenceParser.java

public BeanDefinition parse(Element element, ParserContext parserContext) {
    String interfaceName = element.getAttribute("interfaceName");
    String id = element.getAttribute("id");
    String ipAddr = element.getAttribute("ipAddr");
    String protocolType = element.getAttribute("protocol");

    RootBeanDefinition beanDefinition = new RootBeanDefinition();
    beanDefinition.setBeanClass(NettyRpcReference.class);
    beanDefinition.setLazyInit(false);//from w ww.j  a  v  a 2  s. c  o  m

    beanDefinition.getPropertyValues().addPropertyValue("interfaceName", interfaceName);
    beanDefinition.getPropertyValues().addPropertyValue("ipAddr", ipAddr);
    beanDefinition.getPropertyValues().addPropertyValue("protocol", protocolType);

    parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
    return beanDefinition;
}

From source file:com.codestd.spring.cxf.config.schema.AnnotationBeanDefinitionParser.java

@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
    RootBeanDefinition beanDefinition = new RootBeanDefinition();
    beanDefinition.setBeanClass(beanClass);
    beanDefinition.setLazyInit(false);/*from  ww  w . j  a va 2  s. co m*/

    String id = element.getAttribute("id");
    if (id == null || id.length() == 0) {
        String name = element.getAttribute("name");
        if (!StringUtils.isEmpty(name))
            id = name;
        else
            id = beanClass.getName();
    }

    if (parserContext.getRegistry().containsBeanDefinition(id)) {
        throw new IllegalStateException("Duplicate spring bean id " + id);
    }
    parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);

    String annotationPackage = element.getAttribute("package");
    if (!StringUtils.isEmpty(annotationPackage))
        beanDefinition.getPropertyValues().add("annotationPackage", annotationPackage);

    return 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 ww. j a v  a2 s  .c  o m*/
    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:com.bfd.harpc.config.spring.HarpcBeanDefinitionParser.java

/**
 * {@link#parse}//from w ww  .j av  a 2 s.c o  m
 * <p>
 * 
 * @param element
 * @param parserContext
 * @param clazz
 * @return {@link BeanDefinition}
 */
private BeanDefinition parse(Element element, ParserContext parserContext, Class<?> clazz) {
    RootBeanDefinition beanDefinition = new RootBeanDefinition();
    beanDefinition.setBeanClass(clazz);

    Method[] methods = clazz.getMethods();
    String id = StringUtils.EMPTY;
    for (Method method : methods) {
        if (method.getName().length() > 3 && method.getName().startsWith("set")
                && method.getParameterTypes().length == 1) {
            String attribute = method.getName().substring(3);
            char ch = attribute.charAt(0);
            attribute = Character.toLowerCase(ch) + attribute.substring(1);

            String value = element.getAttribute(attribute);

            if (StringUtils.isNotEmpty(value)) {
                Type type = method.getParameterTypes()[0];
                if (type == boolean.class) {
                    beanDefinition.getPropertyValues().addPropertyValue(attribute, Boolean.valueOf(value));
                } else {
                    if ("ref".equals(attribute) && parserContext.getRegistry().containsBeanDefinition(value)) {
                        beanDefinition.getPropertyValues().addPropertyValue(attribute,
                                new RuntimeBeanReference(value));
                    } else {
                        beanDefinition.getPropertyValues().addPropertyValue(attribute, value);
                        if ("id".equals(attribute)) {
                            id = value;
                        }
                    }
                }
            }
        }
    }
    parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);

    return beanDefinition;
}

From source file:com.helpinput.spring.registinerceptor.mvc.UrlInterceptorBeanRegistInterceptor.java

@Override
public BeanDefinition beforeRegist(Class<?> clz, String beanName, String scope, DefaultListableBeanFactory dlbf,
        BeanDefinitionBuilder builder) {
    if (getCondition(clz)) {
        String refDefname = beanName + "$$$$";

        RootBeanDefinition refDef = new RootBeanDefinition();
        refDef.setBeanClass(clz);
        refDef.setScope(scope);/*  w  w w.  j  a va2 s .c o  m*/
        dlbf.registerBeanDefinition(refDefname, refDef);

        RootBeanDefinition mappedInterceptorDef = new RootBeanDefinition(MappedInterceptor.class);
        mappedInterceptorDef.setScope(scope);

        ManagedList<String> includePatterns = null;
        ManagedList<String> excludePatterns = null;
        Object interceptorBean;

        Mapping mapAnn = clz.getAnnotation(Mapping.class);
        if (mapAnn != null) {
            String[] includes = mapAnn.value();
            if (Utils.hasLength(includes)) {
                includePatterns = new ManagedList<>(includes.length);
                for (String s : includes)
                    includePatterns.add(s);
            }
        }

        MappingExclude unMapAnn = clz.getAnnotation(MappingExclude.class);
        if (unMapAnn != null) {
            String[] excludes = unMapAnn.value();
            if (Utils.hasLength(excludes)) {
                excludePatterns = new ManagedList<>(excludes.length);
                for (String s : excludes)
                    excludePatterns.add(s);
            }
        }

        interceptorBean = new RuntimeBeanReference(refDefname);
        mappedInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, includePatterns);
        mappedInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, excludePatterns);
        mappedInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(2, interceptorBean);
        return mappedInterceptorDef;
    }
    return null;
}

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 ww  . ja v a 2 s .  co  m*/
                paramBeanList.add(bean);
            }
        }
    }
    beanDef.getPropertyValues().addPropertyValue("paramBeanList", paramBeanList);
}