This commit is contained in:
Phillip Webb 2013-12-16 11:03:06 -08:00
parent 85fb1cba0b
commit 513c6a1de2
12 changed files with 76 additions and 53 deletions

View File

@ -19,7 +19,7 @@ Javadocs. Some rules of thumb:
* Look for classes called `*AutoConfiguration` and read their sources,
in particular the `@Conditional*` annotations to find out what
features they enable and when. In those clases...
* Look for classes that are `@ConfigurationProperties`
(e.g. [`ServerProperties`](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java?source=c))
and read from there the available external configuration
@ -27,16 +27,16 @@ Javadocs. Some rules of thumb:
acts as a prefix to external properties, thus `ServerProperties` has
`name="server"` and its configuration properties are `server.port`,
`server.address` etc.
* Look for use of `RelaxedEnvironment` to pull configuration values
explicitly out of the `Environment`. It often is used with a prefix.
* Look for `@Value` annotations that bind directly to the
`Environment`. This is less flexible than the `RelaxedEnvironment`
approach, but does allow some relaxed binding, specifically for OS
environment variables (so `CAPITALS_AND_UNDERSCORES` are synonyms
for `period.separated`).
* Look for `@ConditionalOnExpression` annotations that switch features
on and off in response to SpEL expressions, normally evaluated with
placeholders resolved from the `Environment`.
@ -84,7 +84,7 @@ In addition, if your context contains any beans of type `ObjectMapper`
then all of the `Module` beans will be registered with all of the
mappers. So there is a global mechanism for contributing custom
modules when you add new features to your application.
Finally, if you provide any `@Beans` of type
`MappingJackson2HttpMessageConverter` then they will replace the
default value in the MVC configuration. Also, a convenience bean is
@ -173,7 +173,7 @@ public EmbeddedServletContainerCustomizer containerCustomizer(){
if(factory instanceof TomcatEmbeddedServletContainerFactory){
TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) factory;
containerFactory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
@Override
public void customize(Connector connector) {
@ -190,7 +190,7 @@ public EmbeddedServletContainerCustomizer containerCustomizer(){
connector.setAttribute("clientAuth", "false");
connector.setAttribute("sslProtocol", "TLS");
connector.setAttribute("SSLEnabled", true);
});
}
}
@ -256,7 +256,7 @@ Create a deployable WAR by extending `SpringBootServletInitializer`
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
@ -307,7 +307,7 @@ Applications can fall into more than one category:
* Servlet 3.0 applications with no `web.xml`
* Applications with a `web.xml`
* Applications with a context hierarchy and
* Applications with a context hierarchy and
* Those without a context hierarchy
All of these should be amenable to translation, but each might require
@ -465,7 +465,7 @@ want to set levels, e.g.
```xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="DEBUG"/>
</configuration>
```
@ -478,7 +478,7 @@ will see that it uses some useful System properties which the
* `${LOG_FILE}` if `logging.file` was set in Boot's external configuration
* `${LOG_PATH` if `logging.path` was set (representing a directory for
log files to live in)
Spring Boot also provides some nice ANSI colour terminal output on a
console (but not in a log file) using a custom Logback converter. See
the default `base.xml` configuration for details.
@ -626,7 +626,7 @@ algorithm for choosing a specific implementation.
* If neither of those is available but an embedded database is then we
create one of those for you (preference order is h2, then Apache
Derby, then hsqldb).
The pooling `DataSource` option is controlled by external
configuration properties in `spring.datasource.*` for example:
@ -742,7 +742,7 @@ Properties from different sources are added to the Spring
`Environment` in a defined order, and the precedence for resolution is
1) commandline, 2) filesystem (current working directory)
`application.properties`, 3) classpath `application.properties`. To
modify this you can provide System properties (or environment variables)
modify this you can provide System properties (or environment variables)
* `config.name` (`CONFIG_NAME`), defaults to `application` as the root
of the file name

View File

@ -57,7 +57,7 @@ public class ConfigurationPropertiesReportEndpoint extends
return this.keysToSanitize;
}
public void setKeysToSanitize(String[] keysToSanitize) {
public void setKeysToSanitize(String... keysToSanitize) {
Assert.notNull(keysToSanitize, "KeysToSanitize must not be null");
this.keysToSanitize = keysToSanitize;
}
@ -69,10 +69,10 @@ public class ConfigurationPropertiesReportEndpoint extends
.getBeansWithAnnotation(ConfigurationProperties.class);
// Serialize beans into map structure and sanitize values
ObjectMapper mapper = new ObjectMapper();
for (Map.Entry<String, Object> entry : beans.entrySet()) {
ObjectMapper m = new ObjectMapper();
beans.put(entry.getKey(),
sanitize(m.convertValue(entry.getValue(), Map.class)));
Map<String, Object> value = mapper.convertValue(entry.getValue(), Map.class);
beans.put(entry.getKey(), sanitize(value));
}
return beans;
@ -94,7 +94,7 @@ public class ConfigurationPropertiesReportEndpoint extends
private Object sanitize(String name, Object object) {
for (String keyToSanitize : this.keysToSanitize) {
if (name.toLowerCase().endsWith(keyToSanitize)) {
return object == null ? null : "******";
return (object == null ? null : "******");
}
}
return object;

View File

@ -26,6 +26,7 @@ import javax.annotation.PostConstruct;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -39,6 +40,8 @@ import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link HttpMessageConverter}s.
*
* @author Dave Syer
*/
@Configuration

View File

@ -39,6 +39,8 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link HttpMessageConverters}.
*
* @author Dave Syer
* @author Phillip Webb
*/

View File

@ -35,8 +35,6 @@ import org.eclipse.aether.repository.RemoteRepository;
/**
* (Copied from aether source code - not available yet in Maven repo.)
*
* @author dsyer
*/
public final class JreProxySelector implements ProxySelector {

View File

@ -1,6 +1,5 @@
# Spring Boot Actuator Sample
You can build this sample using Maven (>3) or Gradle (1.6).
You can build this sample using Maven (>3) or Gradle (1.6).
With Maven:
@ -9,7 +8,8 @@ $ mvn package
$ java -jar target/*.jar
```
Then access the app via a browser (or curl) on http://localhost:8080 (the user name is "user" and look at the INFO log output for the password to login).
Then access the app via a browser (or curl) on http://localhost:8080 (the user name is
"user" and look at the INFO log output for the password to login).
With gradle:
@ -18,4 +18,6 @@ $ gradle build
$ java -jar build/libs/*.jar
```
The gradle build contains an intentionally odd configuration to exclude the security dependencies from the executable JAR. So the app run like this behaves differently than the one run from the Maven-built JAR file. See comments in the `build.gradle` for details.
The gradle build contains an intentionally odd configuration to exclude the security
dependencies from the executable JAR. So the app run like this behaves differently than
the one run from the Maven-built JAR file. See comments in the `build.gradle` for details.

View File

@ -16,8 +16,6 @@
package org.springframework.boot.sample.actuator.log4j;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Callable;
@ -36,6 +34,8 @@ import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.assertEquals;
/**
* Basic integration tests for service demo application.
*
@ -77,7 +77,6 @@ public class SampleActuatorApplicationTests {
assertEquals("Hello Phil", body.get("message"));
}
private RestTemplate getRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {

View File

@ -27,7 +27,7 @@ interface ReviewRepository extends Repository<Review, Long> {
Page<Review> findByHotel(Hotel hotel, Pageable pageable);
Review findByHotelAndIndex(Hotel hotel, int index);
Review save(Review review);
}

View File

@ -70,7 +70,7 @@ is intended to be run as it is you need to have all dependencies in it, however
if a plan is to explode a jar file and run main class manually you may already
have some of the libraries available via `CLASSPATH`. This is a situation where
you can repackage boot jar with a different set of dependencies. Using a custom
configuration will automatically disable dependency resolving from
configuration will automatically disable dependency resolving from
`compile`, `runtime` and `provided` scopes. Custom configuration can be either
defined globally inside `springBoot` or per task.
@ -131,7 +131,7 @@ sources jars are automatically skipped).
Because on default every repackage task execution will find all
created jar artifacts, the order of Gradle task execution is
important. This is not going to be an issue if you have a normal
important. This is not going to be an issue if you have a normal
project setup where only one jar file is created. However if you are
planning to create more complex project setup with custom Jar and
BootRepackage tasks, there are few tweaks to consider.
@ -149,7 +149,7 @@ out from your project. You could also just disable default
bootRepackage.withJarTask = jar
```
Above example simply instructs default `bootRepackage` task to only
work with a default `jar` task.
work with a default `jar` task.
```groovy
@ -165,6 +165,6 @@ create dependency to your build so that `bootJars` task would
be run after the default `bootRepackage` task is executed.
All the above tweaks are usually used to avoid situation where
already created boot jar is repackaged again. Repackaging
already created boot jar is repackaged again. Repackaging
an existing boot jar will not break anything but you may
get unnecessary dependencies in it.

View File

@ -68,14 +68,13 @@ public class PropertiesConfigurationFactoryTests {
assertEquals("blah", foo.name);
}
@Test
public void testUnderscore() throws Exception {
Foo foo = createFoo("spring_foo_baz: blah\nname: blah");
assertEquals("blah", foo.spring_foo_baz);
assertEquals("blah", foo.name);
}
@Test
public void testUnknownPropertyOkByDefault() throws Exception {
Foo foo = createFoo("hi: hello\nname: foo\nbar: blah");
@ -137,11 +136,11 @@ public class PropertiesConfigurationFactoryTests {
private String name;
private String bar;
private String spring_foo_baz;
public String getSpringFooBaz() {
return spring_foo_baz;
return this.spring_foo_baz;
}
public void setSpringFooBaz(String spring_foo_baz) {

View File

@ -28,9 +28,14 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
/**
* Class-level annotation that is used to determine how to load and configure an
* ApplicationContext for integration tests. Similar to the standard
* {@link ContextConfiguration} but uses Spring Boot's
* {@link SpringApplicationContextLoader}.
*
* @author Dave Syer
* @see SpringApplicationContextLoader
*/
@ContextConfiguration(loader = SpringApplicationContextLoader.class)
@Documented
@Inherited

View File

@ -58,27 +58,15 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig)
throws Exception {
Set<Object> sources = new LinkedHashSet<Object>();
sources.addAll(Arrays.asList(mergedConfig.getClasses()));
sources.addAll(Arrays.asList(mergedConfig.getLocations()));
SpringApplication application = new SpringApplication();
application.setSources(sources);
Map<String, Object> args = new LinkedHashMap<String, Object>();
application.setSources(getSources(mergedConfig));
if (!ObjectUtils.isEmpty(mergedConfig.getActiveProfiles())) {
application.setAdditionalProfiles(Arrays.asList(mergedConfig
.getActiveProfiles()));
}
// Not running an embedded server, just setting up web context
args.put("server.port", "0");
args.put("management.port", "0");
application.setDefaultProperties(args);
List<ApplicationContextInitializer<?>> initializers = new ArrayList<ApplicationContextInitializer<?>>(
application.getInitializers());
for (Class<? extends ApplicationContextInitializer<?>> type : mergedConfig
.getContextInitializerClasses()) {
initializers.add(BeanUtils.instantiate(type));
}
application.setDefaultProperties(getArgs(mergedConfig));
List<ApplicationContextInitializer<?>> initializers = getInitializers(
mergedConfig, application);
if (mergedConfig instanceof WebMergedContextConfiguration) {
new WebConfigurer().setup(mergedConfig, application, initializers);
}
@ -86,9 +74,36 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
application.setWebEnvironment(false);
}
application.setInitializers(initializers);
return application.run();
}
private Set<Object> getSources(MergedContextConfiguration mergedConfig) {
Set<Object> sources = new LinkedHashSet<Object>();
sources.addAll(Arrays.asList(mergedConfig.getClasses()));
sources.addAll(Arrays.asList(mergedConfig.getLocations()));
return sources;
}
private Map<String, Object> getArgs(MergedContextConfiguration mergedConfig) {
Map<String, Object> args = new LinkedHashMap<String, Object>();
// Not running an embedded server, just setting up web context
args.put("server.port", "0");
args.put("management.port", "0");
return args;
}
private List<ApplicationContextInitializer<?>> getInitializers(
MergedContextConfiguration mergedConfig, SpringApplication application) {
List<ApplicationContextInitializer<?>> initializers = new ArrayList<ApplicationContextInitializer<?>>();
initializers.addAll(application.getInitializers());
for (Class<? extends ApplicationContextInitializer<?>> initializerClass : mergedConfig
.getContextInitializerClasses()) {
initializers.add(BeanUtils.instantiate(initializerClass));
}
return initializers;
}
@Override
public ApplicationContext loadContext(String... locations) throws Exception {
throw new UnsupportedOperationException(