@Bean @Order(-1) PropertySource dispacherPropertySource() throws IOException { YamlPropertySourceLoader loader = new YamlPropertySourceLoader(); // profile null is default PropertySource propertySource = loader.load("dispachers", new ClassPathResource("dispatch.yml"),null); env.getPropertySources().addLast(propertySource); return propertySource; }
@Bean public PropertySource<?> yamlPropertySourceLoader() throws IOException { YamlPropertySourceLoader loader = new YamlPropertySourceLoader(); PropertySource<?> applicationYamlPropertySource = loader.load( "application.yml", new ClassPathResource("application.yml"),"default"); return applicationYamlPropertySource; }
@Bean @Order(-1) PropertySource consumerPropertySource() throws IOException { YamlPropertySourceLoader loader = new YamlPropertySourceLoader(); PropertySource propertySource = loader.load("consumers", new ClassPathResource("consumer.yml"),null); env.getPropertySources().addLast(propertySource); return propertySource; }
@Override public void initialize(ConfigurableApplicationContext applicationContext) { try { Resource resource = applicationContext.getResource("classpath:application.yml"); YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader(); PropertySource<?> yamlTestProperties = sourceLoader.load("yamlTestProperties", resource, null); applicationContext.getEnvironment().getPropertySources().addLast(yamlTestProperties); } catch (IOException e) { throw new RuntimeException(e); } }
@Test public void propertyParseIsEqualToYaml() throws Exception { HoconPropertySourceLoader hoconLoader = new HoconPropertySourceLoader(); YamlPropertySourceLoader yamlLoader = new YamlPropertySourceLoader(); EnumerablePropertySource hoconParse = loadProperties(hoconLoader, "/application.conf"); EnumerablePropertySource yamlParse = loadProperties(yamlLoader, "/application.yaml"); verifyPropertyKeysAreSame(hoconParse, yamlParse); verifyPropertyValuesAreSame(hoconParse, yamlParse); }
/** * Yaml property source loader. * * @return the property source * @throws IOException Signals that an I/O exception has occurred. */ @Bean public PropertySource<?> yamlPropertySourceLoader() throws IOException { YamlPropertySourceLoader loader = new YamlPropertySourceLoader(); PropertySource<?> applicationYamlPropertySource = loader.load("application.yml", new ClassPathResource("application.yml"), "default"); return applicationYamlPropertySource; }
public static PropertySource<?> loadYaml(String classpath){ YamlPropertySourceLoader loader = new YamlPropertySourceLoader(); try { PropertySource<?> props = loader.load(classpath, SpringUtils.newClassPathResource(classpath), null); return props; } catch (IOException e) { throw new BaseException("load yaml file error: " + classpath); } }
@Test public void test() throws Exception{ YamlPropertySourceLoader loader = new YamlPropertySourceLoader(); PropertySource<?> props = loader.load("application", SpringUtils.newClassPathResource("application.yaml"), null); Object env = props.getProperty("spring.profiles.active"); System.out.println("env: " + env); env = props.getProperty("server.port"); System.out.println("port: " + env); }
@Override public void initialize(ConfigurableApplicationContext applicationContext) { try { Resource resource = applicationContext.getResource("classpath:/config/application.yml"); YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader(); PropertySource<?> yamlTestProperties = sourceLoader.load("application.yml", resource, null); applicationContext.getEnvironment().getPropertySources().addLast(yamlTestProperties); } catch (IOException e) { throw new RuntimeException(e); } }
private PropertySource<?> loadPropertySource(Resource resource, String path) { if (resource.exists()) { try { PropertySource<?> source = new YamlPropertySourceLoader().load(path, resource, null); if (source != null) { logger.info("Loaded YAML properties from: " + resource); } return source; } catch (IOException e) { } } return null; }
@Bean public static YamlPropertySourceLoader yamlPropertySourceLoader() { return new YamlPropertySourceLoader(); }