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

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#refresh() . 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: SpringMBeanServerBeanBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    // 注册当前 Class
    context.register(SpringMBeanServerBeanBootstrap.class);
    // 启动应用上下文
    context.refresh();
    // 获取名为 "mBeanExporter" Bean,来自于 mBeanExporter() 方法 @Bean 定义
    MBeanExporter mBeanExporter = context.getBean("mBeanExporter", MBeanExporter.class);
    // 获取名为 "mBeanServer" Bean,来自于 mBeanServer() 方法 @Bean 定义
    MBeanServer mBeanServer = context.getBean("mBeanServer", MBeanServer.class);
    // 从 "mBeanExporter" Bean 中获取来自于其afterPropertiesSet()方法创建 "mBeanServer" 对象
    MBeanServer mBeanServerFromMBeanExporter = mBeanExporter.getServer();
    System.out.printf("来自 mBeanExporter Bean 关联的 MBeanServer 实例是否等于平台 MBeanServer : %b \n",
            mBeanServerFromMBeanExporter == ManagementFactory.getPlatformMBeanServer()
    );

    System.out.printf("来自 mBeanExporter Bean 关联的 MBeanServer 实例是否等于 mBeanServer Bean : %b \n",
            mBeanServerFromMBeanExporter == mBeanServer
    );
    // 关闭应用上下文
    context.close();
}
 
Example 2
Source File: EnableHelloWorldBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    // 构建 Annotation 配置驱动 Spring 上下文
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    // 注册 当前引导类(被 @Configuration 标注) 到 Spring 上下文
    context.register(EnableHelloWorldBootstrap.class);
    // 启动上下文
    context.refresh();
    // 获取名称为 "helloWorld" Bean 对象
    String helloWorld = context.getBean("helloWorld", String.class);
    // 输出用户名称:"Hello,World"
    System.out.printf("helloWorld = %s \n", helloWorld);
    // 关闭上下文
    context.close();
}
 
Example 3
Source File: DatasetSinkPropertiesTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Test
public void batchSizeCanBeCustomized() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, "hdfs.dataset.batchSize:1000");
	context.register(Conf.class);
	context.refresh();
	HdfsDatasetSinkProperties properties = context.getBean(HdfsDatasetSinkProperties.class);
	assertThat(properties.getBatchSize(), equalTo(1000));
}
 
Example 4
Source File: SftpSinkPropertiesTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Test
public void autoCreateDirCanBeDisabled() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, "sftp.autoCreateDir:false");
	context.register(Conf.class);
	context.refresh();
	SftpSinkProperties properties = context.getBean(SftpSinkProperties.class);
	assertTrue(!properties.isAutoCreateDir());
}
 
Example 5
Source File: ScheduledAndTransactionalAnnotationIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void succeedsWhenJdkProxyAndScheduledMethodIsPresentOnInterface() throws InterruptedException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(Config.class, JdkProxyTxConfig.class, RepoConfigB.class);
	ctx.refresh();

	Thread.sleep(50); // allow @Scheduled method to be called several times

	MyRepositoryWithScheduledMethod repository = ctx.getBean(MyRepositoryWithScheduledMethod.class);
	CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
	assertThat("repository is not a proxy", AopUtils.isAopProxy(repository), is(true));
	assertThat("@Scheduled method never called", repository.getInvocationCount(), greaterThan(0));
	assertThat("no transactions were committed", txManager.commits, greaterThan(0));
}
 
Example 6
Source File: FtpSinkPropertiesTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Test
public void tmpFileRemoteDirCanBeCustomized() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, "ftp.temporaryRemoteDir:/foo");
	context.register(Conf.class);
	context.refresh();
	FtpSinkProperties properties = context.getBean(FtpSinkProperties.class);
	assertThat(properties.getTemporaryRemoteDir(), equalTo("/foo"));
}
 
Example 7
Source File: ScheduledAndTransactionalAnnotationIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void succeedsWhenJdkProxyAndScheduledMethodIsPresentOnInterface() throws InterruptedException {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(Config.class, JdkProxyTxConfig.class, RepoConfigB.class);
	ctx.refresh();

	Thread.sleep(100);  // allow @Scheduled method to be called several times

	MyRepositoryWithScheduledMethod repository = ctx.getBean(MyRepositoryWithScheduledMethod.class);
	CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
	assertThat("repository is not a proxy", AopUtils.isJdkDynamicProxy(repository), is(true));
	assertThat("@Scheduled method never called", repository.getInvocationCount(), greaterThan(0));
	assertThat("no transactions were committed", txManager.commits, greaterThan(0));
}
 
Example 8
Source File: IgniteSpringDataCrudSelfTest.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(PersonRepository.class);
    repoWithCompoundKey = ctx.getBean(PersonRepositoryWithCompoundKey.class);
    ignite = ctx.getBean(IgniteEx.class);
}
 
Example 9
Source File: ProxyAnnotationDiscoveryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void nonAnnotatedService_PTC_true() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(PTCTrue.class, AnnotatedServiceImpl.class);
	ctx.refresh();
	NonAnnotatedService s = ctx.getBean(NonAnnotatedService.class);
	assertTrue("expected a subclass proxy", AopUtils.isCglibProxy(s));
	assertThat(s, instanceOf(AnnotatedServiceImpl.class));
}
 
Example 10
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 11
Source File: Spr12233Tests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void spr12233() throws Exception {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(PropertySourcesPlaceholderConfigurer.class);
	ctx.register(ImportConfiguration.class);
	ctx.refresh();
	ctx.close();
}
 
Example 12
Source File: ImportAnnotationDetectionTests.java    From spring-analysis-note with MIT License 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 13
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"));
}
 
Example 14
Source File: HttpInvokerFactoryBeanIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void withConfigurationClassWithPlainFactoryBean() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.register(ConfigWithPlainFactoryBean.class);
	context.refresh();
	MyBean myBean = context.getBean("myBean", MyBean.class);
	assertSame(context.getBean("myService"), myBean.myService);
	myBean.myService.handle();
	myBean.myService.handleAsync();
}
 
Example 15
Source File: AmazonS3SinkPropertiesTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Test
public void aclCanBeCustomized() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, "s3.bucket:foo", "s3.acl:AuthenticatedRead");
	context.register(Conf.class);
	context.refresh();
	AmazonS3SinkProperties properties = context.getBean(AmazonS3SinkProperties.class);
	assertThat(properties.getAcl(), equalTo(CannedAccessControlList.AuthenticatedRead));
	context.close();
}
 
Example 16
Source File: SessionAttributeMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
@SuppressWarnings("resource")
public void setup() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();
	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new SessionAttributeMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);
	this.session = mock(WebSession.class);
	this.exchange = MockServerWebExchange.builder(MockServerHttpRequest.get("/")).session(this.session).build();
	this.handleMethod = ReflectionUtils.findMethod(getClass(), "handleWithSessionAttribute", (Class<?>[]) null);
}
 
Example 17
Source File: HttpInvokerFactoryBeanIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("resource")
public void testNonLoadedConfigClass() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.registerBeanDefinition("config", new RootBeanDefinition(InvokerAutowiringConfig.class.getName()));
	context.refresh();
	MyBean myBean = context.getBean("myBean", MyBean.class);
	assertSame(context.getBean("myService"), myBean.myService);
	myBean.myService.handle();
	myBean.myService.handleAsync();
}
 
Example 18
Source File: BeanInstantiationExceptionDemo.java    From geekbang-lessons with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    // 创建 BeanFactory 容器
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

    // 注册 BeanDefinition Bean Class 是一个 CharSequence 接口
    BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(CharSequence.class);
    applicationContext.registerBeanDefinition("errorBean", beanDefinitionBuilder.getBeanDefinition());

    // 启动应用上下文
    applicationContext.refresh();

    // 关闭应用上下文
    applicationContext.close();
}
 
Example 19
Source File: SftpSinkPropertiesTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Test
public void remoteDirCanBeCustomized() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	EnvironmentTestUtils.addEnvironment(context, "sftp.remoteDir:/remote");
	context.register(Conf.class);
	context.refresh();
	SftpSinkProperties properties = context.getBean(SftpSinkProperties.class);
	assertThat(properties.getRemoteDir(), equalTo("/remote"));
}
 
Example 20
Source File: NacosPropertySourcePostProcessorTest.java    From nacos-spring-project with Apache License 2.0 3 votes vote down vote up
@Test
public void testRelativeOrder() throws NacosException {

	AnnotationConfigApplicationContext context = createContext(DATA_ID, DEFAULT_GROUP,
			TEST_CONTENT);

	context.register(RelativeOrderNacosPropertySource.class);

	context.refresh();

	ConfigurableEnvironment environment = context.getEnvironment();

	PropertySource propertySource = environment.getPropertySources().get("before");

	// Java System Properties before Nacos Properties
	String systemProperty = System.getProperty(TEST_PROPERTY_NAME);
	String propertyValue = environment.getProperty(TEST_PROPERTY_NAME);

	Assert.assertEquals(systemProperty, propertyValue);
	Assert.assertNotNull(TEST_PROPERTY_VALUE, propertyValue);

	// Environment Variables after Nacos Properties
	String path = System.getenv().get("PATH");
	propertyValue = environment.getProperty("PATH");

	Assert.assertNotNull(path, propertyValue);
	Assert.assertEquals("/My/Path", propertyValue);
}