Example usage for org.springframework.beans.factory.config AutowireCapableBeanFactory autowireBeanProperties

List of usage examples for org.springframework.beans.factory.config AutowireCapableBeanFactory autowireBeanProperties

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config AutowireCapableBeanFactory autowireBeanProperties.

Prototype

void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck)
        throws BeansException;

Source Link

Document

Autowire the bean properties of the given bean instance by name or type.

Usage

From source file:com.opensymphony.xwork2.spring.SpringObjectFactory.java

/**
 * @param bean the bean to be autowired//from  w w  w  . j a v  a  2  s.c o m
 * @param autoWiringFactory the autowiring factory
 *
 * @return bean
 */
public Object autoWireBean(Object bean, AutowireCapableBeanFactory autoWiringFactory) {
    if (autoWiringFactory != null) {
        autoWiringFactory.autowireBeanProperties(bean, autowireStrategy, false);
    }
    injectApplicationContext(bean);

    injectInternalBeans(bean);

    return bean;
}

From source file:com.rvantwisk.cnctools.ScreensConfiguration.java

/**
 * Add's a new bean to Spring's context//w  w  w.ja  v a2s  .c o  m
 *
 * @param name
 */
public void registerBean(final String name) {
    if (registeredbeans.containsKey(name)) {
        return;
    }
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClassName(name);
    beanDefinition.setAutowireCandidate(true);
    AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
    registry.registerBeanDefinition(name, beanDefinition);
    factory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    registeredbeans.put(name, Boolean.TRUE);
}

From source file:org.jacp.project.launcher.SpringLauncher.java

@Override
public synchronized <T> T registerAndGetBean(Class<? extends T> type, final String id, final Scope scope) {
    if (this.factory.containsBean(id))
        return getBean(type);
    final AutowireCapableBeanFactory factory = getContext().getAutowireCapableBeanFactory();
    final BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
    final GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(type);//from w ww  . j  av a2 s.co m
    beanDefinition.setScope(scope.getType());
    beanDefinition.setAutowireCandidate(true);
    registry.registerBeanDefinition(id, beanDefinition);
    factory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    return getBean(type);
}

From source file:de.hybris.platform.btgcockpit.service.BTGCockpitServiceTest.java

public void initApplicationContext() {
    final GenericApplicationContext context = new GenericApplicationContext();
    context.setResourceLoader(new DefaultResourceLoader(Registry.class.getClassLoader()));
    context.setClassLoader(Registry.class.getClassLoader());
    context.getBeanFactory().setBeanClassLoader(Registry.class.getClassLoader());
    context.setParent(Registry.getGlobalApplicationContext());
    final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
    xmlReader.setDocumentReaderClass(ScopeTenantIgnoreDocReader.class);
    xmlReader.setBeanClassLoader(Registry.class.getClassLoader());
    xmlReader.loadBeanDefinitions(getSpringConfigurationLocations());
    context.refresh();/*w w  w .  j a  v  a2 s. c o  m*/
    final AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
    beanFactory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
    this.applicationContex = context;
}

From source file:org.jacpfx.spring.launcher.SpringXmlConfigLauncher.java

@Override
public synchronized <T> T registerAndGetBean(Class<? extends T> type, final String id, final Scope scope) {
    if (contains(id))
        return getBean(id);
    final AutowireCapableBeanFactory factory = getContext().getAutowireCapableBeanFactory();
    final BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
    final GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(type);//  w w w  .j  av  a2s.c om
    if (scope != null)
        beanDefinition.setScope(scope.getType());
    beanDefinition.setAutowireCandidate(true);
    registry.registerBeanDefinition(id, beanDefinition);
    factory.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    return getBean(id);
}

From source file:org.springmodules.validation.valang.parser.DefaultVisitor.java

private Function lifeCycleCallbacks(Function function, int line, int column) {
    if (function instanceof BeanFactoryAware) {
        ((BeanFactoryAware) function).setBeanFactory(beanFactory);
    }/*from  w  w w . j  av  a  2 s  .  c  om*/
    if (function instanceof ApplicationContextAware) {
        ((ApplicationContextAware) function).setApplicationContext(applicationContext);
    }
    if (function instanceof ResourceLoaderAware) {
        ((ResourceLoaderAware) function).setResourceLoader(resourceLoader);
    }
    if (function instanceof MessageSourceAware) {
        ((MessageSourceAware) function).setMessageSource(messageSource);
    }
    if (function instanceof ApplicationEventPublisherAware) {
        ((ApplicationEventPublisherAware) function).setApplicationEventPublisher(applicationEventPublisher);
    }
    if (function instanceof ServletContextAware) {
        ((ServletContextAware) function).setServletContext(servletContext);
    }
    if (function instanceof AbstractFunction) {
        AbstractFunction abstractFunction = (AbstractFunction) function;
        AutowireCapableBeanFactory autowireCapableBeanFactory = null;

        if (abstractFunction.isAutowireByName() || abstractFunction.isAutowireByType()) {
            if (applicationContext instanceof ConfigurableApplicationContext) {
                ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
                autowireCapableBeanFactory = configurableApplicationContext.getBeanFactory();
            } else if (beanFactory instanceof AutowireCapableBeanFactory) {
                autowireCapableBeanFactory = (AutowireCapableBeanFactory) beanFactory;
            } else if (applicationContext == null && beanFactory == null) {
                throw new ValangException(
                        "Could not autowire function: no application context or bean factory available", line,
                        column);
            } else {
                throw new ValangException(
                        "Could not autowire function: application context or bean factory does not support autowiring",
                        line, column);
            }
        }
        if (abstractFunction.isAutowireByName()) {
            autowireCapableBeanFactory.autowireBeanProperties(function,
                    AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
        }
        if (abstractFunction.isAutowireByType()) {
            autowireCapableBeanFactory.autowireBeanProperties(function,
                    AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
        }
        try {
            abstractFunction.init();
        } catch (Exception e) {
            throw new ValangException("Error initializing function", e, line, column);
        }
    }
    return function;
}

From source file:org.apache.usergrid.tools.ToolBase.java

public void startSpring() {

    // copy("/testApplicationContext.xml", TMP);
    String[] locations = { "toolsApplicationContext.xml" };
    ApplicationContext ac = new ClassPathXmlApplicationContext(locations);

    AutowireCapableBeanFactory acbf = ac.getAutowireCapableBeanFactory();
    acbf.autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    acbf.initializeBean(this, "testClient");

    assertNotNull(emf);/*from   w ww .  j  av  a2  s  .  c  o  m*/
    assertTrue("EntityManagerFactory is instance of EntityManagerFactoryImpl",
            emf instanceof EntityManagerFactoryImpl);
}

From source file:org.codehaus.groovy.grails.plugins.web.api.ControllersApi.java

/**
 * Initializes a command object.//from  www  .  j  av a  2  s  .c om
 *
 * If type is a domain class and the request body or parameters include an id, the id is used to retrieve
 * the command object instance from the database, otherwise the no-arg constructor on type is invoke.  If
 * an attempt is made to retrieve the command object instance from the database and no corresponding
 * record is found, null is returned.
 *
 * The command object is then subjected to data binding and dependency injection before being returned.
 *
 *
 * @param controllerInstance The controller instance
 * @param type The type of the command object
 * @return the initialized command object or null if the command object is a domain class, the body or
 * parameters included an id and no corresponding record was found in the database.
 */
public Object initializeCommandObject(final Object controllerInstance, final Class type) throws Exception {
    final HttpServletRequest request = getRequest(controllerInstance);
    final DataBindingSource dataBindingSource = DataBindingUtils
            .createDataBindingSource(getGrailsApplication(controllerInstance), type, request);
    final DataBindingSource commandObjectBindingSource = WebMetaUtils.getCommandObjectBindingSource(type,
            dataBindingSource);
    final Object commandObjectInstance;
    Object entityIdentifierValue = null;
    if (DomainClassArtefactHandler.isDomainClass(type)) {
        entityIdentifierValue = commandObjectBindingSource.getIdentifierValue();
        if (entityIdentifierValue == null) {
            final GrailsWebRequest webRequest = GrailsWebRequest.lookup(request);
            entityIdentifierValue = webRequest != null ? webRequest.getParams().getIdentifier() : null;
        }
    }
    if (entityIdentifierValue != null) {
        commandObjectInstance = InvokerHelper.invokeStaticMethod(type, "get", entityIdentifierValue);
    } else {
        commandObjectInstance = type.newInstance();
    }

    if (commandObjectInstance != null) {
        final boolean shouldDoDataBinding;

        if (entityIdentifierValue != null) {
            final HttpMethod requestMethod = HttpMethod.valueOf(request.getMethod());
            switch (requestMethod) {
            case PATCH:
            case POST:
            case PUT:
                shouldDoDataBinding = true;
                break;
            default:
                shouldDoDataBinding = false;
            }
        } else {
            shouldDoDataBinding = true;
        }

        if (shouldDoDataBinding) {
            bindData(controllerInstance, commandObjectInstance, commandObjectBindingSource,
                    Collections.EMPTY_MAP, null);
        }

        final ApplicationContext applicationContext = getApplicationContext(controllerInstance);
        final AutowireCapableBeanFactory autowireCapableBeanFactory = applicationContext
                .getAutowireCapableBeanFactory();
        autowireCapableBeanFactory.autowireBeanProperties(commandObjectInstance,
                AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    }
    return commandObjectInstance;
}

From source file:org.openspaces.remoting.SpaceRemotingServiceExporter.java

private void autowireArguments(Object service, Object[] args) {
    if (disableAutowiredArguments) {
        return;//from w  w w .j  a v a 2 s  .co  m
    }
    if (args == null) {
        return;
    }
    if (shouldAutowire(service)) {
        for (Object arg : args) {
            if (arg == null) {
                continue;
            }
            AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
            beanFactory.autowireBeanProperties(arg, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
            beanFactory.initializeBean(arg, arg.getClass().getName());
        }
    }
}