org.springframework.core.env.PropertySource.StubPropertySource#org.springframework.jndi.JndiPropertySource源码实例Demo

下面列出了org.springframework.core.env.PropertySource.StubPropertySource#org.springframework.jndi.JndiPropertySource 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: waltz   文件: DIConfiguration.java
/**
 * Required for property interpolation to work correctly
 * @see <a href="http://stackoverflow.com/a/41760877/2311919">Explanation</a>
 */
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(ConfigurableEnvironment env) {
    PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();

    JndiPropertySource jndiPropertySource = new JndiPropertySource("java:comp");
    env.getPropertySources().addFirst(jndiPropertySource);

    return placeholderConfigurer;
}
 
@Test
public void testSetEnvironment() {
  ConfigurableEnvironment environment = Mockito.mock(ConfigurableEnvironment.class);
  MutablePropertySources propertySources = new MutablePropertySources();
  Map<String, String> propertyMap = new HashMap<>();
  final String map0Key0 = "map0-Key0";
  final String map1Key0 = "map1-Key0";
  final String map2Key0 = "map2-Key0";
  final String map3Key0 = "map3-Key0";
  propertyMap.put(map0Key0, "map0-Value0");
  propertyMap.put(map1Key0, "map1-Value0");
  propertyMap.put(map2Key0, "map2-Value0");
  propertyMap.put(map3Key0, "map3-Value0");

  /*
  propertySources
  |- compositePropertySource0
  |  |- mapPropertySource0
  |  |  |- map0-Key0 = map0-Value0
  |  |- compositePropertySource1
  |     |- mapPropertySource1
  |     |  |- map1-Key0 = map1-Value0
  |     |- mapPropertySource2
  |        |- map2-Key0 = map2-Value0
  |     |- JndiPropertySource(mocked)
  |- mapPropertySource3
    |- map3-Key0 = map3-Value0
   */
  CompositePropertySource compositePropertySource0 = new CompositePropertySource("compositePropertySource0");
  propertySources.addFirst(compositePropertySource0);

  HashMap<String, Object> map0 = new HashMap<>();
  map0.put(map0Key0, propertyMap.get(map0Key0));
  MapPropertySource mapPropertySource0 = new MapPropertySource("mapPropertySource0", map0);
  compositePropertySource0.addFirstPropertySource(mapPropertySource0);

  CompositePropertySource compositePropertySource1 = new CompositePropertySource("compositePropertySource1");
  compositePropertySource0.addPropertySource(compositePropertySource1);
  HashMap<String, Object> map1 = new HashMap<>();
  map1.put(map1Key0, propertyMap.get(map1Key0));
  MapPropertySource mapPropertySource1 = new MapPropertySource("mapPropertySource1", map1);
  compositePropertySource1.addPropertySource(mapPropertySource1);
  HashMap<String, Object> map2 = new HashMap<>();
  map2.put(map2Key0, propertyMap.get(map2Key0));
  MapPropertySource mapPropertySource2 = new MapPropertySource("mapPropertySource2", map2);
  compositePropertySource1.addPropertySource(mapPropertySource2);
  compositePropertySource1.addPropertySource(Mockito.mock(JndiPropertySource.class));

  HashMap<String, Object> map3 = new HashMap<>();
  map3.put(map3Key0, propertyMap.get(map3Key0));
  MapPropertySource mapPropertySource3 = new MapPropertySource("mapPropertySource3", map3);
  compositePropertySource0.addPropertySource(mapPropertySource3);

  Mockito.when(environment.getPropertySources()).thenReturn(propertySources);
  Mockito.doAnswer((Answer<String>) invocation -> {
    Object[] args = invocation.getArguments();
    String propertyName = (String) args[0];

    if ("spring.config.name".equals(propertyName) || "spring.application.name".equals(propertyName)) {
      return null;
    }

    String value = propertyMap.get(propertyName);
    if (null == value) {
      fail("get unexpected property name: " + propertyName);
    }
    return value;
  }).when(environment).getProperty(anyString(), Matchers.eq(Object.class));

  new ConfigurationSpringInitializer().setEnvironment(environment);

  Map<String, Map<String, Object>> extraLocalConfig = getExtraConfigMapFromConfigUtil();
  assertFalse(extraLocalConfig.isEmpty());
  Map<String, Object> extraProperties = extraLocalConfig
      .get(ConfigurationSpringInitializer.EXTRA_CONFIG_SOURCE_PREFIX + environment.getClass().getName() + "@"
          + environment.hashCode());
  assertNotNull(extraLocalConfig);
  for (Entry<String, String> entry : propertyMap.entrySet()) {
    assertEquals(entry.getValue(), extraProperties.get(entry.getKey()));
  }
}
 
/**
 * Customize the set of property sources with those contributed by superclasses as
 * well as those appropriate for standard servlet-based environments:
 * <ul>
 * <li>{@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME}
 * <li>{@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}
 * <li>{@value #JNDI_PROPERTY_SOURCE_NAME}
 * </ul>
 * <p>Properties present in {@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME} will
 * take precedence over those in {@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}, and
 * properties found in either of the above take precedence over those found in
 * {@value #JNDI_PROPERTY_SOURCE_NAME}.
 * <p>Properties in any of the above will take precedence over system properties and
 * environment variables contributed by the {@link StandardEnvironment} superclass.
 * <p>The {@code Servlet}-related property sources are added as
 * {@link StubPropertySource stubs} at this stage, and will be
 * {@linkplain #initPropertySources(ServletContext, ServletConfig) fully initialized}
 * once the actual {@link ServletContext} object becomes available.
 * @see StandardEnvironment#customizePropertySources
 * @see org.springframework.core.env.AbstractEnvironment#customizePropertySources
 * @see ServletConfigPropertySource
 * @see ServletContextPropertySource
 * @see org.springframework.jndi.JndiPropertySource
 * @see org.springframework.context.support.AbstractApplicationContext#initPropertySources
 * @see #initPropertySources(ServletContext, ServletConfig)
 */
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
	propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
	propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
	if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
		propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
	}
	super.customizePropertySources(propertySources);
}
 
/**
 * Customize the set of property sources with those contributed by superclasses as
 * well as those appropriate for standard servlet-based environments:
 * <ul>
 * <li>{@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME}
 * <li>{@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}
 * <li>{@value #JNDI_PROPERTY_SOURCE_NAME}
 * </ul>
 * <p>Properties present in {@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME} will
 * take precedence over those in {@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}, and
 * properties found in either of the above take precedence over those found in
 * {@value #JNDI_PROPERTY_SOURCE_NAME}.
 * <p>Properties in any of the above will take precedence over system properties and
 * environment variables contributed by the {@link StandardEnvironment} superclass.
 * <p>The {@code Servlet}-related property sources are added as
 * {@link StubPropertySource stubs} at this stage, and will be
 * {@linkplain #initPropertySources(ServletContext, ServletConfig) fully initialized}
 * once the actual {@link ServletContext} object becomes available.
 * @see StandardEnvironment#customizePropertySources
 * @see org.springframework.core.env.AbstractEnvironment#customizePropertySources
 * @see ServletConfigPropertySource
 * @see ServletContextPropertySource
 * @see org.springframework.jndi.JndiPropertySource
 * @see org.springframework.context.support.AbstractApplicationContext#initPropertySources
 * @see #initPropertySources(ServletContext, ServletConfig)
 */
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
	propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
	propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
	if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
		propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
	}
	super.customizePropertySources(propertySources);
}
 
源代码5 项目: lams   文件: StandardServletEnvironment.java
/**
 * Customize the set of property sources with those contributed by superclasses as
 * well as those appropriate for standard servlet-based environments:
 * <ul>
 * <li>{@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME}
 * <li>{@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}
 * <li>{@value #JNDI_PROPERTY_SOURCE_NAME}
 * </ul>
 * <p>Properties present in {@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME} will
 * take precedence over those in {@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}, and
 * properties found in either of the above take precedence over those found in
 * {@value #JNDI_PROPERTY_SOURCE_NAME}.
 * <p>Properties in any of the above will take precedence over system properties and
 * environment variables contributed by the {@link StandardEnvironment} superclass.
 * <p>The {@code Servlet}-related property sources are added as
 * {@link StubPropertySource stubs} at this stage, and will be
 * {@linkplain #initPropertySources(ServletContext, ServletConfig) fully initialized}
 * once the actual {@link ServletContext} object becomes available.
 * @see StandardEnvironment#customizePropertySources
 * @see org.springframework.core.env.AbstractEnvironment#customizePropertySources
 * @see ServletConfigPropertySource
 * @see ServletContextPropertySource
 * @see org.springframework.jndi.JndiPropertySource
 * @see org.springframework.context.support.AbstractApplicationContext#initPropertySources
 * @see #initPropertySources(ServletContext, ServletConfig)
 */
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
	propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
	propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
	if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
		propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
	}
	super.customizePropertySources(propertySources);
}
 
/**
 * Customize the set of property sources with those contributed by superclasses as
 * well as those appropriate for standard portlet-based environments:
 * <ul>
 * <li>{@value #PORTLET_CONFIG_PROPERTY_SOURCE_NAME}
 * <li>{@value #PORTLET_CONTEXT_PROPERTY_SOURCE_NAME}
 * <li>{@linkplain StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME "servletContextInitParams"}
 * <li>{@linkplain StandardServletEnvironment#JNDI_PROPERTY_SOURCE_NAME "jndiProperties"}
 * </ul>
 * <p>Properties present in {@value #PORTLET_CONFIG_PROPERTY_SOURCE_NAME} will
 * take precedence over those in {@value #PORTLET_CONTEXT_PROPERTY_SOURCE_NAME},
 * which takes precedence over those in {@linkplain
 * StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME "servletContextInitParams"}
 * and so on.
 * <p>Properties in any of the above will take precedence over system properties and
 * environment variables contributed by the {@link StandardEnvironment} superclass.
 * <p>The property sources are added as stubs for now, and will be
 * {@linkplain PortletApplicationContextUtils#initPortletPropertySources fully
 * initialized} once the actual {@link PortletConfig}, {@link PortletContext}, and
 * {@link ServletContext} objects are available.
 * @see StandardEnvironment#customizePropertySources
 * @see org.springframework.core.env.AbstractEnvironment#customizePropertySources
 * @see PortletConfigPropertySource
 * @see PortletContextPropertySource
 * @see PortletApplicationContextUtils#initPortletPropertySources
 */
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
	propertySources.addLast(new StubPropertySource(PORTLET_CONFIG_PROPERTY_SOURCE_NAME));
	propertySources.addLast(new StubPropertySource(PORTLET_CONTEXT_PROPERTY_SOURCE_NAME));
	propertySources.addLast(new StubPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
	if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
		propertySources.addLast(new JndiPropertySource(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME));
	}
	super.customizePropertySources(propertySources);
}
 
/**
 * Customize the set of property sources with those contributed by superclasses as
 * well as those appropriate for standard servlet-based environments:
 * <ul>
 * <li>{@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME}
 * <li>{@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}
 * <li>{@value #JNDI_PROPERTY_SOURCE_NAME}
 * </ul>
 * <p>Properties present in {@value #SERVLET_CONFIG_PROPERTY_SOURCE_NAME} will
 * take precedence over those in {@value #SERVLET_CONTEXT_PROPERTY_SOURCE_NAME}, and
 * properties found in either of the above take precedence over those found in
 * {@value #JNDI_PROPERTY_SOURCE_NAME}.
 * <p>Properties in any of the above will take precedence over system properties and
 * environment variables contributed by the {@link StandardEnvironment} superclass.
 * <p>The {@code Servlet}-related property sources are added as
 * {@link StubPropertySource stubs} at this stage, and will be
 * {@linkplain #initPropertySources(ServletContext, ServletConfig) fully initialized}
 * once the actual {@link ServletContext} object becomes available.
 * @see StandardEnvironment#customizePropertySources
 * @see org.springframework.core.env.AbstractEnvironment#customizePropertySources
 * @see ServletConfigPropertySource
 * @see ServletContextPropertySource
 * @see org.springframework.jndi.JndiPropertySource
 * @see org.springframework.context.support.AbstractApplicationContext#initPropertySources
 * @see #initPropertySources(ServletContext, ServletConfig)
 */
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
	propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
	propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
	if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
		propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
	}
	super.customizePropertySources(propertySources);
}