Example usage for org.springframework.context ApplicationContext getAutowireCapableBeanFactory

List of usage examples for org.springframework.context ApplicationContext getAutowireCapableBeanFactory

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getAutowireCapableBeanFactory.

Prototype

AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;

Source Link

Document

Expose AutowireCapableBeanFactory functionality for this context.

Usage

From source file:io.gravitee.management.idp.core.plugin.impl.IdentityProviderManagerImpl.java

private <T> T create(Plugin plugin, Class<T> identityClass, Map<String, Object> properties) {
    if (identityClass == null) {
        return null;
    }/*from w  w w  . j av a  2s.c o  m*/

    try {
        T identityObj = createInstance(identityClass);
        final Import annImport = identityClass.getAnnotation(Import.class);
        Set<Class<?>> configurations = (annImport != null) ? new HashSet<>(Arrays.asList(annImport.value()))
                : Collections.emptySet();

        ApplicationContext idpApplicationContext = pluginContextFactory
                .create(new AnnotationBasedPluginContextConfigurer(plugin) {
                    @Override
                    public Set<Class<?>> configurations() {
                        return configurations;
                    }

                    @Override
                    public ConfigurableEnvironment environment() {
                        return new StandardEnvironment() {
                            @Override
                            protected void customizePropertySources(MutablePropertySources propertySources) {
                                propertySources.addFirst(new MapPropertySource(plugin.id(), properties));
                                super.customizePropertySources(propertySources);
                            }
                        };
                    }
                });

        idpApplicationContext.getAutowireCapableBeanFactory().autowireBean(identityObj);

        return identityObj;
    } catch (Exception ex) {
        LOGGER.error("An unexpected error occurs while loading identity provider", ex);
        return null;
    }
}

From source file:org.sakaiproject.signup.tool.entityproviders.SignupEntityProducer.java

public void init() {
    if (log.isDebugEnabled())
        log.debug("signup EP.init()");
    try {/*from www  .  j  av  a2s.c  o m*/
        entityManager.registerEntityProducer(this, REFERENCE_ROOT);
        log.info("Registered kaltura entity producer as: " + REFERENCE_ROOT);

        // get the main sakai AC (it will be the parent of our AC)
        ApplicationContext sakaiAC = applicationContext.getParent();
        if (sakaiAC != null && sakaiAC instanceof ConfigurableApplicationContext) {
            // only ConfigurableApplicationContext - or higher - can register singletons
            Object currentKEP = ComponentManager.get(SignupEntityProducer.class.getName());
            // check if something is already registered
            if (currentKEP != null) {
                log.info("Found existing " + SignupEntityProducer.class.getName() + " in the ComponentManager: "
                        + currentKEP);
                // attempt to unregister the existing bean (otherwise the register call will fail)
                try {
                    // only DefaultListableBeanFactory - or higher - can unregister singletons
                    DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) sakaiAC
                            .getAutowireCapableBeanFactory();
                    dlbf.destroySingleton(SignupEntityProducer.class.getName());
                    log.info("Removed existing " + SignupEntityProducer.class.getName()
                            + " from the ComponentManager");
                } catch (Exception e) {
                    log.warn("FAILED attempted removal of signup bean: " + e);
                }
            }
            // register this EP with the sakai AC
            ((ConfigurableApplicationContext) sakaiAC).getBeanFactory()
                    .registerSingleton(SignupEntityProducer.class.getName(), this);
        }
        // now verify if we are good to go
        if (ComponentManager.get(SignupEntityProducer.class.getName()) != null) {
            log.info("Found " + SignupEntityProducer.class.getName() + " in the ComponentManager");
        } else {
            log.warn("FAILED to insert and lookup " + SignupEntityProducer.class.getName()
                    + " in the Sakai ComponentManager, archive imports for signup will not work");
        }
    } catch (Exception ex) {
        log.warn("signup EP.init(): " + ex, ex);
    }
    this.copyFileProcessor = new CopyFileProcessor(getSakaiFacade(), getSignupMeetingService());
}

From source file:com.mmnaseri.dragonfly.runtime.analysis.ApplicationDesignAdvisor.java

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
    reportIssues(analyze());/*from   w ww .  j  a  va2 s. c o  m*/
    final BeanDefinitionRegistry definitionRegistry = (BeanDefinitionRegistry) applicationContext
            .getAutowireCapableBeanFactory();
    definitionRegistry.removeBeanDefinition(beanName);
}

From source file:com.mystudy.source.spring.mvc.DispatcherServlet.java

/**
 * Create a default strategy.//from  ww w .  j  a  v  a2  s .c  om
 * <p>The default implementation uses {@link org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean}.
 * @param context the current WebApplicationContext
 * @param clazz the strategy implementation class to instantiate
 * @return the fully configured strategy instance
 * @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory()
 * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory#createBean
 */
protected Object createDefaultStrategy(ApplicationContext context, Class<?> clazz) {
    return context.getAutowireCapableBeanFactory().createBean(clazz);
}

From source file:de.unioninvestment.eai.portal.portlet.crud.CrudUI.java

private void autowireUiDependencies(VaadinRequest request) {
    ApplicationContext context = getSpringContext(request);
    if (context != null) {
        context.getAutowireCapableBeanFactory().autowireBean(this);
    }// w ww .j a  v  a  2 s  .  co m
}

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 w w.  j a v a  2s  .  c  om*/
    assertTrue("EntityManagerFactory is instance of EntityManagerFactoryImpl",
            emf instanceof EntityManagerFactoryImpl);
}

From source file:org.broadleafcommerce.core.web.catalog.taglib.GoogleAnalyticsTag.java

@Override
public void doTag() throws JspException, IOException {
    JspWriter out = getJspContext().getOut();

    if (this.webPropertyId == null) {
        ServletContext sc = ((PageContext) getJspContext()).getServletContext();
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(sc);
        context.getAutowireCapableBeanFactory().autowireBeanProperties(this,
                AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    }/*www  .ja  va  2 s.c  o  m*/

    String webPropertyId = getWebPropertyId();

    if (webPropertyId.equals("UA-XXXXXXX-X")) {
        LOG.warn(
                "googleAnalytics.webPropertyId has not been overridden in a custom property file. Please set this in order to properly use the Google Analytics tag");
    }

    out.println(analytics(webPropertyId, order));
    super.doTag();
}

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

/**
 * Constructor used by controllers/*from www  . j ava2s.c o  m*/
 *
 * @param instance The instance
 */
public static void initialize(Object instance) {
    ApplicationContext applicationContext = getStaticApplicationContext();
    if (applicationContext == null) {
        return;
    }

    applicationContext.getAutowireCapableBeanFactory().autowireBeanProperties(instance,
            AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);

    if (Environment.getCurrent() == Environment.TEST) {
        GrailsWebRequest webRequest = GrailsWebRequest.lookup();
        if (webRequest != null) {
            webRequest.setControllerName(GrailsNameUtils.getLogicalPropertyName(instance.getClass().getName(),
                    ControllerArtefactHandler.TYPE));
        }
    }
}

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

/**
 * Initializes a command object./*ww w  .  j av a 2s . com*/
 *
 * 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.grails.plugins.AbstractGrailsPluginManager.java

/**
 * Base implementation that simply goes through the list of plugins and calls doWithRuntimeConfiguration on each
 * @param springConfig The RuntimeSpringConfiguration instance
 *///from  w  w w  . ja  v a2 s .  c o  m
public void doRuntimeConfiguration(RuntimeSpringConfiguration springConfig) {
    ApplicationContext context = springConfig.getUnrefreshedApplicationContext();
    AutowireCapableBeanFactory autowireCapableBeanFactory = context.getAutowireCapableBeanFactory();
    if (autowireCapableBeanFactory instanceof ConfigurableListableBeanFactory) {
        ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) autowireCapableBeanFactory;
        ConversionService existingConversionService = beanFactory.getConversionService();
        ConverterRegistry converterRegistry;
        if (existingConversionService == null) {
            GenericConversionService conversionService = new GenericConversionService();
            converterRegistry = conversionService;
            beanFactory.setConversionService(conversionService);
        } else {
            converterRegistry = (ConverterRegistry) existingConversionService;
        }

        converterRegistry.addConverter(
                new Converter<GrailsApplication, org.codehaus.groovy.grails.commons.GrailsApplication>() {
                    @Override
                    public org.codehaus.groovy.grails.commons.GrailsApplication convert(
                            GrailsApplication source) {
                        return new LegacyGrailsApplication(source);
                    }
                });
        converterRegistry.addConverter(new Converter<NavigableMap.NullSafeNavigator, Object>() {
            @Override
            public Object convert(NavigableMap.NullSafeNavigator source) {
                return null;
            }
        });
    }
    checkInitialised();
    for (GrailsPlugin plugin : pluginList) {
        if (plugin.supportsCurrentScopeAndEnvironment()
                && plugin.isEnabled(context.getEnvironment().getActiveProfiles())) {
            plugin.doWithRuntimeConfiguration(springConfig);
        }
    }
}