下面列出了org.springframework.context.annotation.AnnotationConfigApplicationContext#register ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
@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();
}
@Before
public void init() {
context = new AnnotationConfigApplicationContext();
context.register(InMemoryMessageLogTestConfiguration.class);
TestPropertyValues.of(
"synapse.receiver.default-headers.enabled=false"
).applyTo(context);
}
@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"));
}
@Override
protected HttpHandler createHttpHandler() {
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
wac.register(TestConfiguration.class);
wac.refresh();
return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build();
}
@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"));
}
@Before
public void setup() {
artifactVersion = getEnvironment().getProperty("artifactVersion");
context = new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("yarn");
context.register(TestYarnConfiguration.class);
context.setParent(getApplicationContext());
context.refresh();
}
private AnnotationConfigApplicationContext load(Class<?> config, String... env) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
TestPropertyValues.of(env).applyTo(context);
context.register(config);
context.refresh();
return context;
}
@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));
}
@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));
}
@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"));
}
}
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(DemoServiceConsumerBootstrap.class);
context.refresh();
System.in.read();
context.close();
}
@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"));
}
/**
* 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;
}
@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));
}
@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());
}
@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"));
}
@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"));
}
/** {@inheritDoc} */
@Override protected void beforeTestsStarted() throws Exception {
super.beforeTestsStarted();
ctx = new AnnotationConfigApplicationContext();
ctx.register(ApplicationConfiguration.class);
ctx.refresh();
repo = ctx.getBean(PersonExpressionRepository.class);
}
@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));
}