org.springframework.context.ApplicationContext#getParent ( )源码实例Demo

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

@Override
protected WebApplicationContext initWebAppContext() {
	ServletContext servletContext = this.webAppContext.getServletContext();
	Assert.state(servletContext != null, "No ServletContext");
	ApplicationContext rootWac = WebApplicationContextUtils.getWebApplicationContext(servletContext);

	if (rootWac == null) {
		rootWac = this.webAppContext;
		ApplicationContext parent = this.webAppContext.getParent();
		while (parent != null) {
			if (parent instanceof WebApplicationContext && !(parent.getParent() instanceof WebApplicationContext)) {
				rootWac = parent;
				break;
			}
			parent = parent.getParent();
		}
		servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac);
	}

	return this.webAppContext;
}
 
源代码2 项目: engine   文件: BeanDefinitionUtils.java
/**
 * Creates a bean definition for the specified bean name. If the parent context of the current context contains a
 * bean definition with the same name, the definition is created as a bean copy of the parent definition. This
 * method is useful for config parsers that want to create a bean definition from configuration but also want to
 * retain the default properties of the original bean.
 *
 * @param applicationContext    the current application context
 * @param beanName
 * @return the bean definition
 */
public static BeanDefinition createBeanDefinitionFromOriginal(ApplicationContext applicationContext,
                                                              String beanName) {
    ApplicationContext parentContext = applicationContext.getParent();
    BeanDefinition parentDefinition = null;

    if (parentContext != null &&
        parentContext.getAutowireCapableBeanFactory() instanceof ConfigurableListableBeanFactory) {
        ConfigurableListableBeanFactory parentBeanFactory = (ConfigurableListableBeanFactory)parentContext
            .getAutowireCapableBeanFactory();

        try {
            parentDefinition = parentBeanFactory.getBeanDefinition(beanName);
        } catch (NoSuchBeanDefinitionException e) {}
    }

    if (parentDefinition != null) {
        return new GenericBeanDefinition(parentDefinition);
    } else {
        return new GenericBeanDefinition();
    }
}
 
源代码3 项目: metrics   文件: GaugeRegistry4SpringContext.java
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
    //仅在root application context启动后执行
    if (applicationContext.getParent() != null) {
        return;
    }
    Map<String, Object> beansOfType = applicationContext.getBeansOfType(Object.class);
    for (Object bean : beansOfType.values()) {
        if (bean == null) {
            return;
        }
        Class<?> targetClass = AopUtils.getTargetClass(bean);
        Method[] methods = targetClass.getMethods();
        if (methods == null || methods.length == 0) {
            return;
        }
        for (Method method : methods) {
            EnableGauge aliGaugeAnnotation = method.getAnnotation(EnableGauge.class);
            if (aliGaugeAnnotation == null) {
                continue;
            }
            GaugeRegistry.registerGauge(aliGaugeAnnotation, bean, method);
        }
    }
}
 
@Test
public void verifyDispatcherWacConfig() {
	ApplicationContext parent = wac.getParent();
	assertNotNull(parent);
	assertTrue(parent instanceof WebApplicationContext);

	ApplicationContext grandParent = parent.getParent();
	assertNotNull(grandParent);
	assertFalse(grandParent instanceof WebApplicationContext);

	ServletContext dispatcherServletContext = wac.getServletContext();
	assertNotNull(dispatcherServletContext);
	ServletContext rootServletContext = ((WebApplicationContext) parent).getServletContext();
	assertNotNull(rootServletContext);
	assertSame(dispatcherServletContext, rootServletContext);

	assertSame(parent,
		rootServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
	assertSame(parent,
		dispatcherServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));

	assertEquals("ear", ear);
	assertEquals("root", root);
	assertEquals("dispatcher", dispatcher);
}
 
源代码5 项目: cxf   文件: SpringBus.java
public void onApplicationEvent(ApplicationEvent event) {
    if (ctx == null) {
        return;
    }
    boolean doIt = false;
    ApplicationContext ac = ctx;
    while (ac != null) {
        if (event.getSource() == ac) {
            doIt = true;
            break;
        }
        ac = ac.getParent();
    }
    if (doIt) {
        if (event instanceof ContextRefreshedEvent) {
            if (getState() != BusState.RUNNING) {
                initialize();
            }
        } else if (event instanceof ContextClosedEvent && getState() == BusState.RUNNING) {
            // The bus could be create by using SpringBusFactory.createBus("/cxf.xml");
            // Just to make sure the shutdown is called rightly
            shutdown();
        }
    }
}
 
@Override
protected WebApplicationContext initWebAppContext() {

	ServletContext servletContext = this.webAppContext.getServletContext();
	ApplicationContext rootWac = WebApplicationContextUtils.getWebApplicationContext(servletContext);

	if (rootWac == null) {
		rootWac = this.webAppContext;
		ApplicationContext parent = this.webAppContext.getParent();
		while (parent != null) {
			if (parent instanceof WebApplicationContext && !(parent.getParent() instanceof WebApplicationContext)) {
				rootWac = parent;
				break;
			}
			parent = parent.getParent();
		}
		servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac);
	}

	return this.webAppContext;
}
 
@Test
public void verifyDispatcherWacConfig() {
	ApplicationContext parent = wac.getParent();
	assertNotNull(parent);
	assertTrue(parent instanceof WebApplicationContext);

	ApplicationContext grandParent = parent.getParent();
	assertNotNull(grandParent);
	assertFalse(grandParent instanceof WebApplicationContext);

	ServletContext dispatcherServletContext = wac.getServletContext();
	assertNotNull(dispatcherServletContext);
	ServletContext rootServletContext = ((WebApplicationContext) parent).getServletContext();
	assertNotNull(rootServletContext);
	assertSame(dispatcherServletContext, rootServletContext);

	assertSame(parent,
		rootServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
	assertSame(parent,
		dispatcherServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));

	assertEquals("ear", ear);
	assertEquals("root", root);
	assertEquals("dispatcher", dispatcher);
}
 
@Override
public void setApplicationContext(ApplicationContext applicationContext)
		throws BeansException {
	this.applicationContext = applicationContext;
	if (applicationContext
			.getAutowireCapableBeanFactory() instanceof ConfigurableListableBeanFactory) {
		this.beanFactory = (ConfigurableListableBeanFactory) applicationContext
				.getAutowireCapableBeanFactory();
	}
	if (applicationContext.getParent() != null && applicationContext.getParent()
			.getAutowireCapableBeanFactory() instanceof ConfigurableListableBeanFactory) {
		ConfigurableListableBeanFactory listable = (ConfigurableListableBeanFactory) applicationContext
				.getParent().getAutowireCapableBeanFactory();
		String[] names = listable
				.getBeanNamesForType(ConfigurationPropertiesBeans.class);
		if (names.length == 1) {
			this.parent = (ConfigurationPropertiesBeans) listable.getBean(names[0]);
			this.beans.putAll(this.parent.beans);
		}
	}
}
 
private void removeDecryptedProperties(ApplicationContext applicationContext) {
	ApplicationContext parent = applicationContext;
	while (parent != null) {
		if (parent.getEnvironment() instanceof ConfigurableEnvironment) {
			((ConfigurableEnvironment) parent.getEnvironment()).getPropertySources()
					.remove(DECRYPTED_PROPERTY_SOURCE_NAME);
		}
		parent = parent.getParent();
	}
}
 
源代码10 项目: cxf   文件: SpringBus.java
/** {@inheritDoc}*/
public void setApplicationContext(ApplicationContext applicationContext) {
    ctx = (AbstractApplicationContext)applicationContext;
    @SuppressWarnings("rawtypes")
    ApplicationListener listener = new ApplicationListener() {
        public void onApplicationEvent(ApplicationEvent event) {
            SpringBus.this.onApplicationEvent(event);
        }
    };
    ctx.addApplicationListener(listener);
    ApplicationContext ac = applicationContext.getParent();
    while (ac != null) {
        if (ac instanceof AbstractApplicationContext) {
            ((AbstractApplicationContext)ac).addApplicationListener(listener);
        }
        ac = ac.getParent();
    }

    // set the classLoader extension with the application context classLoader
    setExtension(applicationContext.getClassLoader(), ClassLoader.class);

    setExtension(new ConfigurerImpl(applicationContext), Configurer.class);

    ResourceManager m = getExtension(ResourceManager.class);
    m.addResourceResolver(new BusApplicationContextResourceResolver(applicationContext));

    setExtension(applicationContext, ApplicationContext.class);
    ConfiguredBeanLocator loc = getExtension(ConfiguredBeanLocator.class);
    if (!(loc instanceof SpringBeanLocator)) {
        setExtension(new SpringBeanLocator(applicationContext, this), ConfiguredBeanLocator.class);
    }
    if (getState() != BusState.RUNNING) {
        initialize();
    }
}
 
@Bean
public ConfigClientProperties configClientProperties(Environment environment,
		ApplicationContext context) {
	if (context.getParent() != null
			&& BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
					context.getParent(), ConfigClientProperties.class).length > 0) {
		return BeanFactoryUtils.beanOfTypeIncludingAncestors(context.getParent(),
				ConfigClientProperties.class);
	}
	ConfigClientProperties client = new ConfigClientProperties(environment);
	return client;
}
 
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();

    if (applicationContext.getParent() == null) {
        FactoryBuilder.registerBeanFactory(applicationContext.getBean(BeanFactory.class));
    }
}
 
@Bean
public NacosConfigProperties nacosConfigProperties(ApplicationContext context) {
	if (context.getParent() != null
			&& BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
					context.getParent(), NacosConfigProperties.class).length > 0) {
		return BeanFactoryUtils.beanOfTypeIncludingAncestors(context.getParent(),
				NacosConfigProperties.class);
	}
	return new NacosConfigProperties();
}
 
源代码14 项目: jwala   文件: ApplicationContextListener.java
/**
 * Implementation of the spring event listener interface.
 *
 * @param event the spring event from the application
 */
@EventListener
public void handleEvent(ApplicationEvent event) {
    // checking for start up event
    // order of events is BrokerAvailabilityEvent -> ContextRefreshedEvent[parent=null] -> ContextRefreshedEvent[with non-null parent]
    // so wait until the latest event is received: ContextRefreshedEvent[with non-null parent]

    // skip the BrokerAvailabilityEvent, and ignore all other events (SessionConnectedEvent, ServletRequestHandledEvent, ContextClosedEvent, etc.)
    if (!(event instanceof ContextRefreshedEvent)) {
        LOGGER.debug("Expecting ContextRefreshedEvent. Skipping.");
        return;
    }

    LOGGER.info("Received ContextRefreshedEvent {}", event);

    ContextRefreshedEvent crEvent = (ContextRefreshedEvent) event;
    final ApplicationContext applicationContext = crEvent.getApplicationContext();
    // skip the ContextRefreshedEvent[parent=null] but check for non-null context first
    if (null == applicationContext) {
        LOGGER.debug("Expecting non-null ApplicationContext. Skipping.");
        return;
    }
    if (null == applicationContext.getParent()) {
        LOGGER.debug("Expecting non-null ApplicationContext parent. Skipping.");
        return;
    }

    processBootstrapConfiguration();
}
 
private void setPortProperty(ApplicationContext context, String propertyName, int port) {
  if (context instanceof ConfigurableApplicationContext) {
    setPortProperty(((ConfigurableApplicationContext) context).getEnvironment(),
        propertyName, port);
  }
  if (context.getParent() != null) {
    setPortProperty(context.getParent(), propertyName, port);
  }
}
 
private void insert(ApplicationContext applicationContext,
		PropertySource<?> propertySource) {
	ApplicationContext parent = applicationContext;
	while (parent != null) {
		if (parent.getEnvironment() instanceof ConfigurableEnvironment) {
			ConfigurableEnvironment mutable = (ConfigurableEnvironment) parent
					.getEnvironment();
			insert(mutable.getPropertySources(), propertySource);
		}
		parent = parent.getParent();
	}
}
 
源代码17 项目: spring-cloud-commons   文件: RestartEndpoint.java
private void close() {
	ApplicationContext context = this.context;
	while (context instanceof Closeable) {
		try {
			((Closeable) context).close();
		}
		catch (IOException e) {
			logger.error("Cannot close context: " + context.getId(), e);
		}
		context = context.getParent();
	}
}
 
源代码18 项目: mPaaS   文件: ApplicationContextHolder.java
public static void setApplicationContext(ApplicationContext context) {
    if (applicationContext == null
            || applicationContext == context.getParent()) {
        applicationContext = context;
    }
}
 
源代码19 项目: mPass   文件: ApplicationContextHolder.java
public static void setApplicationContext(ApplicationContext context) {
    if (applicationContext == null
            || applicationContext == context.getParent()) {
        applicationContext = context;
    }
}
 
源代码20 项目: spring-boot-inside   文件: ContextUtils.java
public static String treeHtml(ApplicationContext context) {

		StringBuilder result = new StringBuilder();

		result.append("<ul>");

		result.append("<li>" + context.toString() + "</li>");

		while (context.getParent() != null) {
			context = context.getParent();
			result.append("<li>" + context.toString() + "</li>");
		}

		result.append("</ul>");
		return result.toString();
	}