This commit is contained in:
Phillip Webb 2016-05-27 16:32:27 -07:00
parent 68040c952f
commit bc6f1cfdf3
2 changed files with 87 additions and 72 deletions

View File

@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
@ -88,19 +89,22 @@ class ImportAutoConfigurationImportSelector
private void collectCandidateConfigurations(Class<?> source, Annotation annotation, private void collectCandidateConfigurations(Class<?> source, Annotation annotation,
Set<String> candidates, Set<Class<?>> seen) { Set<String> candidates, Set<Class<?>> seen) {
if (ANNOTATION_NAMES.contains(annotation.annotationType().getName())) { if (ANNOTATION_NAMES.contains(annotation.annotationType().getName())) {
String[] value = (String[]) AnnotationUtils candidates.addAll(getConfigurationsForAnnotation(source, annotation));
.getAnnotationAttributes(annotation, true).get("value");
if (value.length > 0) {
candidates.addAll(Arrays.asList(value));
}
else {
candidates.addAll(SpringFactoriesLoader.loadFactoryNames(source,
getClass().getClassLoader()));
}
} }
collectCandidateConfigurations(annotation.annotationType(), candidates, seen); collectCandidateConfigurations(annotation.annotationType(), candidates, seen);
} }
private Collection<String> getConfigurationsForAnnotation(Class<?> source,
Annotation annotation) {
String[] value = (String[]) AnnotationUtils
.getAnnotationAttributes(annotation, true).get("value");
if (value.length > 0) {
return Arrays.asList(value);
}
return SpringFactoriesLoader.loadFactoryNames(source,
getClass().getClassLoader());
}
@Override @Override
protected Set<String> getExclusions(AnnotationMetadata metadata, protected Set<String> getExclusions(AnnotationMetadata metadata,
AnnotationAttributes attributes) { AnnotationAttributes attributes) {

View File

@ -331,10 +331,11 @@ via `@ConfigurationProperties`.
Spring Boot uses a very particular `PropertySource` order that is designed to allow Spring Boot uses a very particular `PropertySource` order that is designed to allow
sensible overriding of values. Properties are considered in the following order: sensible overriding of values. Properties are considered in the following order:
. {spring-javadoc}/test/context/TestPropertySource.{dc-ext}[`@TestPropertySource`] annotations . {spring-javadoc}/test/context/TestPropertySource.{dc-ext}[`@TestPropertySource`]
on your tests. annotations on your tests.
. Command line arguments. . Command line arguments.
. Properties from `SPRING_APPLICATION_JSON` (inline JSON embedded in an environment variable or system property) . Properties from `SPRING_APPLICATION_JSON` (inline JSON embedded in an environment
variable or system property)
. `ServletConfig` init parameters. . `ServletConfig` init parameters.
. `ServletContext` init parameters. . `ServletContext` init parameters.
. JNDI attributes from `java:comp/env`. . JNDI attributes from `java:comp/env`.
@ -351,8 +352,8 @@ sensible overriding of values. Properties are considered in the following order:
variants). variants).
. Application properties packaged inside your jar (`application.properties` and YAML . Application properties packaged inside your jar (`application.properties` and YAML
variants). variants).
. {spring-javadoc}/context/annotation/PropertySource.{dc-ext}[`@PropertySource`] annotations . {spring-javadoc}/context/annotation/PropertySource.{dc-ext}[`@PropertySource`]
on your `@Configuration` classes. annotations on your `@Configuration` classes.
. Default properties (specified using `SpringApplication.setDefaultProperties`). . Default properties (specified using `SpringApplication.setDefaultProperties`).
To provide a concrete example, suppose you develop a `@Component` that uses a To provide a concrete example, suppose you develop a `@Component` that uses a
@ -1091,13 +1092,13 @@ default `ERROR`, `WARN` and `INFO` level messages are logged. You can also enabl
NOTE: you can also specify `debug=true` in your `application.properties`. NOTE: you can also specify `debug=true` in your `application.properties`.
When the debug mode is enabled, a selection of core loggers (embedded container, Hibernate When the debug mode is enabled, a selection of core loggers (embedded container, Hibernate
and Spring Boot) are configured to output more information. Enabling the debug mode does _not_ and Spring Boot) are configured to output more information. Enabling the debug mode does
configure your application to log all messages with `DEBUG` level. _not_ configure your application to log all messages with `DEBUG` level.
Alternatively, you can enable a "`trace`" mode by starting your application with a `--trace` Alternatively, you can enable a "`trace`" mode by starting your application with a
flag (or `trace=true` in your `application.properties`). This will enable trace logging for a `--trace` flag (or `trace=true` in your `application.properties`). This will enable trace
selection of core loggers (embedded container, Hibernate schema generation and the whole Spring logging for a selection of core loggers (embedded container, Hibernate schema generation
portfolio). and the whole Spring portfolio).
[[boot-features-logging-color-coded-output]] [[boot-features-logging-color-coded-output]]
==== Color-coded output ==== Color-coded output
@ -1289,7 +1290,8 @@ To help with the customization some other properties are transferred from the Sp
|`logging.pattern.level` |`logging.pattern.level`
|`LOG_LEVEL_PATTERN` |`LOG_LEVEL_PATTERN`
|The format to use to render the log level (default `%5p`). (The `logging.pattern.level` form is only supported by Logback.) |The format to use to render the log level (default `%5p`). (The `logging.pattern.level`
form is only supported by Logback.)
|`PID` |`PID`
|`PID` |`PID`
@ -1319,7 +1321,8 @@ Logback). For example, if you use `logging.pattern.level=user:%X{user}
if it exists, e.g. if it exists, e.g.
---- ----
2015-09-30 12:30:04.031 user:juergen INFO 22174 --- [ nio-8080-exec-0] demo.Controller Handling authenticated request 2015-09-30 12:30:04.031 user:juergen INFO 22174 --- [ nio-8080-exec-0] demo.Controller
Handling authenticated request
---- ----
==== ====
@ -1492,8 +1495,8 @@ If you need to add or customize converters you can use Spring Boot's
} }
---- ----
Any `HttpMessageConverter` bean that is present in the context will be added to the list of Any `HttpMessageConverter` bean that is present in the context will be added to the list
converters. You can also override default converters that way. of converters. You can also override default converters that way.
@ -1530,8 +1533,8 @@ inner-classes. For example:
} }
---- ----
All `@JsonComponent` beans in the `ApplicationContext` will be automatically registered with All `@JsonComponent` beans in the `ApplicationContext` will be automatically registered
Jackson, and since `@JsonComponent` is meta-annotated with `@Component`, the usual with Jackson, and since `@JsonComponent` is meta-annotated with `@Component`, the usual
component-scanning rules apply. component-scanning rules apply.
Spring Boot also provides Spring Boot also provides
@ -1681,10 +1684,10 @@ the same data in HTML format (to customize it just add a `View` that resolves to
type `ErrorAttributes` to use the existing mechanism but replace the contents. type `ErrorAttributes` to use the existing mechanism but replace the contents.
TIP: The `BasicErrorController` can be used as a base class for a custom `ErrorController`. TIP: The `BasicErrorController` can be used as a base class for a custom `ErrorController`.
This is particularly useful if you want to add a handler for a new content type (the default This is particularly useful if you want to add a handler for a new content type (the
is to handle `text/html` specifically and provide a fallback for everything else). To do that default is to handle `text/html` specifically and provide a fallback for everything else).
just extend `BasicErrorController` and add a public method with a `@RequestMapping` that To do that just extend `BasicErrorController` and add a public method with a
has a `produces` attribute, and create a bean of your new type. `@RequestMapping` that has a `produces` attribute, and create a bean of your new type.
You can also define a `@ControllerAdvice` to customize the JSON document to return for a You can also define a `@ControllerAdvice` to customize the JSON document to return for a
particular controller and/or exception type. particular controller and/or exception type.
@ -1846,8 +1849,9 @@ auto-configuration for Spring HATEOAS that works well with most applications. Th
auto-configuration replaces the need to use `@EnableHypermediaSupport` and registers a auto-configuration replaces the need to use `@EnableHypermediaSupport` and registers a
number of beans to ease building hypermedia-based applications including a number of beans to ease building hypermedia-based applications including a
`LinkDiscoverers` (for client side support) and an `ObjectMapper` configured to correctly `LinkDiscoverers` (for client side support) and an `ObjectMapper` configured to correctly
marshal responses into the desired representation. The `ObjectMapper` will be customized based on the marshal responses into the desired representation. The `ObjectMapper` will be customized
`spring.jackson.*` properties or a `Jackson2ObjectMapperBuilder` bean if one exists. based on the `spring.jackson.*` properties or a `Jackson2ObjectMapperBuilder` bean if one
exists.
You can take control of Spring HATEOAS's configuration by using You can take control of Spring HATEOAS's configuration by using
`@EnableHypermediaSupport`. Note that this will disable the `ObjectMapper` customization `@EnableHypermediaSupport`. Note that this will disable the `ObjectMapper` customization
@ -2164,8 +2168,8 @@ it you normally use external properties and beans of type `WebSecurityConfigurer
(e.g. to add form-based login). To also switch off the authentication manager configuration (e.g. to add form-based login). To also switch off the authentication manager configuration
you can add a bean of type `AuthenticationManager`, or else configure the you can add a bean of type `AuthenticationManager`, or else configure the
global `AuthenticationManager` by autowiring an `AuthenticationManagerBuilder` into global `AuthenticationManager` by autowiring an `AuthenticationManagerBuilder` into
a method in one of your `@Configuration` classes. There are several secure applications in the a method in one of your `@Configuration` classes. There are several secure applications in
{github-code}/spring-boot-samples/[Spring Boot samples] to get you started with common the {github-code}/spring-boot-samples/[Spring Boot samples] to get you started with common
use cases. use cases.
The basic features you get out of the box in a web application are: The basic features you get out of the box in a web application are:
@ -3200,12 +3204,13 @@ Spring Data includes repository support for Neo4j.
In fact, both Spring Data JPA and Spring Data Neo4j share the same common In fact, both Spring Data JPA and Spring Data Neo4j share the same common
infrastructure; so you could take the JPA example from earlier and, assuming that `City` infrastructure; so you could take the JPA example from earlier and, assuming that `City`
is now a Neo4j OGM `@NodeEntity` rather than a JPA `@Entity`, it will work in the same way. is now a Neo4j OGM `@NodeEntity` rather than a JPA `@Entity`, it will work in the same
way.
TIP: You can customize entity scanning locations using the `@NodeEntityScan` annotation. TIP: You can customize entity scanning locations using the `@NodeEntityScan` annotation.
To enable repository support (and optionally support for `@Transactional`), add the following To enable repository support (and optionally support for `@Transactional`), add the
two annotations to your Spring configuration: following two annotations to your Spring configuration:
[source,java,indent=0] [source,java,indent=0]
---- ----
@ -3634,10 +3639,11 @@ you switch to a different JSR-107 implementation.
There are several ways to customize the underlying `javax.cache.cacheManager`: There are several ways to customize the underlying `javax.cache.cacheManager`:
* Caches can be created on startup via the `spring.cache.cache-names` property. If a custom * Caches can be created on startup via the `spring.cache.cache-names` property. If a
`javax.cache.configuration.Configuration` bean is defined, it is used to customize them. custom `javax.cache.configuration.Configuration` bean is defined, it is used to
customize them.
* `org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer` beans are * `org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer` beans are
invoked with the reference of the `CacheManager` for full customization. invoked with the reference of the `CacheManager` for full customization.
TIP: If a standard `javax.cache.CacheManager` bean is defined, it is wrapped TIP: If a standard `javax.cache.CacheManager` bean is defined, it is wrapped
automatically in a `org.springframework.cache.CacheManager` implementation that the automatically in a `org.springframework.cache.CacheManager` implementation that the
@ -4001,10 +4007,11 @@ beans are defined, they are associated automatically to the default factory.
The default factory is transactional by default. If you are running in an infrastructure The default factory is transactional by default. If you are running in an infrastructure
where a `JtaTransactionManager` is present, it will be associated to the listener container where a `JtaTransactionManager` is present, it will be associated to the listener container
by default. If not, the `sessionTransacted` flag will be enabled. In that latter scenario, by default. If not, the `sessionTransacted` flag will be enabled. In that latter scenario,
you can associate your local data store transaction to the processing of an incoming message you can associate your local data store transaction to the processing of an incoming
by adding `@Transactional` on your listener method (or a delegate thereof). This will make message by adding `@Transactional` on your listener method (or a delegate thereof). This
sure that the incoming message is acknowledged once the local transaction has completed. This will make sure that the incoming message is acknowledged once the local transaction has
also includes sending response messages that have been performed on the same JMS session. completed. This also includes sending response messages that have been performed on the
same JMS session.
The following component creates a listener endpoint on the `someQueue` destination: The following component creates a listener endpoint on the `someQueue` destination:
@ -4021,15 +4028,16 @@ The following component creates a listener endpoint on the `someQueue` destinati
} }
---- ----
TIP: Check {spring-javadoc}/jms/annotation/EnableJms.{dc-ext}[the Javadoc of `@EnableJms`] for TIP: Check {spring-javadoc}/jms/annotation/EnableJms.{dc-ext}[the Javadoc of `@EnableJms`]
more details. for more details.
If you need to create more `JmsListenerContainerFactory` instances or if you want to override If you need to create more `JmsListenerContainerFactory` instances or if you want to
the default, Spring Boot provides a `DefaultJmsListenerContainerFactoryConfigurer` that you override the default, Spring Boot provides a `DefaultJmsListenerContainerFactoryConfigurer`
can use to initialize a `DefaultJmsListenerContainerFactory` with the same settings as the one that you can use to initialize a `DefaultJmsListenerContainerFactory` with the same
that is auto-configured. settings as the one that is auto-configured.
For instance, the following exposes another factory that uses a specific `MessageConverter`: For instance, the following exposes another factory that uses a specific
`MessageConverter`:
[source,java,indent=0] [source,java,indent=0]
---- ----
@ -4130,8 +4138,8 @@ directly into your own beans:
---- ----
NOTE: {spring-amqp-javadoc}/rabbit/core/RabbitMessagingTemplate.{dc-ext}[`RabbitMessagingTemplate`] NOTE: {spring-amqp-javadoc}/rabbit/core/RabbitMessagingTemplate.{dc-ext}[`RabbitMessagingTemplate`]
can be injected in a similar manner. If a `MessageConverter` bean is defined, it is associated can be injected in a similar manner. If a `MessageConverter` bean is defined, it is
automatically to the auto-configured `AmqpTemplate`. associated automatically to the auto-configured `AmqpTemplate`.
Any `org.springframework.amqp.core.Queue` that is defined as a bean will be automatically Any `org.springframework.amqp.core.Queue` that is defined as a bean will be automatically
used to declare a corresponding queue on the RabbitMQ instance if necessary. used to declare a corresponding queue on the RabbitMQ instance if necessary.
@ -4164,12 +4172,14 @@ The following component creates a listener endpoint on the `someQueue` queue:
TIP: Check {spring-amqp-javadoc}/rabbit/annotation/EnableRabbit.{dc-ext}[the Javadoc of `@EnableRabbit`] TIP: Check {spring-amqp-javadoc}/rabbit/annotation/EnableRabbit.{dc-ext}[the Javadoc of `@EnableRabbit`]
for more details. for more details.
If you need to create more `RabbitListenerContainerFactory` instances or if you want to override If you need to create more `RabbitListenerContainerFactory` instances or if you want to
the default, Spring Boot provides a `SimpleRabbitListenerContainerFactoryConfigurer` that you can override the default, Spring Boot provides a
use to initialize a `SimpleRabbitListenerContainerFactory` with the same settings as the one that `SimpleRabbitListenerContainerFactoryConfigurer` that you can use to initialize a
is auto-configured. `SimpleRabbitListenerContainerFactory` with the same settings as the one that is
auto-configured.
For instance, the following exposes another factory that uses a specific `MessageConverter`: For instance, the following exposes another factory that uses a specific
`MessageConverter`:
[source,java,indent=0] [source,java,indent=0]
---- ----
@ -4205,15 +4215,15 @@ Then you can use in any `@RabbitListener`-annotated method as follows:
---- ----
You can enable retries to handle situations where your listener throws an exception. You can enable retries to handle situations where your listener throws an exception.
When retries are exhausted, the message will be rejected and either dropped or routed to a dead-letter exchange When retries are exhausted, the message will be rejected and either dropped or routed to a
if the broker is configured so. dead-letter exchange if the broker is configured so. Retries are disabled by default.
Retries are disabled by default.
IMPORTANT: If retries are not enabled and the listener throws an exception, by default the delivery will be retried IMPORTANT: If retries are not enabled and the listener throws an exception, by default the
indefinitely. delivery will be retried indefinitely. You can modify this behavior in two ways; set the
You can modify this behavior in two ways; set the `defaultRequeueRejected` property to `false` and zero redeliveries `defaultRequeueRejected` property to `false` and zero re-deliveries will be attempted; or,
will be attempted; or, throw an `AmqpRejectAndDontRequeueException` to signal the message should be rejected. throw an `AmqpRejectAndDontRequeueException` to signal the message should be rejected.
This is the mechanism used when retries are enabled and the maximum delivery attempts are reached. This is the mechanism used when retries are enabled and the maximum delivery attempts are
reached.
@ -4495,8 +4505,8 @@ If you use the
the following provided libraries: the following provided libraries:
* http://junit.org[JUnit] -- The de-facto standard for unit testing Java applications. * http://junit.org[JUnit] -- The de-facto standard for unit testing Java applications.
* {spring-reference}/#integration-testing.html[Spring Test] & Spring Boot Test -- Utilities and integration test support for Spring Boot * {spring-reference}/#integration-testing.html[Spring Test] & Spring Boot Test --
applications. Utilities and integration test support for Spring Boot applications.
* http://joel-costigliola.github.io/assertj/[AssertJ] -- A fluent assertion library. * http://joel-costigliola.github.io/assertj/[AssertJ] -- A fluent assertion library.
* http://hamcrest.org/JavaHamcrest/[Hamcrest] -- A library of matcher objects (also known * http://hamcrest.org/JavaHamcrest/[Hamcrest] -- A library of matcher objects (also known
as constraints or predicates). as constraints or predicates).
@ -4839,7 +4849,8 @@ controllers without needing to start a full HTTP server.
---- ----
TIP: If you need to configure elements of the auto-configuration (for example when servlet TIP: If you need to configure elements of the auto-configuration (for example when servlet
filters should be applied) you can use attributes in the `@AutoConfigureMockMvc` annotation. filters should be applied) you can use attributes in the `@AutoConfigureMockMvc`
annotation.
If you use HtmlUnit or Selenium, auto-configuration will also provide a `WebClient` bean If you use HtmlUnit or Selenium, auto-configuration will also provide a `WebClient` bean
and/or a `WebDriver` bean. Here is an example that uses HtmlUnit: and/or a `WebDriver` bean. Here is an example that uses HtmlUnit:
@ -5116,8 +5127,8 @@ public class MyTest {
[[boot-features-rest-templates-test-utility]] [[boot-features-rest-templates-test-utility]]
==== TestRestTemplate ==== TestRestTemplate
`TestRestTemplate` is a convenience subclass of Spring's `RestTemplate` that is useful in `TestRestTemplate` is a convenience alternative to Spring's `RestTemplate` that is useful
integration tests. You can get a vanilla template or one that sends Basic HTTP in integration tests. You can get a vanilla template or one that sends Basic HTTP
authentication (with a username and password). In either case the template will behave authentication (with a username and password). In either case the template will behave
in a test-friendly way: not following redirects (so you can assert the response location), in a test-friendly way: not following redirects (so you can assert the response location),
ignoring cookies (so the template is stateless), and not throwing exceptions on ignoring cookies (so the template is stateless), and not throwing exceptions on
@ -5129,7 +5140,7 @@ will respond by configuring the client appropriately.
---- ----
public class MyTest { public class MyTest {
RestTemplate template = new TestRestTemplate(); private TestRestTemplate template = new TestRestTemplate();
@Test @Test
public void testRequest() throws Exception { public void testRequest() throws Exception {