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

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#register() . 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
Source File: VaultPropertySourceUnitTests.java    From spring-vault with Apache License 2.0 6 votes vote down vote up
@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 File: EnableAsyncTests.java    From spring-analysis-note with 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
Source File: DemoServiceConsumerBootstrap.java    From dubbo-registry-nacos with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(DemoServiceConsumerBootstrap.class);
    context.refresh();
    System.in.read();
    context.close();
}
 
Example 4
Source File: EnvironmentSystemIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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));
}
 
Example 5
Source File: IgniteSpringDataCrudSelfExpressionTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override protected void beforeTestsStarted() throws Exception {
    super.beforeTestsStarted();

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

    repo = ctx.getBean(PersonExpressionRepository.class);
}
 
Example 6
Source File: ControlFileTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@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 7
Source File: ConfigurationClassWithPlaceholderConfigurerBeanTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 8
Source File: FtpSourcePropertiesTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: ImageRecognitionProcessorPropertiesTests.java    From tensorflow with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: DataBridgeApp.java    From herd with 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 11
Source File: HdfsSinkPropertiesTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: JournaledEventSourceTest.java    From synapse with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
    context = new AnnotationConfigApplicationContext();
    context.register(InMemoryMessageLogTestConfiguration.class);
    TestPropertyValues.of(
            "synapse.receiver.default-headers.enabled=false"
    ).applyTo(context);

}
 
Example 13
Source File: EnableTransactionManagementIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 14
Source File: EnableAsyncTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 15
Source File: SparkClusterTaskPropertiesTests.java    From spring-cloud-task-app-starters with Apache License 2.0 5 votes vote down vote up
@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 16
Source File: OnOAuth2ClientCredentialsEnabledTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
private AnnotationConfigApplicationContext load(Class<?> config, String... env) {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	TestPropertyValues.of(env).applyTo(context);
	context.register(config);
	context.refresh();
	return context;
}
 
Example 17
Source File: AppDeployerIT.java    From spring-cloud-deployer-yarn with Apache License 2.0 5 votes vote down vote up
@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 18
Source File: ImportAnnotationDetectionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 19
Source File: MultipartIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected HttpHandler createHttpHandler() {
	AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
	wac.register(TestConfiguration.class);
	wac.refresh();
	return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build();
}
 
Example 20
Source File: SparkClientTaskPropertiesTests.java    From spring-cloud-task-app-starters with Apache License 2.0 5 votes vote down vote up
@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"));
}