Search in sources :

Example 46 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project apollo by ctripcorp.

the class BeanRegistrationUtil method registerBeanDefinitionIfNotExists.

public static boolean registerBeanDefinitionIfNotExists(BeanDefinitionRegistry registry, String beanName, Class<?> beanClass) {
    if (registry.containsBeanDefinition(beanName)) {
        return false;
    }
    String[] candidates = registry.getBeanDefinitionNames();
    for (String candidate : candidates) {
        BeanDefinition beanDefinition = registry.getBeanDefinition(candidate);
        if (Objects.equals(beanDefinition.getBeanClassName(), beanClass.getName())) {
            return false;
        }
    }
    BeanDefinition annotationProcessor = BeanDefinitionBuilder.genericBeanDefinition(beanClass).getBeanDefinition();
    registry.registerBeanDefinition(beanName, annotationProcessor);
    return true;
}
Also used : BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 47 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project opennms by OpenNMS.

the class JaxbUtils method getClassForElement.

public static Class<?> getClassForElement(final String elementName) {
    if (elementName == null)
        return null;
    final Class<?> existing = m_elementClasses.get(elementName);
    if (existing != null)
        return existing;
    final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(XmlRootElement.class));
    for (final BeanDefinition bd : scanner.findCandidateComponents("org.opennms")) {
        final String className = bd.getBeanClassName();
        try {
            final Class<?> clazz = Class.forName(className);
            final XmlRootElement annotation = clazz.getAnnotation(XmlRootElement.class);
            if (annotation == null) {
                LOG.warn("Somehow found class {} but it has no @XmlRootElement annotation! Skipping.", className);
                continue;
            }
            if (elementName.equalsIgnoreCase(annotation.name())) {
                LOG.trace("Found class {} for element name {}", className, elementName);
                m_elementClasses.put(elementName, clazz);
                return clazz;
            }
        } catch (final ClassNotFoundException e) {
            LOG.warn("Unable to get class object from class name {}. Skipping.", className, e);
        }
    }
    return null;
}
Also used : AnnotationTypeFilter(org.springframework.core.type.filter.AnnotationTypeFilter) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 48 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project opennms by OpenNMS.

the class Upgrade method getUpgradeObjects.

/**
 * Gets the upgrade objects.
 *
 * @return the upgrade objects
 * @throws OnmsUpgradeException the OpenNMS upgrade exception
 */
protected List<OnmsUpgrade> getUpgradeObjects() throws OnmsUpgradeException {
    List<OnmsUpgrade> upgrades = new ArrayList<>();
    try {
        ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
        provider.addIncludeFilter(new AssignableTypeFilter(OnmsUpgrade.class));
        Set<BeanDefinition> components = provider.findCandidateComponents(getClassScope());
        for (BeanDefinition component : components) {
            if (component.isAbstract()) {
                continue;
            }
            Class<?> cls = Class.forName(component.getBeanClassName());
            if (cls.getAnnotation(Ignore.class) != null) {
                continue;
            }
            OnmsUpgrade upgrade = (OnmsUpgrade) cls.newInstance();
            upgrades.add(upgrade);
            log("Found upgrade task %s\n", upgrade.getId());
        }
        Collections.sort(upgrades, new OnmsUpgradeComparator());
    } catch (Exception e) {
        throw new OnmsUpgradeException("  Can't find the upgrade classes because: " + e.getMessage(), e);
    }
    return upgrades;
}
Also used : Ignore(org.opennms.upgrade.api.Ignore) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) ArrayList(java.util.ArrayList) OnmsUpgrade(org.opennms.upgrade.api.OnmsUpgrade) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) OnmsUpgradeComparator(org.opennms.upgrade.api.OnmsUpgradeComparator) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) OnmsUpgradeException(org.opennms.upgrade.api.OnmsUpgradeException) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter)

Example 49 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project dubbo by alibaba.

the class DubboBeanDefinitionParser method parseMethods.

@SuppressWarnings("unchecked")
private static void parseMethods(String id, NodeList nodeList, RootBeanDefinition beanDefinition, ParserContext parserContext) {
    if (nodeList != null && nodeList.getLength() > 0) {
        ManagedList methods = null;
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node instanceof Element) {
                Element element = (Element) node;
                if ("method".equals(node.getNodeName()) || "method".equals(node.getLocalName())) {
                    String methodName = element.getAttribute("name");
                    if (methodName == null || methodName.length() == 0) {
                        throw new IllegalStateException("<dubbo:method> name attribute == null");
                    }
                    if (methods == null) {
                        methods = new ManagedList();
                    }
                    BeanDefinition methodBeanDefinition = parse(((Element) node), parserContext, MethodConfig.class, false);
                    String name = id + "." + methodName;
                    BeanDefinitionHolder methodBeanDefinitionHolder = new BeanDefinitionHolder(methodBeanDefinition, name);
                    methods.add(methodBeanDefinitionHolder);
                }
            }
        }
        if (methods != null) {
            beanDefinition.getPropertyValues().addPropertyValue("methods", methods);
        }
    }
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) ManagedList(org.springframework.beans.factory.support.ManagedList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 50 with BeanDefinition

use of org.springframework.beans.factory.config.BeanDefinition in project dubbo by alibaba.

the class DubboBeanDefinitionParser method parseArguments.

@SuppressWarnings("unchecked")
private static void parseArguments(String id, NodeList nodeList, RootBeanDefinition beanDefinition, ParserContext parserContext) {
    if (nodeList != null && nodeList.getLength() > 0) {
        ManagedList arguments = null;
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node instanceof Element) {
                Element element = (Element) node;
                if ("argument".equals(node.getNodeName()) || "argument".equals(node.getLocalName())) {
                    String argumentIndex = element.getAttribute("index");
                    if (arguments == null) {
                        arguments = new ManagedList();
                    }
                    BeanDefinition argumentBeanDefinition = parse(((Element) node), parserContext, ArgumentConfig.class, false);
                    String name = id + "." + argumentIndex;
                    BeanDefinitionHolder argumentBeanDefinitionHolder = new BeanDefinitionHolder(argumentBeanDefinition, name);
                    arguments.add(argumentBeanDefinitionHolder);
                }
            }
        }
        if (arguments != null) {
            beanDefinition.getPropertyValues().addPropertyValue("arguments", arguments);
        }
    }
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) ManagedList(org.springframework.beans.factory.support.ManagedList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Aggregations

BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)601 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)188 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)116 Test (org.junit.jupiter.api.Test)112 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)89 Element (org.w3c.dom.Element)72 Test (org.junit.Test)66 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)60 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)53 ClassPathScanningCandidateComponentProvider (org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)41 ManagedList (org.springframework.beans.factory.support.ManagedList)39 AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)36 BeanDefinitionRegistry (org.springframework.beans.factory.support.BeanDefinitionRegistry)35 BeanMetadataElement (org.springframework.beans.BeanMetadataElement)34 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)34 AnnotationTypeFilter (org.springframework.core.type.filter.AnnotationTypeFilter)31 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)30 Method (java.lang.reflect.Method)28 ArrayList (java.util.ArrayList)28 Map (java.util.Map)27