/** * Register the specified configuration classes with the {@link ApplicationContext}. * @param configurations the configurations to add * @return a new instance with the updated configuration */ public SELF withConfiguration(Configurations configurations) { Assert.notNull(configurations, "Configurations must not be null"); return newInstance(this.contextFactory, this.initializers, this.environmentProperties, this.systemProperties, this.classLoader, this.parent, add(this.configurations, configurations)); }
/** * Create and refresh a new {@link ApplicationContext} based on the current state of * this loader. The context is consumed by the specified {@code consumer} and closed * upon completion. * @param consumer the consumer of the created {@link ApplicationContext} * @return this instance */ @SuppressWarnings("unchecked") public SELF run(ContextConsumer<? super A> consumer) { withContextClassLoader(this.classLoader, () -> { this.systemProperties.applyToSystemProperties(() -> { try (A context = createAssertableContext()) { accept(consumer, context); } return null; }); }); return (SELF) this; }
/** * Configure the {@link ConfigurableApplicationContext#setParent(ApplicationContext) * parent} of the {@link ApplicationContext}. * @param parent the parent * @return a new instance with the updated parent */ public SELF withParent(ApplicationContext parent) { return newInstance(this.contextFactory, this.initializers, this.environmentProperties, this.systemProperties, this.classLoader, parent, this.configurations); }
private C createAndLoadContext() { C context = this.contextFactory.get(); try { configureContext(context); return context; } catch (RuntimeException ex) { context.close(); throw ex; } }
private void accept(ContextConsumer<? super A> consumer, A context) { try { consumer.accept(context); } catch (Throwable ex) { rethrow(ex); } }
/** * Register the specified user configuration classes with the * {@link ApplicationContext}. * @param configurationClasses the user configuration classes to add * @return a new instance with the updated configuration */ public SELF withUserConfiguration(Class<?>... configurationClasses) { return withConfiguration(UserConfigurations.of(configurationClasses)); }
/** * Customize the {@link ClassLoader} that the {@link ApplicationContext} should use * for resource loading and bean class loading. * @param classLoader the classloader to use (can be null to use the default) * @return a new instance with the updated class loader * @see FilteredClassLoader */ public SELF withClassLoader(ClassLoader classLoader) { return newInstance(this.contextFactory, this.initializers, this.environmentProperties, this.systemProperties, classLoader, this.parent, this.configurations); }
/** * Add a {@link ApplicationContextInitializer} to be called when the context is * created. * @param initializer the initializer to add * @return a new instance with the updated initializers */ public SELF withInitializer( ApplicationContextInitializer<? super ConfigurableApplicationContext> initializer) { Assert.notNull(initializer, "Initializer must not be null"); return newInstance(this.contextFactory, add(this.initializers, initializer), this.environmentProperties, this.systemProperties, this.classLoader, this.parent, this.configurations); }
/** * Add the specified {@link Environment} property pairs. Key-value pairs can be * specified with colon (":") or equals ("=") separators. Override matching keys that * might have been specified previously. * @param pairs the key-value pairs for properties that need to be added to the * environment * @return a new instance with the updated property values * @see TestPropertyValues * @see #withSystemProperties(String...) */ public SELF withPropertyValues(String... pairs) { return newInstance(this.contextFactory, this.initializers, this.environmentProperties.and(pairs), this.systemProperties, this.classLoader, this.parent, this.configurations); }
/** * Add the specified {@link System} property pairs. Key-value pairs can be specified * with colon (":") or equals ("=") separators. System properties are added before the * context is {@link #run(ContextConsumer) run} and restored when the context is * closed. * @param pairs the key-value pairs for properties that need to be added to the system * @return a new instance with the updated system properties * @see TestPropertyValues * @see #withSystemProperties(String...) */ public SELF withSystemProperties(String... pairs) { return newInstance(this.contextFactory, this.initializers, this.environmentProperties, this.systemProperties.and(pairs), this.classLoader, this.parent, this.configurations); }