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

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#setParent() . These examples are extracted from open source projects. 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
@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();
}
 
Example 2
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();
}
 
Example 3
@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();
}
 
Example 4
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;
    }
 
Example 5
@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();
}
 
Example 6
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;
}
 
Example 7
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;
}
 
Example 8
Source Project: webanno   File: Plugin.java    License: Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 9
@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;
    }
  });
}
 
Example 10
/**
 * 创建Spring容器
 */
private ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(new ClassPathXmlApplicationContext(configLocations));
    context.scan(DEFAULT_SCAN);
    context.refresh();
    return context;
}
 
Example 11
@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);
    }
}
 
Example 12
@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 13
@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 14
Source Project: ariADDna   File: GuiApp.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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();

}
 
Example 15
private static AnnotationConfigApplicationContext getAnnotationConfigApplicationContext(
    Class<?> springConfigClass, GenericApplicationContext genericApplicationContext) {
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
    annotationConfigApplicationContext.setParent(genericApplicationContext);
    annotationConfigApplicationContext.register(SpringContextConfiguration.class, springConfigClass);
    return annotationConfigApplicationContext;
}
 
Example 16
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));
}
 
Example 17
Source Project: mpns   File: SpringConfig.java    License: Apache License 2.0 4 votes vote down vote up
static void scanHandler() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(CTX);
    context.scan("com.mpush.mpns.web.handler");
    context.refresh();
}