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

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

Introduction

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

Prototype

Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;

Source Link

Document

Fully create a new bean instance of the given class with the specified autowire strategy.

Usage

From source file:io.acme.solution.application.conf.CommandHandlerUtils.java

public static Map<String, CommandHandler> buildCommandHandlersRegistry(final String basePackage,
        final ApplicationContext context) {

    final Map<String, CommandHandler> registry = new HashMap<>();
    final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
            false);/*from   w w w  . j av  a 2  s .c  om*/
    final AutowireCapableBeanFactory beanFactory = context.getAutowireCapableBeanFactory();
    scanner.addIncludeFilter(new AssignableTypeFilter(CommandHandler.class));

    CommandHandler currentHandler = null;

    for (BeanDefinition bean : scanner.findCandidateComponents(basePackage)) {
        currentHandler = (CommandHandler) beanFactory.createBean(
                ClassUtils.resolveClassName(bean.getBeanClassName(), context.getClassLoader()),
                AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
        registry.put(currentHandler.getInterest(), currentHandler);
    }

    return registry;
}

From source file:com.alliander.osgp.acceptancetests.SpringInstantiationStrategy.java

@Override
public InstantiationState instantiate(final Class<?> markedClass, final Object parameter)
        throws InvocationTargetException, InstantiationException, IllegalAccessException {

    final InstantiationStateCreator creator = new InstantiationStateCreator();

    if (markedClass.isAnnotationPresent(Configurable.class)) {
        final AutowireCapableBeanFactory factory = this.applicationContext.getAutowireCapableBeanFactory();
        final Object object = factory.createBean(markedClass, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME,
                true);/*from   ww  w.  j  a v a 2 s.c om*/
        return creator.didInstantiate(object);
    }

    return creator.didNotInstantiate();
}

From source file:com.zenika.stripes.contrib.spring.SpringNameBasedActionResolver.java

/**
 * Helper method to construct and return a new ActionBean instance. Called whenever a new
 * instance needs to be manufactured. Provides a convenient point for subclasses to add
 * specific behaviour during action bean creation.
 * //w  w w.  j  ava 2  s.  co  m
 * ActionBean are instancied from the Spring factory AutowireCapableBeanFactory.
 * @see <a href="http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-beans/3.0.2.RELEASE/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java#AbstractAutowireCapableBeanFactory.createBean%28java.lang.Class%29">createBean code</a>
 *
 * @param type the type of ActionBean to create
 * @param context the current ActionBeanContext
 * @return the new ActionBean instance
 * @throws Exception if anything goes wrong!
 */
@Override
protected ActionBean makeNewActionBean(Class<? extends ActionBean> type, ActionBeanContext context) {
    ServletContext servletContext = StripesFilter.getConfiguration().getServletContext();
    ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);

    AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
    ActionBean actionBean = (ActionBean) beanFactory.createBean(type, AutowireCapableBeanFactory.AUTOWIRE_NO,
            false);

    return actionBean;
}

From source file:com.zenika.stripes.contrib.spring.SpringRuntimeConfiguration.java

/**
 * Splits a comma-separated list of class names and maps each {@link LifecycleStage} to the
 * interceptors in the list that intercept it. Also automatically finds Interceptors in
 * packages listed in {@link BootstrapPropertyResolver#PACKAGES} if searchExtensionPackages is true.
 * /*  w ww.j a v  a 2  s  . c  o  m*/
 * Interceptors are instancied from the Spring factory AutowireCapableBeanFactory.
 * @see <a href="http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-beans/3.0.2.RELEASE/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java#AbstractAutowireCapableBeanFactory.createBean%28java.lang.Class%29">createBean code</a>
 * 
 * @return a Map of {@link LifecycleStage} to Collection of {@link Interceptor}
 */
@Override
protected Map<LifecycleStage, Collection<Interceptor>> initInterceptors(List classes) {

    Map<LifecycleStage, Collection<Interceptor>> map = new HashMap<LifecycleStage, Collection<Interceptor>>();

    for (Object type : classes) {
        try {
            ServletContext servletContext = getServletContext();
            ApplicationContext applicationContext = WebApplicationContextUtils
                    .getWebApplicationContext(servletContext);

            AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
            Interceptor interceptor = (Interceptor) beanFactory.createBean((Class) type,
                    AutowireCapableBeanFactory.AUTOWIRE_NO, false);
            addInterceptor(map, interceptor);
        } catch (Exception e) {
            throw new StripesRuntimeException(
                    "Could not instantiate configured Interceptor [" + type.getClass().getName() + "].", e);
        }
    }

    return map;
}

From source file:grails.plugin.viewmodels.constructor.Autowirer.java

public void autowire(Object instance) {
    AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
    beanFactory.autowireBeanProperties(instance, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    beanFactory.initializeBean(instance, instance.getClass().getName());

    AutowiredAnnotationBeanPostProcessor annotationProcessor = (AutowiredAnnotationBeanPostProcessor) beanFactory
            .createBean(AutowiredAnnotationBeanPostProcessor.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME,
                    false);/*from   w w  w  . j  a  va  2s .com*/

    annotationProcessor.processInjection(instance);

    if (instance instanceof ApplicationContextAware) {
        ((ApplicationContextAware) instance).setApplicationContext(applicationContext);
    }
}

From source file:nl.strohalm.cyclos.services.permissions.PermissionServiceImpl.java

@Override
public PermissionCatalogHandler getPermissionCatalogHandler(final Group group) {
    final AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
    try {//from   w ww  .jav  a2s . c  o m
        PermissionCatalogHandlerImpl handler = (PermissionCatalogHandlerImpl) factory.createBean(
                PermissionCatalogHandlerImpl.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
        handler.load(group);
        return handler;
    } catch (Exception e) {
        throw new PermissionCatalogInitializationException(group.getNature(), e.getMessage(), e);
    }
}