Example usage for org.springframework.context.annotation AnnotationConfigUtils registerAnnotationConfigProcessors

List of usage examples for org.springframework.context.annotation AnnotationConfigUtils registerAnnotationConfigProcessors

Introduction

In this page you can find the example usage for org.springframework.context.annotation AnnotationConfigUtils registerAnnotationConfigProcessors.

Prototype

public static void registerAnnotationConfigProcessors(BeanDefinitionRegistry registry) 

Source Link

Document

Register all relevant annotation post processors in the given registry.

Usage

From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java

@Test
public void systemPropertiesSecurityManager() {
    GenericApplicationContext ac = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);

    GenericBeanDefinition bd = new GenericBeanDefinition();
    bd.setBeanClass(TestBean.class);
    bd.getPropertyValues().add("country", "#{systemProperties.country}");
    ac.registerBeanDefinition("tb", bd);

    SecurityManager oldSecurityManager = System.getSecurityManager();
    try {//  ww w. j a v a2 s . c  om
        System.setProperty("country", "NL");

        SecurityManager securityManager = new SecurityManager() {
            @Override
            public void checkPropertiesAccess() {
                throw new AccessControlException("Not Allowed");
            }

            @Override
            public void checkPermission(Permission perm) {
                // allow everything else
            }
        };
        System.setSecurityManager(securityManager);
        ac.refresh();

        TestBean tb = ac.getBean("tb", TestBean.class);
        assertEquals("NL", tb.getCountry());

    } finally {
        System.setSecurityManager(oldSecurityManager);
        System.getProperties().remove("country");
    }
}

From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java

@Test
public void stringConcatenationWithDebugLogging() {
    GenericApplicationContext ac = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);

    GenericBeanDefinition bd = new GenericBeanDefinition();
    bd.setBeanClass(String.class);
    bd.getConstructorArgumentValues()/*from  w  w w.j a  va  2s.  co  m*/
            .addGenericArgumentValue("test-#{ T(java.lang.System).currentTimeMillis() }");
    ac.registerBeanDefinition("str", bd);
    ac.refresh();

    String str = ac.getBean("str", String.class);
    assertTrue(str.startsWith("test-"));
}

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

/**
 * Load a Spring ApplicationContext from the supplied {@link MergedContextConfiguration}.
 *
 * <p>Implementation details://from   w w  w. j  a  v a2 s . co m
 *
 * <ul>
 * <li>Calls {@link #validateMergedContextConfiguration(MergedContextConfiguration)}
 * to allow subclasses to validate the supplied configuration before proceeding.</li>
 * <li>Creates a {@link GenericApplicationContext} instance.</li>
 * <li>If the supplied {@code MergedContextConfiguration} references a
 * {@linkplain MergedContextConfiguration#getParent() parent configuration},
 * the corresponding {@link MergedContextConfiguration#getParentApplicationContext()
 * ApplicationContext} will be retrieved and
 * {@linkplain GenericApplicationContext#setParent(ApplicationContext) set as the parent}
 * for the context created by this method.</li>
 * <li>Calls {@link #prepareContext(GenericApplicationContext)} for backwards
 * compatibility with the {@link org.springframework.test.context.ContextLoader
 * ContextLoader} SPI.</li>
 * <li>Calls {@link #prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)}
 * to allow for customizing the context before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory(DefaultListableBeanFactory)} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #loadBeanDefinitions(GenericApplicationContext, MergedContextConfiguration)}
 * to populate the context from the locations or classes in the supplied
 * {@code MergedContextConfiguration}.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@link AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext(GenericApplicationContext)} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>Calls {@link #customizeContext(ConfigurableApplicationContext, MergedContextConfiguration)} to
 * allow for customizing the context before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 *
 * @return a new application context
 * @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration)
 * @see GenericApplicationContext
 * @since 3.1
 */
@Override
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig)
        throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Loading ApplicationContext for merged context configuration [%s].",
                mergedConfig));
    }

    validateMergedContextConfiguration(mergedConfig);

    GenericApplicationContext context = new GenericApplicationContext();

    ApplicationContext parent = mergedConfig.getParentApplicationContext();
    if (parent != null) {
        context.setParent(parent);
    }
    prepareContext(context);
    prepareContext(context, mergedConfig);
    customizeBeanFactory(context.getDefaultListableBeanFactory());
    loadBeanDefinitions(context, mergedConfig);
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    customizeContext(context);
    customizeContext(context, mergedConfig);
    context.refresh();
    context.registerShutdownHook();
    return context;
}

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

/**
 * Load a Spring ApplicationContext from the supplied {@code locations}.
 *
 * <p>Implementation details:/*from   ww  w .  j  a  v a  2s.c om*/
 *
 * <ul>
 * <li>Creates a {@link GenericApplicationContext} instance.</li>
 * <li>Calls {@link #prepareContext(GenericApplicationContext)} to allow for customizing the context
 * before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory(DefaultListableBeanFactory)} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #createBeanDefinitionReader(GenericApplicationContext)} to create a
 * {@link BeanDefinitionReader} which is then used to populate the context
 * from the specified locations.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@link AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext(GenericApplicationContext)} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 *
 * <p><b>Note</b>: this method does not provide a means to set active bean definition
 * profiles for the loaded context. See {@link #loadContext(MergedContextConfiguration)}
 * and {@link AbstractContextLoader#prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)}
 * for an alternative.
 *
 * @return a new application context
 * @see org.springframework.test.context.ContextLoader#loadContext
 * @see GenericApplicationContext
 * @see #loadContext(MergedContextConfiguration)
 * @since 2.5
 */
@Override
public final ConfigurableApplicationContext loadContext(String... locations) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Loading ApplicationContext for locations [%s].",
                StringUtils.arrayToCommaDelimitedString(locations)));
    }
    GenericApplicationContext context = new GenericApplicationContext();
    prepareContext(context);
    customizeBeanFactory(context.getDefaultListableBeanFactory());
    createBeanDefinitionReader(context).loadBeanDefinitions(locations);
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    customizeContext(context);
    context.refresh();
    context.registerShutdownHook();
    return context;
}

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

/**
 * TODO [SPR-9864] Document overridden loadContext(MergedContextConfiguration).
 *
 * @see org.springframework.test.context.SmartContextLoader#loadContext(org.springframework.test.context.MergedContextConfiguration)
 *///from w w  w.  j ava  2 s.  co  m
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig)
        throws Exception {

    if (!(mergedConfig instanceof WebMergedContextConfiguration)) {
        throw new IllegalArgumentException(
                String.format(
                        "Cannot load WebApplicationContext from non-web merged context configuration %s. "
                                + "Consider annotating your test class with @WebAppConfiguration.",
                        mergedConfig));
    }
    WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig;

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Loading WebApplicationContext for merged context configuration %s.",
                webMergedConfig));
    }

    GenericWebApplicationContext context = new GenericWebApplicationContext();
    configureWebResources(context, webMergedConfig);
    prepareContext(context, webMergedConfig);
    customizeBeanFactory(context.getDefaultListableBeanFactory(), webMergedConfig);
    loadBeanDefinitions(context, webMergedConfig);
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    customizeContext(context, webMergedConfig);
    context.refresh();
    context.registerShutdownHook();
    return context;
}

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

public ApplicationContext loadContext(String... locations) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("Loading ApplicationContext for locations ["
                + StringUtils.arrayToCommaDelimitedString(locations) + "].");
    }//from w  w  w. j a  va2  s . c  o m
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    prepareContext(context);
    customizeBeanFactory(context.getDefaultListableBeanFactory());
    createBeanDefinitionReader(context).loadBeanDefinitions(locations);
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    customizeContext(context);
    context.refresh();
    context.registerShutdownHook();
    return context;
}

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

public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception {
    GenericWebApplicationContext context = new GenericWebApplicationContext();

    // Commented out until SPR-10392 is fixed and we can upgrade to Spring 3.2.3.RELEASE or higher
    //      ApplicationContext parent = config.getParentApplicationContext();
    //      if(parent != null) {
    //         context.setParent(parent);
    //      }//from  w w w.  jav a  2  s  .  com

    prepareContext(context);
    prepareContext(context, config);
    customizeBeanFactory(context.getDefaultListableBeanFactory());
    createBeanDefinitionReader(context).loadBeanDefinitions(config.getLocations());
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    customizeContext(context);
    context.refresh();
    context.registerShutdownHook();

    return context;
}

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

/**
 * Load a Spring {@link WebApplicationContext} from the supplied
 * {@link MergedContextConfiguration}.//from  www  .j a  v  a2s.c  om
 * <p>Implementation details:
 * <ul>
 * <li>Calls {@link #validateMergedContextConfiguration(WebMergedContextConfiguration)}
 * to allow subclasses to validate the supplied configuration before proceeding.</li>
 * <li>Creates a {@link GenericWebApplicationContext} instance.</li>
 * <li>If the supplied {@code MergedContextConfiguration} references a
 * {@linkplain MergedContextConfiguration#getParent() parent configuration},
 * the corresponding {@link MergedContextConfiguration#getParentApplicationContext()
 * ApplicationContext} will be retrieved and
 * {@linkplain GenericWebApplicationContext#setParent(ApplicationContext) set as the parent}
 * for the context created by this method.</li>
 * <li>Delegates to {@link #configureWebResources} to create the
 * {@link MockServletContext} and set it in the {@code WebApplicationContext}.</li>
 * <li>Calls {@link #prepareContext} to allow for customizing the context
 * before bean definitions are loaded.</li>
 * <li>Calls {@link #customizeBeanFactory} to allow for customizing the
 * context's {@code DefaultListableBeanFactory}.</li>
 * <li>Delegates to {@link #loadBeanDefinitions} to populate the context
 * from the locations or classes in the supplied {@code MergedContextConfiguration}.</li>
 * <li>Delegates to {@link AnnotationConfigUtils} for
 * {@linkplain AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
 * annotation configuration processors.</li>
 * <li>Calls {@link #customizeContext} to allow for customizing the context
 * before it is refreshed.</li>
 * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
 * context and registers a JVM shutdown hook for it.</li>
 * </ul>
 * @return a new web application context
 * @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration)
 * @see GenericWebApplicationContext
 */
@Override
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig)
        throws Exception {
    Assert.isTrue(mergedConfig instanceof WebMergedContextConfiguration,
            () -> String
                    .format("Cannot load WebApplicationContext from non-web merged context configuration %s. "
                            + "Consider annotating your test class with @WebAppConfiguration.", mergedConfig));

    WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig;

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Loading WebApplicationContext for merged context configuration %s.",
                webMergedConfig));
    }

    validateMergedContextConfiguration(webMergedConfig);

    GenericWebApplicationContext context = new GenericWebApplicationContext();

    ApplicationContext parent = mergedConfig.getParentApplicationContext();
    if (parent != null) {
        context.setParent(parent);
    }
    configureWebResources(context, webMergedConfig);
    prepareContext(context, webMergedConfig);
    customizeBeanFactory(context.getDefaultListableBeanFactory(), webMergedConfig);
    loadBeanDefinitions(context, webMergedConfig);
    AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
    customizeContext(context, webMergedConfig);
    context.refresh();
    context.registerShutdownHook();
    return context;
}

From source file:ubic.gemma.web.util.WebContextLoader.java

@Override
public ApplicationContext loadContext(String... locations) {
    if (WebContextLoader.logger.isDebugEnabled()) {
        WebContextLoader.logger.debug("Loading WebApplicationContext for locations ["
                + StringUtils.arrayToCommaDelimitedString(locations) + "].");
    }/*from   ww  w . j a  v  a2  s . c o m*/
    ConfigurableWebApplicationContext context = new XmlWebApplicationContext();
    context.setConfigLocations(locations);
    context.setServletContext(new MockServletContext(""));
    context.refresh();

    AnnotationConfigUtils.registerAnnotationConfigProcessors((BeanDefinitionRegistry) context.getBeanFactory());

    context.registerShutdownHook();
    return context;
}