[bs-168] Support convenient binding of @Bean to external source
@ConfigurationProperties now has a path() attribute that can be used to specify a resource location explicitly. [Fixes #51968657]pull/7/head
parent
15ba11f302
commit
d5aad97d1f
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.bootstrap.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.bootstrap.config.YamlProcessor.DocumentMatcher;
|
||||
import org.springframework.bootstrap.config.YamlProcessor.MatchStatus;
|
||||
|
||||
/**
|
||||
* A {@link DocumentMatcher} that matches the default profile implicitly but not
|
||||
* explicitly (i.e. matches if "spring.profiles" is not found and not otherwise).
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public final class DefaultProfileDocumentMatcher implements DocumentMatcher {
|
||||
|
||||
@Override
|
||||
public MatchStatus matches(Properties properties) {
|
||||
if (!properties.containsKey("spring.profiles")) {
|
||||
return MatchStatus.FOUND;
|
||||
} else {
|
||||
return MatchStatus.NOT_FOUND;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.bootstrap.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.PropertiesPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
|
||||
/**
|
||||
* Strategy to load '.properties' files into a {@link PropertySource}.
|
||||
*/
|
||||
public class PropertiesPropertySourceLoader implements PropertySourceLoader {
|
||||
|
||||
@Override
|
||||
public boolean supports(Resource resource) {
|
||||
return resource.getFilename().endsWith(".properties");
|
||||
}
|
||||
|
||||
@Override
|
||||
public PropertySource<?> load(Resource resource, Environment environment) {
|
||||
try {
|
||||
Properties properties = loadProperties(resource, environment);
|
||||
return new PropertiesPropertySource(resource.getDescription(), properties);
|
||||
} catch (IOException ex) {
|
||||
throw new IllegalStateException("Could not load properties from " + resource,
|
||||
ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected Properties loadProperties(Resource resource, Environment environment)
|
||||
throws IOException {
|
||||
return PropertiesLoaderUtils.loadProperties(resource);
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.bootstrap.config;
|
||||
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* Strategy interface used to load a {@link PropertySource}.
|
||||
*/
|
||||
public interface PropertySourceLoader {
|
||||
|
||||
/**
|
||||
* @return Is this resource supported?
|
||||
*/
|
||||
public boolean supports(Resource resource);
|
||||
|
||||
/**
|
||||
* Load the resource into a property source.
|
||||
* @return a property source
|
||||
*/
|
||||
PropertySource<?> load(Resource resource, Environment environment);
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.bootstrap.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.bootstrap.config.YamlProcessor.ArrayDocumentMatcher;
|
||||
import org.springframework.bootstrap.config.YamlProcessor.DocumentMatcher;
|
||||
import org.springframework.bootstrap.config.YamlProcessor.MatchStatus;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class SpringProfileDocumentMatcher implements DocumentMatcher {
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
/**
|
||||
* @param environment
|
||||
*/
|
||||
public SpringProfileDocumentMatcher(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MatchStatus matches(Properties properties) {
|
||||
String[] profiles = this.environment.getActiveProfiles();
|
||||
if (profiles.length == 0) {
|
||||
profiles = new String[] { "default" };
|
||||
}
|
||||
return new ArrayDocumentMatcher("spring.profiles", profiles).matches(properties);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.bootstrap.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.bootstrap.config.YamlProcessor.DocumentMatcher;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* Strategy to load '.yml' files into a {@link PropertySource}.
|
||||
*/
|
||||
public class YamlPropertySourceLoader extends PropertiesPropertySourceLoader {
|
||||
|
||||
private List<DocumentMatcher> matchers;
|
||||
|
||||
/**
|
||||
* A property source loader that loads all properties and matches all documents.
|
||||
*
|
||||
* @return a property source loader
|
||||
*/
|
||||
public static YamlPropertySourceLoader matchAllLoader() {
|
||||
return new YamlPropertySourceLoader();
|
||||
}
|
||||
|
||||
/**
|
||||
* A property source loader that matches documents that have no explicit profile or
|
||||
* which have an explicit "spring.profiles.active" value in the current active
|
||||
* profiles.
|
||||
*
|
||||
* @return a property source loader
|
||||
*/
|
||||
public static YamlPropertySourceLoader springProfileAwareLoader(
|
||||
Environment environment) {
|
||||
return new YamlPropertySourceLoader(new SpringProfileDocumentMatcher(environment),
|
||||
new DefaultProfileDocumentMatcher());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param matchers
|
||||
*/
|
||||
public YamlPropertySourceLoader(DocumentMatcher... matchers) {
|
||||
this.matchers = Arrays.asList(matchers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(Resource resource) {
|
||||
return resource.getFilename().endsWith(".yml");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Properties loadProperties(final Resource resource,
|
||||
final Environment environment) throws IOException {
|
||||
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
|
||||
if (this.matchers != null && !this.matchers.isEmpty()) {
|
||||
factory.setMatchDefault(false);
|
||||
factory.setDocumentMatchers(this.matchers);
|
||||
}
|
||||
factory.setResources(new Resource[] { resource });
|
||||
return factory.getObject();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.bootstrap.context.initializer;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.bootstrap.config.YamlProcessor.DocumentMatcher;
|
||||
import org.springframework.bootstrap.config.YamlProcessor.MatchStatus;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A {@link DocumentMatcher} that sets the active profile if it finds a document with
|
||||
* a key <code>spring.profiles.active</code>.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public final class ProfileSettingDocumentMatcher implements DocumentMatcher {
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
public ProfileSettingDocumentMatcher(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MatchStatus matches(Properties properties) {
|
||||
if (!properties.containsKey("spring.profiles")) {
|
||||
Set<String> profiles = StringUtils.commaDelimitedListToSet(properties
|
||||
.getProperty("spring.profiles.active", ""));
|
||||
if (this.environment instanceof ConfigurableEnvironment) {
|
||||
ConfigurableEnvironment configurable = (ConfigurableEnvironment) this.environment;
|
||||
for (String profile : profiles) {
|
||||
// allow document with no profile to set the active one
|
||||
configurable.addActiveProfile(profile);
|
||||
}
|
||||
}
|
||||
// matches default profile
|
||||
return MatchStatus.FOUND;
|
||||
} else {
|
||||
return MatchStatus.NOT_FOUND;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
---
|
||||
name: foo
|
||||
|
||||
---
|
||||
spring.profiles: super
|
||||
name: bar
|
||||
|
||||
---
|
||||
spring.profiles: other
|
||||
name: spam
|
@ -0,0 +1,2 @@
|
||||
---
|
||||
name: other
|
Loading…
Reference in New Issue