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

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

@Test
public void repro() {
	ConfigurableApplicationContext parent = new GenericApplicationContext();
	parent.refresh();

	AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
	child.setParent(parent);
	child.refresh();

	ConfigurableEnvironment env = child.getBean(ConfigurableEnvironment.class);
	assertThat("unknown env", env, anyOf(
			sameInstance(parent.getEnvironment()),
			sameInstance(child.getEnvironment())));
	assertThat("expected child ctx env", env, sameInstance(child.getEnvironment()));

	child.close();
	parent.close();
}
 
public static void main(String[] args) {
    // 1. 创建 parent Spring 应用上下文
    AnnotationConfigApplicationContext parentContext = new AnnotationConfigApplicationContext();
    parentContext.setId("parent-context");
    // 注册 MyListener 到 parent Spring 应用上下文
    parentContext.register(MyListener.class);

    // 2. 创建 current Spring 应用上下文
    AnnotationConfigApplicationContext currentContext = new AnnotationConfigApplicationContext();
    currentContext.setId("current-context");
    // 3. current -> parent
    currentContext.setParent(parentContext);
    // 注册 MyListener 到 current Spring 应用上下文
    currentContext.register(MyListener.class);

    // 4.启动 parent Spring 应用上下文
    parentContext.refresh();

    // 5.启动 current Spring 应用上下文
    currentContext.refresh();

    // 关闭所有 Spring 应用上下文
    currentContext.close();
    parentContext.close();
}
 
@Test
public void repro() {
	ConfigurableApplicationContext parent = new GenericApplicationContext();
	parent.refresh();

	AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
	child.setParent(parent);
	child.refresh();

	ConfigurableEnvironment env = child.getBean(ConfigurableEnvironment.class);
	assertThat("unknown env", env, anyOf(
			sameInstance(parent.getEnvironment()),
			sameInstance(child.getEnvironment())));
	assertThat("expected child ctx env", env, sameInstance(child.getEnvironment()));

	child.close();
	parent.close();
}
 
private ConfigurableApplicationContext createContext(int levels, boolean listenersAsBean) {

        if (levels < 1) {
            return null;
        }

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        if (listenersAsBean) {
            context.register(MyContextEventListener.class);
        } else {
            context.addApplicationListener(new MyContextEventListener(context));
        }

        context.setParent(createContext(levels - 1, listenersAsBean));

        context.refresh();

        return context;
    }
 
@Test
public void repro() {
	ConfigurableApplicationContext parent = new GenericApplicationContext();
	parent.refresh();

	AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
	child.setParent(parent);
	child.refresh();

	ConfigurableEnvironment env = child.getBean(ConfigurableEnvironment.class);
	assertThat("unknown env", env, anyOf(
			sameInstance(parent.getEnvironment()),
			sameInstance(child.getEnvironment())));
	assertThat("expected child ctx env", env, sameInstance(child.getEnvironment()));

	child.close();
	parent.close();
}
 
AbstractApplicationContext createApplicationContext(Domain domain) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(gatewayApplicationContext);
    context.setClassLoader(new ReactorHandlerClassLoader(gatewayApplicationContext.getClassLoader()));
    context.setEnvironment((ConfigurableEnvironment) gatewayApplicationContext.getEnvironment());

    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setIgnoreUnresolvablePlaceholders(true);
    configurer.setEnvironment(gatewayApplicationContext.getEnvironment());
    context.addBeanFactoryPostProcessor(configurer);

    context.getBeanFactory().registerSingleton("domain", domain);
    context.register(HandlerConfiguration.class);
    context.setId("context-domain-" + domain.getId());
    context.refresh();

    return context;
}
 
AbstractApplicationContext createApplicationContext(Api api) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(gatewayApplicationContext);
    context.setClassLoader(new ReactorHandlerClassLoader(gatewayApplicationContext.getClassLoader()));
    context.setEnvironment((ConfigurableEnvironment) gatewayApplicationContext.getEnvironment());

    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setIgnoreUnresolvablePlaceholders(true);
    configurer.setEnvironment(gatewayApplicationContext.getEnvironment());
    context.addBeanFactoryPostProcessor(configurer);

    context.getBeanFactory().registerSingleton("api", api);
    context.register(ApiHandlerConfiguration.class);
    context.setId("context-api-" + api.getId());
    context.refresh();

    return context;
}
 
源代码8 项目: webanno   文件: Plugin.java
protected ApplicationContext createApplicationContext()
{
    Plugin springPlugin = (Plugin) getWrapper().getPlugin();
    
    // Create an application context for this plugin using Spring annotated classes starting
    // with the plugin class
    AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext();
    pluginContext
            .setResourceLoader(new DefaultResourceLoader(getWrapper().getPluginClassLoader()));
    pluginContext.registerBean(ExportedComponentPostProcessor.class);
    pluginContext.register(springPlugin.getSources().stream().toArray(Class[]::new));

    // Attach the plugin application context to the main application context such that it can
    // access its beans for auto-wiring
    ApplicationContext parent = ((PluginManager) getWrapper().getPluginManager())
            .getApplicationContext();
    pluginContext.setParent(parent);

    // Initialize the context
    pluginContext.refresh();
    
    
    return pluginContext;
}
 
@Override
public void setApplicationContext(ApplicationContext mainContext) throws BeansException {
  super.setApplicationContext(mainContext);

  AnnotationConfigApplicationContext nestedContext = new AnnotationConfigApplicationContext();
  nestedContext.setParent(mainContext);
  
  deployCalled = false;
  nestedContext.refresh();
  deployOnChildRefresh = deployCalled;

  ((AbstractApplicationContext) mainContext).addApplicationListener(new ApplicationListener<MyEvent>() {

    @Override
    public void onApplicationEvent(MyEvent event) {
      triggered = true;
    }
  });
}
 
源代码10 项目: netstrap   文件: NetstrapBootApplication.java
/**
 * 创建Spring容器
 */
private ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(new ClassPathXmlApplicationContext(configLocations));
    context.scan(DEFAULT_SCAN);
    context.refresh();
    return context;
}
 
@Override
public ProtocolProvider create(String type, ApplicationContext parentContext) {
    logger.debug("Looking for an protocol provider for [{}]", type);
    Protocol protocol = protocols.get(type);
    if (protocol != null) {
        try {
            ProtocolProvider protocolProvider = createInstance(protocol.protocolProvider());
            Plugin plugin = protocolPlugins.get(protocol);

            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
            context.setParent(parentContext);
            context.setClassLoader(pluginClassLoaderFactory.getOrCreateClassLoader(plugin));
            context.setEnvironment((ConfigurableEnvironment) parentContext.getEnvironment());

            PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
            configurer.setIgnoreUnresolvablePlaceholders(true);
            configurer.setEnvironment(parentContext.getEnvironment());
            context.addBeanFactoryPostProcessor(configurer);

            context.register(protocol.configuration());
            context.registerBeanDefinition(plugin.clazz(), BeanDefinitionBuilder.rootBeanDefinition(plugin.clazz()).getBeanDefinition());
            context.refresh();

            context.getAutowireCapableBeanFactory().autowireBean(protocolProvider);

            if (protocolProvider instanceof InitializingBean) {
                ((InitializingBean) protocolProvider).afterPropertiesSet();
            }
            return protocolProvider;
        } catch (Exception ex) {
            logger.error("An unexpected error occurs while loading protocol", ex);
            return null;
        }
    } else {
        logger.error("No protocol provider is registered for type {}", type);
        throw new IllegalStateException("No protocol provider is registered for type " + type);
    }
}
 
源代码12 项目: spring-cloud-deployer-yarn   文件: AppDeployerIT.java
@Before
public void setup() {
	artifactVersion = getEnvironment().getProperty("artifactVersion");
	context = new AnnotationConfigApplicationContext();
	context.getEnvironment().setActiveProfiles("yarn");
	context.register(TestYarnConfiguration.class);
	context.setParent(getApplicationContext());
	context.refresh();
}
 
@Before
public void setup() {
	artifactVersion = getEnvironment().getProperty("artifactVersion");
	context = new AnnotationConfigApplicationContext();
	context.getEnvironment().setActiveProfiles("yarn");
	context.register(TestYarnConfiguration.class);
	context.setParent(getApplicationContext());
	context.refresh();
}
 
源代码14 项目: ariADDna   文件: GuiApp.java
/**
 * Native start method
 * @param primaryStage
 * @throws Exception
 */
public void start(final Stage primaryStage) throws Exception {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(GuiConfig.class);
    ctx.getBeanFactory()
            .registerSingleton(primaryStage.getClass().getCanonicalName(), primaryStage);
    ctx.setParent(parentCtx);
    ctx.refresh();

    Parent parent = (Parent) ViewsFactory.MAIN.getNode(ctx.getBean(FXMLLoaderProvider.class));
    Scene scene = new Scene(parent, 800, 600);
    primaryStage.setScene(scene);
    primaryStage.show();

}
 
源代码15 项目: spring-vertx-ext   文件: SpringVerticleFactory.java
private static AnnotationConfigApplicationContext getAnnotationConfigApplicationContext(
    Class<?> springConfigClass, GenericApplicationContext genericApplicationContext) {
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
    annotationConfigApplicationContext.setParent(genericApplicationContext);
    annotationConfigApplicationContext.register(SpringContextConfiguration.class, springConfigClass);
    return annotationConfigApplicationContext;
}
 
源代码16 项目: initializr   文件: ProjectGenerationInvoker.java
private void customizeProjectGenerationContext(AnnotationConfigApplicationContext context,
		InitializrMetadata metadata) {
	context.setParent(this.parentApplicationContext);
	context.registerBean(InitializrMetadata.class, () -> metadata);
	context.registerBean(BuildItemResolver.class, () -> new MetadataBuildItemResolver(metadata,
			context.getBean(ProjectDescription.class).getPlatformVersion()));
	context.registerBean(MetadataProjectDescriptionCustomizer.class,
			() -> new MetadataProjectDescriptionCustomizer(metadata));
}
 
源代码17 项目: mpns   文件: SpringConfig.java
static void scanHandler() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(CTX);
    context.scan("com.mpush.mpns.web.handler");
    context.refresh();
}