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

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#setParent() . 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: HierarchicalSpringEventPropagateDemo.java    From geekbang-lessons with Apache License 2.0 6 votes vote down vote up
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 2
Source File: PostDeployWithNestedContext.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: Plugin.java    From webanno with 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 4
Source File: ApiContextHandlerFactory.java    From gravitee-gateway with Apache License 2.0 6 votes vote down vote up
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 5
Source File: SecurityDomainRouterFactory.java    From graviteeio-access-management with Apache License 2.0 6 votes vote down vote up
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 6
Source File: EnvironmentIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 7
Source File: OnceApplicationContextEventListenerTest.java    From spring-context-support with Apache License 2.0 6 votes vote down vote up
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 8
Source File: EnvironmentIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 9
Source File: EnvironmentIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 10
Source File: ProtocolPluginManagerImpl.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
@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 11
Source File: AppDeployerIT.java    From spring-cloud-deployer-yarn with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: TaskLauncherIT.java    From spring-cloud-deployer-yarn with Apache License 2.0 5 votes vote down vote up
@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
Source File: GuiApp.java    From ariADDna with 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 14
Source File: NetstrapBootApplication.java    From netstrap with Apache License 2.0 5 votes vote down vote up
/**
 * 创建Spring容器
 */
private ConfigurableApplicationContext createApplicationContext(String[] configLocations) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.setParent(new ClassPathXmlApplicationContext(configLocations));
    context.scan(DEFAULT_SCAN);
    context.refresh();
    return context;
}
 
Example 15
Source File: SpringVerticleFactory.java    From spring-vertx-ext with Apache License 2.0 5 votes vote down vote up
private static AnnotationConfigApplicationContext getAnnotationConfigApplicationContext(
    Class<?> springConfigClass, GenericApplicationContext genericApplicationContext) {
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
    annotationConfigApplicationContext.setParent(genericApplicationContext);
    annotationConfigApplicationContext.register(SpringContextConfiguration.class, springConfigClass);
    return annotationConfigApplicationContext;
}
 
Example 16
Source File: ProjectGenerationInvoker.java    From initializr with Apache License 2.0 5 votes vote down vote up
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 File: SpringConfig.java    From mpns with 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();
}