@Override public void initPropertySources(@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) { WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig); }
@Override public void initPropertySources(@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) { WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig); }
Mockito.when(context.getResource(ArgumentMatchers.eq("file:foo/uaa.yml"))).thenReturn( new ByteArrayResource(("logging:\n config: "+tomcatLogConfig).getBytes())); environment.getPropertySources().addFirst(new PropertySource<Object>(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME) { @Override public boolean containsProperty(String name) {
private void setProperty(String name, String value) { StandardServletEnvironment env = (StandardServletEnvironment) webApplicationContext.getEnvironment(); MockPropertySource mockPropertySource = new MockPropertySource(); mockPropertySource.setProperty(name, value); env.getPropertySources().addLast(mockPropertySource); assertEquals(value, webApplicationContext.getEnvironment().getProperty(name)); }
@Bean public PropertySystemInfo systemInfo() { PropertySystemInfo info = new PropertySystemInfo(Status.RUNNING, Type.CONFIG); info.setName(getName()); info.setVersion(getVersion()); info.setStartTime(DateUtil.now()); if (!(env instanceof StandardServletEnvironment)) { return info; } StandardServletEnvironment env = (StandardServletEnvironment) this.env; for (PropertySource<?> next : env.getPropertySources()) { if (next instanceof ResourcePropertySource) { Map<String, Object> source = ((ResourcePropertySource) next).getSource(); info.setInfo(source); } } return info; }
@Override public void initPropertySources(@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) { WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig); }
@Override public void initPropertySources(@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) { WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig); }
public static Map<String, Object> getAllProperties() { Map<String, Object> retMap = new NoConverterMap<>(); Iterator<PropertySource<?>> ite = ConfigProperties.localenv.getPropertySources() .iterator(); while (ite.hasNext()) { PropertySource<?> s = ite.next(); retMap.putAll(resolvePropertySource(s)); } return retMap; }
@Scheduled( cron = "*/3 * * * * *" ) @SchedulerLock( name = "DemoTask" ) public void test () { System.err.println( environment.getPropertySources() .get( "server.ports" ) .getSource() + " : DemoTask test run " ); }
/** * @param context * * @return */ public static Class<? extends WebServer> getBootstrapClassFromContext(WebApplicationContext context) { Map configMap = (Map) ((StandardServletEnvironment) context.getEnvironment()).getPropertySources() .get(ConfigurationConstants.EXECUTION_ENVIRONMENT_KEY).getSource(); return (Class<? extends WebServer>) configMap.get(ConfigurationConstants.EXECUTION_PROPERTIES_BOOTSTRAP); }
/** * @param context * * @return */ public static ServerConfiguration getConfigurationFromContext(WebApplicationContext context) { Map configMap = (Map) ((StandardServletEnvironment) context.getEnvironment()).getPropertySources() .get(ConfigurationConstants.EXECUTION_ENVIRONMENT_KEY).getSource(); return (ServerConfiguration) configMap.get(ConfigurationConstants.SERVER_CONFIGURATION_KEY); }
(StandardServletEnvironment) super.getAppliedPropertySources().get(ENVIRONMENT_PROPERTIES).getSource(); propertySources.getPropertySources().forEach(propertySource -> { if (propertySource.getSource() instanceof Map) {
/** * @param context * * @return */ public static BaseContextConfiguration getContextConfigurationFromContext(WebApplicationContext context) { Map configMap = (Map) ((StandardServletEnvironment) context.getEnvironment()).getPropertySources() .get(ConfigurationConstants.EXECUTION_ENVIRONMENT_KEY).getSource(); return (BaseContextConfiguration) configMap.get(ConfigurationConstants.CONTEXT_CONFIGURATION_KEY); }
@Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) registry; PropertySourcesPlaceholderConfigurer configurer = configPropertySourcesPlaceholderConfigurer(beanFactory); String[] propertiesAwares = beanFactory.getBeanNamesForType(PropertiesAware.class); Arrays.asList(propertiesAwares).forEach(propertiesAwareName -> beanFactory.getBean(propertiesAwareName, PropertiesAware.class).setProperties(localenv)); mergeProperties(configurer); PropertySourcesPropertyResolver propertyResolver = new PropertySourcesPropertyResolver(localenv.getPropertySources()); boolean trimValues = false; boolean ignoreUnresolvablePlaceholders = false; beanFactory.addEmbeddedValueResolver((strVal -> { String resolved = (ignoreUnresolvablePlaceholders ? propertyResolver.resolvePlaceholders(strVal) : propertyResolver.resolveRequiredPlaceholders(strVal)); if (trimValues) { resolved = resolved.trim(); } return (resolved.equals(null) ? null : resolved); })); }
@Override protected WebApplicationContext createWebApplicationContext(ServletContext sc) { Class<?> contextClass = determineContextClass(sc); if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]"); } CusomizableXMLWebApplicationContext ctx = (CusomizableXMLWebApplicationContext) BeanUtils.instantiateClass(contextClass); // 1) This injects the configuration in the Spring execution environment. // !!! REQUIRED BY THE `BeanAutoMockPostProcessor` when in use, else are just ignored MutablePropertySources sources = ((StandardServletEnvironment) ctx.getEnvironment()).getPropertySources(); if (baseContextConfiguration.getPropertiesLocation() != null) { Properties properties = PropertyFileReader.fromClasspath(baseContextConfiguration.getPropertiesLocation()).load(); sources.addFirst(new PropertiesPropertySource("extra_properties_added_for_testing", properties)); log.info("Added extra properties {} for context `{}`", properties, baseContextConfiguration.getContextPath()); } Map<String, Object> configMap = new HashMap<String, Object>() { { put(ConfigurationConstants.EXECUTION_PROPERTIES_BOOTSTRAP, bootstrap); put(ConfigurationConstants.SERVER_CONFIGURATION_KEY, serverConfiguration); put(ConfigurationConstants.CONTEXT_CONFIGURATION_KEY, baseContextConfiguration); } }; sources.addLast(new MapPropertySource(ConfigurationConstants.EXECUTION_ENVIRONMENT_KEY, configMap)); // 2) Essential :: DO NOT REMOVE ctx.setBaseContextConfiguration(baseContextConfiguration); return ctx; }