Search in sources :

Example 1 with DefaultBeanConfiguration

use of org.grails.spring.DefaultBeanConfiguration in project grails-core by grails.

the class BeanBuilder method invokeBeanDefiningMethod.

/**
     * Called when a bean definition node is called.
     *
     * @param name The name of the bean to define
     * @param args The arguments to the bean. The first argument is the class name, the last argument is sometimes a closure. All
     * the arguments in between are constructor arguments
     * @return The bean configuration instance
     */
protected BeanConfiguration invokeBeanDefiningMethod(String name, Object[] args) {
    boolean hasClosureArgument = args[args.length - 1] instanceof Closure;
    if (args[0] instanceof Class) {
        Class<?> beanClass = args[0] instanceof Class ? (Class<?>) args[0] : args[0].getClass();
        if (args.length >= 1) {
            if (hasClosureArgument) {
                if (args.length - 1 != 1) {
                    currentBeanConfig = springConfig.addSingletonBean(name, beanClass, resolveConstructorArguments(args, 1, args.length - 1));
                } else {
                    currentBeanConfig = springConfig.addSingletonBean(name, beanClass);
                }
            } else {
                currentBeanConfig = springConfig.addSingletonBean(name, beanClass, resolveConstructorArguments(args, 1, args.length));
            }
        }
    } else if (args[0] instanceof RuntimeBeanReference) {
        currentBeanConfig = springConfig.addSingletonBean(name);
        currentBeanConfig.setFactoryBean(((RuntimeBeanReference) args[0]).getBeanName());
    } else if (args[0] instanceof Map) {
        // named constructor arguments
        if (args.length > 1 && args[1] instanceof Class) {
            List<?> constructorArgs = resolveConstructorArguments(args, 2, hasClosureArgument ? args.length - 1 : args.length);
            currentBeanConfig = springConfig.addSingletonBean(name, (Class<?>) args[1], constructorArgs);
            @SuppressWarnings("rawtypes") Map namedArgs = (Map) args[0];
            for (Object o : namedArgs.keySet()) {
                String propName = (String) o;
                setProperty(propName, namedArgs.get(propName));
            }
        } else // factory method syntax
        {
            //First arg is the map containing factoryBean : factoryMethod
            @SuppressWarnings("rawtypes") Map.Entry factoryBeanEntry = (Map.Entry) ((Map) args[0]).entrySet().iterator().next();
            // If we have a closure body, that will be the last argument.
            // In between are the constructor args
            int constructorArgsTest = hasClosureArgument ? 2 : 1;
            // If we have more than this number of args, we have constructor args
            if (args.length > constructorArgsTest) {
                //factory-method requires args
                int endOfConstructArgs = hasClosureArgument ? args.length - 1 : args.length;
                currentBeanConfig = springConfig.addSingletonBean(name, null, resolveConstructorArguments(args, 1, endOfConstructArgs));
            } else {
                currentBeanConfig = springConfig.addSingletonBean(name);
            }
            currentBeanConfig.setFactoryBean(factoryBeanEntry.getKey().toString());
            currentBeanConfig.setFactoryMethod(factoryBeanEntry.getValue().toString());
        }
    } else if (args[0] instanceof Closure) {
        currentBeanConfig = springConfig.addAbstractBean(name);
    } else {
        List<?> constructorArgs = resolveConstructorArguments(args, 0, hasClosureArgument ? args.length - 1 : args.length);
        currentBeanConfig = new DefaultBeanConfiguration(name, null, constructorArgs);
        springConfig.addBeanConfiguration(name, currentBeanConfig);
    }
    if (hasClosureArgument) {
        Closure<?> callable = (Closure<?>) args[args.length - 1];
        callable.setDelegate(this);
        callable.setResolveStrategy(Closure.DELEGATE_FIRST);
        callable.call(new Object[] { currentBeanConfig });
    }
    BeanConfiguration beanConfig = currentBeanConfig;
    currentBeanConfig = null;
    return beanConfig;
}
Also used : DefaultBeanConfiguration(org.grails.spring.DefaultBeanConfiguration) Closure(groovy.lang.Closure) GString(groovy.lang.GString) MetaClass(groovy.lang.MetaClass) GroovyObject(groovy.lang.GroovyObject) ManagedList(org.springframework.beans.factory.support.ManagedList) List(java.util.List) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) DefaultBeanConfiguration(org.grails.spring.DefaultBeanConfiguration) BeanConfiguration(org.grails.spring.BeanConfiguration) Map(java.util.Map) HashMap(java.util.HashMap) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Example 2 with DefaultBeanConfiguration

use of org.grails.spring.DefaultBeanConfiguration in project grails-core by grails.

the class BeanBuilder method createDynamicElementReader.

protected DynamicElementReader createDynamicElementReader(String namespace, final boolean decorator) {
    NamespaceHandler handler = namespaceHandlers.get(namespace);
    ParserContext parserContext = new ParserContext(readerContext, new BeanDefinitionParserDelegate(readerContext));
    final DynamicElementReader dynamicElementReader = new DynamicElementReader(namespace, namespaces, handler, parserContext) {

        @Override
        protected void afterInvocation() {
            if (!decorator) {
                currentBeanConfig = null;
            }
        }
    };
    dynamicElementReader.setClassLoader(classLoader);
    if (currentBeanConfig != null) {
        dynamicElementReader.setBeanConfiguration(currentBeanConfig);
    } else if (!decorator) {
        currentBeanConfig = new DefaultBeanConfiguration(namespace);
        dynamicElementReader.setBeanConfiguration(currentBeanConfig);
    }
    dynamicElementReader.setBeanDecorator(decorator);
    return dynamicElementReader;
}
Also used : DefaultBeanConfiguration(org.grails.spring.DefaultBeanConfiguration) BeanDefinitionParserDelegate(org.springframework.beans.factory.xml.BeanDefinitionParserDelegate) NamespaceHandler(org.springframework.beans.factory.xml.NamespaceHandler) ParserContext(org.springframework.beans.factory.xml.ParserContext)

Aggregations

DefaultBeanConfiguration (org.grails.spring.DefaultBeanConfiguration)2 Closure (groovy.lang.Closure)1 GString (groovy.lang.GString)1 GroovyObject (groovy.lang.GroovyObject)1 MetaClass (groovy.lang.MetaClass)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 BeanConfiguration (org.grails.spring.BeanConfiguration)1 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)1 ManagedList (org.springframework.beans.factory.support.ManagedList)1 ManagedMap (org.springframework.beans.factory.support.ManagedMap)1 BeanDefinitionParserDelegate (org.springframework.beans.factory.xml.BeanDefinitionParserDelegate)1 NamespaceHandler (org.springframework.beans.factory.xml.NamespaceHandler)1 ParserContext (org.springframework.beans.factory.xml.ParserContext)1