Java Code Examples for org.springframework.context.annotation.AnnotationConfigApplicationContext#register()

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#register() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
void shouldResolvePlaceholderForRenewablePropertySource() throws Exception {

	System.setProperty("my_property", "renewable");

	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

	ctx.register(Config.class);
	ctx.register(RenewableConfig.class);
	ctx.refresh();

	SecretLeaseContainer leaseContainerMock = ctx.getBean(SecretLeaseContainer.class);
	verify(leaseContainerMock).afterPropertiesSet();
	verify(leaseContainerMock).addLeaseListener(any());
	verify(leaseContainerMock).addErrorListener(any());
	verify(leaseContainerMock).addRequestedSecret(RequestedSecret.renewable("foo/renewable"));
	verifyNoMoreInteractions(leaseContainerMock);
}
 
Example 2
Source Project: spring-analysis-note   File: EnableAsyncTests.java    License: MIT License 6 votes vote down vote up
@Test
public void customExecutorConfigWithThrowsException() {
	// Arrange
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(CustomExecutorConfig.class);
	ctx.refresh();
	AsyncBean asyncBean = ctx.getBean(AsyncBean.class);
	Method method = ReflectionUtils.findMethod(AsyncBean.class, "fail");
	TestableAsyncUncaughtExceptionHandler exceptionHandler =
			(TestableAsyncUncaughtExceptionHandler) ctx.getBean("exceptionHandler");
	assertFalse("handler should not have been called yet", exceptionHandler.isCalled());
	// Act
	asyncBean.fail();
	// Assert
	Awaitility.await()
				.atMost(500, TimeUnit.MILLISECONDS)
				.pollInterval(10, TimeUnit.MILLISECONDS)
				.untilAsserted(() -> exceptionHandler.assertCalledWith(method, UnsupportedOperationException.class));
	ctx.close();
}
 
Example 3
@Before
public void init() {
    context = new AnnotationConfigApplicationContext();
    context.register(InMemoryMessageLogTestConfiguration.class);
    TestPropertyValues.of(
            "synapse.receiver.default-headers.enabled=false"
    ).applyTo(context);

}
 
Example 4
@Test
public void testAppJarCanBeCustomized() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(context, "spark.app-class: Dummy");
    EnvironmentTestUtils.addEnvironment(context, "spark.app-jar: my-app-jar-0.0.1.jar");
    context.register(Conf.class);
    context.refresh();
    SparkClientTaskProperties properties = context.getBean(SparkClientTaskProperties.class);
    assertThat(properties.getAppJar(), equalTo("my-app-jar-0.0.1.jar"));
}
 
Example 5
@Override
protected HttpHandler createHttpHandler() {
	AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
	wac.register(TestConfiguration.class);
	wac.refresh();
	return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build();
}
 
Example 6
@Test
public void importFromBean() throws Exception {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ImportFromBean.class);
	ctx.refresh();
	assertThat(ctx.containsBean("importAnnotationDetectionTests.ImportFromBean"), is(true));
	assertThat(ctx.containsBean("testBean1"), is(true));
	assertThat(ctx.getBean("testBean1", TestBean.class).getName(), is("1"));
}
 
Example 7
@Before
public void setup() {
	artifactVersion = getEnvironment().getProperty("artifactVersion");
	context = new AnnotationConfigApplicationContext();
	context.getEnvironment().setActiveProfiles("yarn");
	context.register(TestYarnConfiguration.class);
	context.setParent(getApplicationContext());
	context.refresh();
}
 
Example 8
private AnnotationConfigApplicationContext load(Class<?> config, String... env) {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	TestPropertyValues.of(env).applyTo(context);
	context.register(config);
	context.refresh();
	return context;
}
 
Example 9
@Test
public void testAppStatusPollIntervalCanBeCustomized() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(context, "spark.app-class: Dummy");
    EnvironmentTestUtils.addEnvironment(context, "spark.app-jar: dummy.jar");
    EnvironmentTestUtils.addEnvironment(context, "spark.rest-url: spark://dummy:6066");
    EnvironmentTestUtils.addEnvironment(context, "spark.app-status-poll-interval: 20000");
    context.register(Conf.class);
    context.refresh();
    SparkClusterTaskProperties properties = context.getBean(SparkClusterTaskProperties.class);
    assertThat(properties.getAppStatusPollInterval(), equalTo(20000L));
}
 
Example 10
@Test
public void asyncProcessorIsOrderedLowestPrecedenceByDefault() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(AsyncConfig.class);
	ctx.refresh();

	AsyncAnnotationBeanPostProcessor bpp = ctx.getBean(AsyncAnnotationBeanPostProcessor.class);
	assertThat(bpp.getOrder(), is(Ordered.LOWEST_PRECEDENCE));
}
 
Example 11
@Test
public void repositoryIsNotTxProxy() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(Config.class);
	ctx.refresh();

	try {
		assertTxProxying(ctx);
		fail("expected exception");
	} catch (AssertionError ex) {
		assertThat(ex.getMessage(), equalTo("FooRepository is not a TX proxy"));
	}
}
 
Example 12
public static void main(String[] args) throws IOException {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(DemoServiceConsumerBootstrap.class);
    context.refresh();
    System.in.read();
    context.close();
}
 
Example 13
@Test
public void fileExtensionCanBeCustomized() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, "hdfs.fileExtension:test");
	context.register(Conf.class);
	context.refresh();
	HdfsSinkProperties properties = context.getBean(HdfsSinkProperties.class);
	assertThat(properties.getFileExtension(), equalTo("test"));
}
 
Example 14
Source Project: herd   File: DataBridgeApp.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates and returns the Spring application context.
 *
 * @return the application context
 */
protected ApplicationContext createApplicationContext()
{
    // Create the Spring application context and register the JavaConfig classes we need.
    // We will use core (in case it's needed), the service aspect that times the duration of the service method calls, and our specific beans defined in
    // the data bridge configuration. We're not including full service and DAO configurations because they come with database/data source dependencies
    // that we don't need and don't want (i.e. we don't want the database to be running as a pre-requisite for running the uploader).
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    ApplicationContextHolder.setApplicationContext(applicationContext);
    applicationContext.register(CoreSpringModuleConfig.class, DataBridgeSpringModuleConfig.class, DataBridgeAopSpringModuleConfig.class,
        DataBridgeEnvSpringModuleConfig.class);
    applicationContext.refresh();
    return applicationContext;
}
 
Example 15
@Test
public void responseSizeCanBeCustomized() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	TestPropertyValues.of(
			"tensorflow.image.recognition.labels:/remote",
			"tensorflow.image.recognition.responseSize:5").applyTo(context);
	context.register(Conf.class);
	context.refresh();
	ImageRecognitionProcessorProperties properties = context.getBean(ImageRecognitionProcessorProperties.class);
	assertThat(properties.getResponseSize(), equalTo(5));
}
 
Example 16
@Test
public void preserveTimestampDirCanBeDisabled() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, "ftp.preserveTimestamp:false");
	context.register(Conf.class);
	context.refresh();
	FtpSourceProperties properties = context.getBean(FtpSourceProperties.class);
	assertTrue(!properties.isPreserveTimestamp());
}
 
Example 17
@Test
@SuppressWarnings("resource")
public void valueFieldsAreProcessedWhenPlaceholderConfigurerIsSegregated() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ConfigWithValueField.class);
	ctx.register(ConfigWithPlaceholderConfigurer.class);
	System.setProperty("test.name", "foo");
	ctx.refresh();
	System.clearProperty("test.name");

	TestBean testBean = ctx.getBean(TestBean.class);
	assertThat(testBean.getName(), equalTo("foo"));
}
 
Example 18
@Test
public void testLoadFromFactory() {
	context = new AnnotationConfigApplicationContext();
	context.register(Config1.class);
	context.refresh();

	ControlFile cf = context.getBean(ControlFile.class);
	assertThat(cf.getGploadOutputTable(), is("test"));
	assertThat(cf.getGploadInputDelimiter(), is(','));
	assertThat(cf.getDatabase(), is("gpadmin"));
	assertThat(cf.getUser(), is("gpadmin"));
	assertThat(cf.getHost(), is("mdw.example.org"));
	assertThat(cf.getPort(), is(5432));
	assertThat(cf.getPassword(), nullValue());

	assertThat(cf.getGploadOutputMode(), is(OutputMode.UPDATE));

	assertThat(cf.getGploadOutputMatchColumns(), notNullValue());
	assertThat(cf.getGploadOutputMatchColumns().size(), is(2));
	assertThat(cf.getGploadOutputMatchColumns().get(0), is("col11"));
	assertThat(cf.getGploadOutputMatchColumns().get(1), is("col12"));

	assertThat(cf.getGploadOutputUpdateColumns(), notNullValue());
	assertThat(cf.getGploadOutputUpdateColumns().size(), is(2));
	assertThat(cf.getGploadOutputUpdateColumns().get(0), is("col21"));
	assertThat(cf.getGploadOutputUpdateColumns().get(1), is("col22"));
	assertThat(cf.getGploadOutputUpdateCondition(), is("condition"));

	assertThat(cf.getGploadSqlBefore().get(0), is("select 1 as before"));
	assertThat(cf.getGploadSqlBefore().get(1), is("select 2 as before"));
	assertThat(cf.getGploadSqlAfter().get(0), is("select 1 as after"));
	assertThat(cf.getGploadSqlAfter().get(1), is("select 2 as after"));
}
 
Example 19
/** {@inheritDoc} */
@Override protected void beforeTestsStarted() throws Exception {
    super.beforeTestsStarted();

    ctx = new AnnotationConfigApplicationContext();
    ctx.register(ApplicationConfiguration.class);
    ctx.refresh();

    repo = ctx.getBean(PersonExpressionRepository.class);
}
 
Example 20
@Test
public void annotationConfigApplicationContext_withProdEnvAndProdConfigClass() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

	assertHasStandardEnvironment(ctx);
	ctx.setEnvironment(prodEnv);

	ctx.register(ProdConfig.class);
	ctx.refresh();

	assertThat("should have prod bean", ctx.containsBean(PROD_BEAN_NAME), is(true));
}