Example usage for org.springframework.context ApplicationContextInitializer initialize

List of usage examples for org.springframework.context ApplicationContextInitializer initialize

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContextInitializer initialize.

Prototype

void initialize(C applicationContext);

Source Link

Document

Initialize the given application context.

Usage

From source file:org.wso2.msf4j.spring.MSF4JSpringApplication.java

private void applyInitializers(ConfigurableApplicationContext context) {
    for (ApplicationContextInitializer initializer : getInitializers()) {
        Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(),
                ApplicationContextInitializer.class);
        Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
        initializer.initialize(context);
    }/*from   w w  w  .j a  v a2 s.  com*/
}

From source file:org.jasig.springframework.web.portlet.context.PortletContextLoader.java

/**
 * Customize the {@link ConfigurablePortletApplicationContext} created by this
 * PortletContextLoader after config locations have been supplied to the context
 * but before the context is <em>refreshed</em>.
 * <p>The default implementation {@linkplain #determineContextInitializerClasses(PortletContext)
 * determines} what (if any) context initializer classes have been specified through
 * {@linkplain #CONTEXT_INITIALIZER_CLASSES_PARAM context init parameters} and
 * {@linkplain ApplicationContextInitializer#initialize invokes each} with the
 * given web application context./*from  w w w  . j a  va 2  s  .  c om*/
 * <p>Any {@code ApplicationContextInitializers} implementing
 * {@link org.springframework.core.Ordered Ordered} or marked with @{@link
 * org.springframework.core.annotation.Order Order} will be sorted appropriately.
 * @param portletContext the current portlet context
 * @param applicationContext the newly created application context
 * @see #createPortletApplicationContext(PortletContext)
 * @see #CONTEXT_INITIALIZER_CLASSES_PARAM
 * @see ApplicationContextInitializer#initialize(ConfigurableApplicationContext)
 */
protected void customizeContext(PortletContext portletContext,
        ConfigurablePortletApplicationContext applicationContext) {
    List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> initializerClasses = determineContextInitializerClasses(
            portletContext);

    if (initializerClasses.size() == 0) {
        // no ApplicationContextInitializers have been declared -> nothing to do
        return;
    }

    ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerInstances = new ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>>();

    for (Class<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerClass : initializerClasses) {
        Class<?> contextClass = applicationContext.getClass();
        Class<?> initializerContextClass = GenericTypeResolver.resolveTypeArgument(initializerClass,
                ApplicationContextInitializer.class);
        Assert.isAssignable(initializerContextClass, contextClass,
                String.format(
                        "Could not add context initializer [%s] as its generic parameter [%s] "
                                + "is not assignable from the type of application context used by this "
                                + "context loader [%s]",
                        initializerClass.getName(), initializerContextClass, contextClass));
        initializerInstances.add(BeanUtils.instantiateClass(initializerClass));
    }

    //TODO remove cast when ContribXmlPortletApplicationContext is merged into super classes
    ((ConfigurablePortletEnvironment) applicationContext.getEnvironment())
            .initPropertySources(this.servletContext, portletContext, null);

    Collections.sort(initializerInstances, new AnnotationAwareOrderComparator());
    for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : initializerInstances) {
        initializer.initialize(applicationContext);
    }
}

From source file:com.dhcc.framework.web.context.DhccContextLoader.java

/**
 * Customize the {@link ConfigurableWebApplicationContext} created by this
 * ContextLoader after config locations have been supplied to the context
 * but before the context is <em>refreshed</em>.
 * <p>The default implementation {@linkplain #determineContextInitializerClasses(ServletContext)
 * determines} what (if any) context initializer classes have been specified through
 * {@linkplain #CONTEXT_INITIALIZER_CLASSES_PARAM context init parameters} and
 * {@linkplain ApplicationContextInitializer#initialize invokes each} with the
 * given web application context.// ww  w. j  ava 2  s  .  co  m
 * <p>Any {@code ApplicationContextInitializers} implementing
 * {@link org.springframework.core.Ordered Ordered} or marked with @{@link
 * org.springframework.core.annotation.Order Order} will be sorted appropriately.
 * @param servletContext the current servlet context
 * @param applicationContext the newly created application context
 * @see #createWebApplicationContext(ServletContext, ApplicationContext)
 * @see #CONTEXT_INITIALIZER_CLASSES_PARAM
 * @see ApplicationContextInitializer#initialize(ConfigurableApplicationContext)
 */
protected void customizeContext(ServletContext servletContext,
        ConfigurableWebApplicationContext applicationContext) {
    List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> initializerClasses = determineContextInitializerClasses(
            servletContext);
    if (initializerClasses.size() == 0) {
        // no ApplicationContextInitializers have been declared -> nothing to do
        return;
    }

    Class<?> contextClass = applicationContext.getClass();
    ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerInstances = new ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>>();

    for (Class<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerClass : initializerClasses) {
        Class<?> initializerContextClass = GenericTypeResolver.resolveTypeArgument(initializerClass,
                ApplicationContextInitializer.class);
        if (initializerContextClass != null) {
            Assert.isAssignable(initializerContextClass, contextClass,
                    String.format(
                            "Could not add context initializer [%s] as its generic parameter [%s] "
                                    + "is not assignable from the type of application context used by this "
                                    + "context loader [%s]: ",
                            initializerClass.getName(), initializerContextClass.getName(),
                            contextClass.getName()));
        }
        initializerInstances.add(BeanUtils.instantiateClass(initializerClass));
    }

    ConfigurableEnvironment env = applicationContext.getEnvironment();
    if (env instanceof ConfigurableWebEnvironment) {
        ((ConfigurableWebEnvironment) env).initPropertySources(servletContext, null);
    }

    Collections.sort(initializerInstances, new AnnotationAwareOrderComparator());
    for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : initializerInstances) {
        initializer.initialize(applicationContext);
    }
}

From source file:org.springframework.boot.SpringApplication.java

/**
 * Apply any {@link ApplicationContextInitializer}s to the context before it is
 * refreshed.//from  www  .ja  va2 s .  c o m
 * @param context the configured ApplicationContext (not refreshed yet)
 * @see ConfigurableApplicationContext#refresh()
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void applyInitializers(ConfigurableApplicationContext context) {
    for (ApplicationContextInitializer initializer : getInitializers()) {
        Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(),
                ApplicationContextInitializer.class);
        Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
        initializer.initialize(context);
    }
}

From source file:org.springframework.test.context.support.AbstractContextLoader.java

@SuppressWarnings("unchecked")
private void invokeApplicationContextInitializers(ConfigurableApplicationContext context,
        MergedContextConfiguration mergedConfig) {

    Set<Class<? extends ApplicationContextInitializer<?>>> initializerClasses = mergedConfig
            .getContextInitializerClasses();
    if (initializerClasses.isEmpty()) {
        // no ApplicationContextInitializers have been declared -> nothing to do
        return;/*from  w w w  .j a v  a  2s .c  o m*/
    }

    List<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerInstances = new ArrayList<>();
    Class<?> contextClass = context.getClass();

    for (Class<? extends ApplicationContextInitializer<?>> initializerClass : initializerClasses) {
        Class<?> initializerContextClass = GenericTypeResolver.resolveTypeArgument(initializerClass,
                ApplicationContextInitializer.class);
        if (initializerContextClass != null && !initializerContextClass.isInstance(context)) {
            throw new ApplicationContextException(String.format(
                    "Could not apply context initializer [%s] since its generic parameter [%s] "
                            + "is not assignable from the type of application context used by this "
                            + "context loader: [%s]",
                    initializerClass.getName(), initializerContextClass.getName(), contextClass.getName()));
        }
        initializerInstances.add((ApplicationContextInitializer<ConfigurableApplicationContext>) BeanUtils
                .instantiateClass(initializerClass));
    }

    AnnotationAwareOrderComparator.sort(initializerInstances);
    for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : initializerInstances) {
        initializer.initialize(context);
    }
}

From source file:org.springframework.web.context.ContextLoader.java

/**
 * Customize the {@link ConfigurableWebApplicationContext} created by this
 * ContextLoader after config locations have been supplied to the context
 * but before the context is <em>refreshed</em>.
 * <p>The default implementation {@linkplain #determineContextInitializerClasses(ServletContext)
 * determines} what (if any) context initializer classes have been specified through
 * {@linkplain #CONTEXT_INITIALIZER_CLASSES_PARAM context init parameters} and
 * {@linkplain ApplicationContextInitializer#initialize invokes each} with the
 * given web application context./*w w w  .j  a  va2  s. c  o  m*/
 * <p>Any {@code ApplicationContextInitializers} implementing
 * {@link org.springframework.core.Ordered Ordered} or marked with @{@link
 * org.springframework.core.annotation.Order Order} will be sorted appropriately.
 * @param sc the current servlet context
 * @param wac the newly created application context
 * @see #CONTEXT_INITIALIZER_CLASSES_PARAM
 * @see ApplicationContextInitializer#initialize(ConfigurableApplicationContext)
 */
protected void customizeContext(ServletContext sc, ConfigurableWebApplicationContext wac) {
    List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> initializerClasses = determineContextInitializerClasses(
            sc);

    for (Class<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerClass : initializerClasses) {
        Class<?> initializerContextClass = GenericTypeResolver.resolveTypeArgument(initializerClass,
                ApplicationContextInitializer.class);
        if (initializerContextClass != null && !initializerContextClass.isInstance(wac)) {
            throw new ApplicationContextException(String.format(
                    "Could not apply context initializer [%s] since its generic parameter [%s] "
                            + "is not assignable from the type of application context used by this "
                            + "context loader: [%s]",
                    initializerClass.getName(), initializerContextClass.getName(), wac.getClass().getName()));
        }
        this.contextInitializers.add(BeanUtils.instantiateClass(initializerClass));
    }

    AnnotationAwareOrderComparator.sort(this.contextInitializers);
    for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : this.contextInitializers) {
        initializer.initialize(wac);
    }
}