org.springframework.context.annotation.AnnotationConfigApplicationContext#refresh ( )源码实例Demo

下面列出了org.springframework.context.annotation.AnnotationConfigApplicationContext#refresh ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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();
}
 
@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));
}
 
@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"));
}
 
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();
}
 
@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();
}
 
@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);
}
 
@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();
}
 
@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();
}
 
@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"));
}
 
@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"));
}
 
源代码11 项目: spring4-understanding   文件: Spr12233Tests.java
@Test
public void spr12233() throws Exception {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(PropertySourcesPlaceholderConfigurer.class);
	ctx.register(ImportConfiguration.class);
	ctx.refresh();
	ctx.close();
}
 
@Override
protected HttpHandler createHttpHandler() {
	AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
	wac.register(TestConfiguration.class);
	wac.refresh();
	return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build();
}
 
@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));
}
 
源代码14 项目: ignite   文件: IgniteSpringDataCrudSelfTest.java
/** {@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);
}
 
@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));
}
 
@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"));
}
 
@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));
}
 
@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());
}
 
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();
}
 
@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);
}