コード例 #1
0
  /**
   * Replace {@code Servlet}- and {@code Portlet}-based {@link
   * org.springframework.core.env.PropertySource.StubPropertySource stub property sources} with
   * actual instances populated with the given {@code servletContext}, {@code portletContext} and
   * {@code portletConfig} objects.
   *
   * <p>This method is idempotent with respect to the fact it may be called any number of times but
   * will perform replacement of stub property sources with their corresponding actual property
   * sources once and only once.
   *
   * @param propertySources the {@link MutablePropertySources} to initialize (must not be {@code
   *     null})
   * @param servletContext the current {@link ServletContext} (ignored if {@code null} or if the
   *     {@link
   *     org.springframework.web.context.support.StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME
   *     servlet context property source} has already been initialized)
   * @param portletContext the current {@link PortletContext} (ignored if {@code null} or if the
   *     {@link StandardPortletEnvironment#PORTLET_CONTEXT_PROPERTY_SOURCE_NAME portlet context
   *     property source} has already been initialized)
   * @param portletConfig the current {@link PortletConfig} (ignored if {@code null} or if the
   *     {@link StandardPortletEnvironment#PORTLET_CONFIG_PROPERTY_SOURCE_NAME portlet config
   *     property source} has already been initialized)
   * @see org.springframework.core.env.PropertySource.StubPropertySource
   * @see
   *     org.springframework.web.context.support.WebApplicationContextUtils#initServletPropertySources(MutablePropertySources,
   *     ServletContext)
   * @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
   */
  public static void initPortletPropertySources(
      MutablePropertySources propertySources,
      ServletContext servletContext,
      PortletContext portletContext,
      PortletConfig portletConfig) {
    Assert.notNull(propertySources, "propertySources must not be null");

    WebApplicationContextUtils.initServletPropertySources(propertySources, servletContext);

    if (portletContext != null
        && propertySources.contains(
            StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME)) {
      propertySources.replace(
          StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME,
          new PortletContextPropertySource(
              StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME, portletContext));
    }
    if (portletConfig != null
        && propertySources.contains(
            StandardPortletEnvironment.PORTLET_CONFIG_PROPERTY_SOURCE_NAME)) {
      propertySources.replace(
          StandardPortletEnvironment.PORTLET_CONFIG_PROPERTY_SOURCE_NAME,
          new PortletConfigPropertySource(
              StandardPortletEnvironment.PORTLET_CONFIG_PROPERTY_SOURCE_NAME, portletConfig));
    }
  }
コード例 #2
0
  @Override
  public void initialize(ConfigurableWebApplicationContext applicationContext) {

    Resource resource = null;
    ServletContext servletContext = applicationContext.getServletContext();
    WebApplicationContextUtils.initServletPropertySources(
        applicationContext.getEnvironment().getPropertySources(),
        servletContext,
        applicationContext.getServletConfig());

    ServletConfig servletConfig = applicationContext.getServletConfig();
    String locations =
        servletConfig == null
            ? null
            : servletConfig.getInitParameter(PROFILE_CONFIG_FILE_LOCATIONS);
    resource = getResource(servletContext, applicationContext, locations);

    if (resource == null) {
      servletContext.log(
          "No YAML environment properties from servlet.  Defaulting to servlet context.");
      locations = servletContext.getInitParameter(PROFILE_CONFIG_FILE_LOCATIONS);
      resource = getResource(servletContext, applicationContext, locations);
    }

    try {
      servletContext.log("Loading YAML environment properties from location: " + resource);
      YamlMapFactoryBean factory = new YamlMapFactoryBean();
      factory.setResolutionMethod(ResolutionMethod.OVERRIDE_AND_IGNORE);

      List<Resource> resources = new ArrayList<Resource>();

      String defaultLocation =
          servletConfig == null
              ? null
              : servletConfig.getInitParameter(PROFILE_CONFIG_FILE_DEFAULT);
      if (defaultLocation != null) {
        Resource defaultResource = new ClassPathResource(defaultLocation);
        if (defaultResource.exists()) {
          resources.add(defaultResource);
        }
      }

      resources.add(resource);
      factory.setResources(resources.toArray(new Resource[resources.size()]));

      Map<String, Object> map = factory.getObject();
      String yamlStr = (new Yaml()).dump(map);
      map.put(rawYamlKey, yamlStr);
      NestedMapPropertySource properties = new NestedMapPropertySource("servletConfigYaml", map);
      applicationContext.getEnvironment().getPropertySources().addLast(properties);
      applySpringProfiles(applicationContext.getEnvironment(), servletContext);
      applyLog4jConfiguration(applicationContext.getEnvironment(), servletContext);

    } catch (Exception e) {
      servletContext.log("Error loading YAML environment properties from location: " + resource, e);
    }
  }
コード例 #3
0
 @Override
 protected void initPropertySources() {
   super.initPropertySources();
   WebApplicationContextUtils.initServletPropertySources(
       this.getEnvironment().getPropertySources(), this.servletContext);
 }
コード例 #4
0
  /**
   * Convenient variant of {@link #initServletPropertySources(MutablePropertySources,
   * ServletContext, ServletConfig)} that always provides {@code null} for the {@link ServletConfig}
   * parameter.
   *
   * @see #initServletPropertySources(MutablePropertySources, ServletContext, ServletConfig)
   */
  public static void initServletPropertySources(
      MutablePropertySources propertySources, ServletContext servletContext) {

    initServletPropertySources(propertySources, servletContext, null);
  }