Example usage for org.springframework.beans.factory.config ConfigurableListableBeanFactory registerResolvableDependency

List of usage examples for org.springframework.beans.factory.config ConfigurableListableBeanFactory registerResolvableDependency

Introduction

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

Prototype

void registerResolvableDependency(Class<?> dependencyType, @Nullable Object autowiredValue);

Source Link

Document

Register a special dependency type with corresponding autowired value.

Usage

From source file:net.solarnetwork.web.gemini.ServerOsgiBundleXmlWebApplicationContext.java

/**
 * Register request/session scopes, a {@link ServletContextAwareProcessor},
 * etc./*w w w . j  a  v a2s  . co  m*/
 * 
 * @see WebApplicationContextUtils#registerWebApplicationScopes(ConfigurableListableBeanFactory)
 */
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    super.postProcessBeanFactory(beanFactory);

    // Drive the kernel's bean factory post processors.
    BundleContext bundleContext = getBundleContext();
    if (bundleContext != null) {
        ServiceReference<OsgiBeanFactoryPostProcessor> sr = bundleContext
                .getServiceReference(OsgiBeanFactoryPostProcessor.class);
        if (sr != null) {
            OsgiBeanFactoryPostProcessor kernelPostProcessor = bundleContext.getService(sr);
            try {
                kernelPostProcessor.postProcessBeanFactory(bundleContext, beanFactory);
            } catch (Exception e) {
                throw new ApplicationContextException("Kernel bean factory post processor failed", e);
            } finally {
                bundleContext.ungetService(sr);
            }
        }
    }

    beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(getServletContext(), getServletConfig()));
    beanFactory.ignoreDependencyInterface(ServletContextAware.class);
    beanFactory.ignoreDependencyInterface(ServletConfigAware.class);
    beanFactory.registerResolvableDependency(ServletContext.class, getServletContext());
    beanFactory.registerResolvableDependency(ServletConfig.class, getServletConfig());

    WebApplicationContextUtils.registerWebApplicationScopes(beanFactory);
}

From source file:org.springframework.context.support.AbstractApplicationContext.java

/**
 * Configure the factory's standard context characteristics,
 * such as the context's ClassLoader and post-processors.
 * @param beanFactory the BeanFactory to configure
 *//*ww w.ja v  a 2  s. c  o  m*/
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    // Tell the internal bean factory to use the context's class loader etc.
    beanFactory.setBeanClassLoader(getClassLoader());
    beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
    beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

    // Configure the bean factory with context callbacks.
    beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
    beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
    beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
    beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
    beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
    beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
    beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

    // BeanFactory interface not registered as resolvable type in a plain factory.
    // MessageSource registered (and found for autowiring) as a bean.
    beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
    beanFactory.registerResolvableDependency(ResourceLoader.class, this);
    beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
    beanFactory.registerResolvableDependency(ApplicationContext.class, this);

    // Register early post-processor for detecting inner beans as ApplicationListeners.
    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

    // Detect a LoadTimeWeaver and prepare for weaving, if found.
    if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
        beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
        // Set a temporary ClassLoader for type matching.
        beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
    }

    // Register default environment beans.
    if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
        beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
    }
    if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
        beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
    }
    if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
        beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
    }
}

From source file:org.springframework.test.context.web.ServletTestExecutionListener.java

private void setUpRequestContextIfNecessary(TestContext testContext) {
    if (!isActivated(testContext) || alreadyPopulatedRequestContextHolder(testContext)) {
        return;//from  ww w. ja v  a 2  s .c om
    }

    ApplicationContext context = testContext.getApplicationContext();

    if (context instanceof WebApplicationContext) {
        WebApplicationContext wac = (WebApplicationContext) context;
        ServletContext servletContext = wac.getServletContext();
        Assert.state(servletContext instanceof MockServletContext, () -> String.format(
                "The WebApplicationContext for test context %s must be configured with a MockServletContext.",
                testContext));

        if (logger.isDebugEnabled()) {
            logger.debug(String.format(
                    "Setting up MockHttpServletRequest, MockHttpServletResponse, ServletWebRequest, and RequestContextHolder for test context %s.",
                    testContext));
        }

        MockServletContext mockServletContext = (MockServletContext) servletContext;
        MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext);
        request.setAttribute(CREATED_BY_THE_TESTCONTEXT_FRAMEWORK, Boolean.TRUE);
        MockHttpServletResponse response = new MockHttpServletResponse();
        ServletWebRequest servletWebRequest = new ServletWebRequest(request, response);

        RequestContextHolder.setRequestAttributes(servletWebRequest);
        testContext.setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
        testContext.setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);

        if (wac instanceof ConfigurableApplicationContext) {
            @SuppressWarnings("resource")
            ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) wac;
            ConfigurableListableBeanFactory bf = configurableApplicationContext.getBeanFactory();
            bf.registerResolvableDependency(MockHttpServletResponse.class, response);
            bf.registerResolvableDependency(ServletWebRequest.class, servletWebRequest);
        }
    }
}

From source file:org.springframework.test.context.web.WebTestExecutionListener.java

/**
 * TODO [SPR-9864] Document setUpRequestContext().
 *
 * @param testContext/*w  w w  .j  a  v a2  s  .com*/
 * @param servletContext
 */
private void setUpRequestContextIfNecessary(TestContext testContext) {

    ApplicationContext context = testContext.getApplicationContext();

    if (context instanceof WebApplicationContext) {
        WebApplicationContext wac = (WebApplicationContext) context;
        ServletContext servletContext = wac.getServletContext();
        if (!(servletContext instanceof MockServletContext)) {
            throw new IllegalStateException(String.format(
                    "The WebApplicationContext for test context %s must be configured with a MockServletContext.",
                    testContext));
        }

        if (logger.isDebugEnabled()) {
            logger.debug(String.format(
                    "Setting up MockHttpServletRequest, MockHttpServletResponse, ServletWebRequest, and RequestContextHolder for test context %s.",
                    testContext));
        }

        if (RequestContextHolder.getRequestAttributes() == null) {
            MockServletContext mockServletContext = (MockServletContext) servletContext;
            MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext);
            MockHttpServletResponse response = new MockHttpServletResponse();
            ServletWebRequest servletWebRequest = new ServletWebRequest(request, response);

            RequestContextHolder.setRequestAttributes(servletWebRequest);

            if (wac instanceof ConfigurableApplicationContext) {
                ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) wac;
                ConfigurableListableBeanFactory bf = configurableApplicationContext.getBeanFactory();
                bf.registerResolvableDependency(MockHttpServletResponse.class, response);
                bf.registerResolvableDependency(ServletWebRequest.class, servletWebRequest);
            }
        }
    }
}