Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
6daad1f5
Commit
6daad1f5
authored
Jan 08, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow to inject the Environment in FailureAnalyzer
Closes gh-11569
parent
6a0dbc5c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
8 deletions
+30
-8
howto.adoc
...oot-project/spring-boot-docs/src/main/asciidoc/howto.adoc
+3
-0
FailureAnalyzers.java
...rg/springframework/boot/diagnostics/FailureAnalyzers.java
+5
-1
FailureAnalyzersTests.java
...ringframework/boot/diagnostics/FailureAnalyzersTests.java
+21
-6
basic.factories
...rc/test/resources/failure-analyzers-tests/basic.factories
+1
-1
No files found.
spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc
View file @
6daad1f5
...
@@ -48,6 +48,9 @@ The following example registers `ProjectConstraintViolationFailureAnalyzer`:
...
@@ -48,6 +48,9 @@ The following example registers `ProjectConstraintViolationFailureAnalyzer`:
com.example.ProjectConstraintViolationFailureAnalyzer
com.example.ProjectConstraintViolationFailureAnalyzer
----
----
NOTE: If you need access to the `BeanFactory` or the `Environment`, your `FailureAnalyzer`
can simply implement `BeanFactoryAware` or `EnvironmentAware` respectively.
[[howto-troubleshoot-auto-configuration]]
[[howto-troubleshoot-auto-configuration]]
...
...
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java
View file @
6daad1f5
/*
/*
* Copyright 2012-201
7
the original author or authors.
* Copyright 2012-201
8
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -27,6 +27,7 @@ import org.springframework.beans.factory.BeanFactory;
...
@@ -27,6 +27,7 @@ import org.springframework.beans.factory.BeanFactory;
import
org.springframework.beans.factory.BeanFactoryAware
;
import
org.springframework.beans.factory.BeanFactoryAware
;
import
org.springframework.boot.SpringBootExceptionReporter
;
import
org.springframework.boot.SpringBootExceptionReporter
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.EnvironmentAware
;
import
org.springframework.core.annotation.AnnotationAwareOrderComparator
;
import
org.springframework.core.annotation.AnnotationAwareOrderComparator
;
import
org.springframework.core.io.support.SpringFactoriesLoader
;
import
org.springframework.core.io.support.SpringFactoriesLoader
;
import
org.springframework.util.Assert
;
import
org.springframework.util.Assert
;
...
@@ -96,6 +97,9 @@ final class FailureAnalyzers implements SpringBootExceptionReporter {
...
@@ -96,6 +97,9 @@ final class FailureAnalyzers implements SpringBootExceptionReporter {
if
(
analyzer
instanceof
BeanFactoryAware
)
{
if
(
analyzer
instanceof
BeanFactoryAware
)
{
((
BeanFactoryAware
)
analyzer
).
setBeanFactory
(
context
.
getBeanFactory
());
((
BeanFactoryAware
)
analyzer
).
setBeanFactory
(
context
.
getBeanFactory
());
}
}
if
(
analyzer
instanceof
EnvironmentAware
)
{
((
EnvironmentAware
)
analyzer
).
setEnvironment
(
context
.
getEnvironment
());
}
}
}
@Override
@Override
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java
View file @
6daad1f5
/*
/*
* Copyright 2012-201
7
the original author or authors.
* Copyright 2012-201
8
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -26,7 +26,9 @@ import org.junit.Test;
...
@@ -26,7 +26,9 @@ import org.junit.Test;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.beans.factory.BeanFactoryAware
;
import
org.springframework.beans.factory.BeanFactoryAware
;
import
org.springframework.context.EnvironmentAware
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.core.env.Environment
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
mock
;
...
@@ -37,14 +39,15 @@ import static org.mockito.Mockito.verify;
...
@@ -37,14 +39,15 @@ import static org.mockito.Mockito.verify;
* Tests for {@link FailureAnalyzers}.
* Tests for {@link FailureAnalyzers}.
*
*
* @author Andy Wilkinson
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
*/
public
class
FailureAnalyzersTests
{
public
class
FailureAnalyzersTests
{
private
static
BeanFactory
AwareFailureAnalyzer
failureAnalyzer
;
private
static
AwareFailureAnalyzer
failureAnalyzer
;
@Before
@Before
public
void
configureMock
()
{
public
void
configureMock
()
{
failureAnalyzer
=
mock
(
BeanFactory
AwareFailureAnalyzer
.
class
);
failureAnalyzer
=
mock
(
AwareFailureAnalyzer
.
class
);
}
}
@Test
@Test
...
@@ -61,6 +64,13 @@ public class FailureAnalyzersTests {
...
@@ -61,6 +64,13 @@ public class FailureAnalyzersTests {
verify
(
failureAnalyzer
).
setBeanFactory
(
any
(
BeanFactory
.
class
));
verify
(
failureAnalyzer
).
setBeanFactory
(
any
(
BeanFactory
.
class
));
}
}
@Test
public
void
environmentIsInjectedIntEnvironmentAwareFailureAnalyzers
()
{
RuntimeException
failure
=
new
RuntimeException
();
analyzeAndReport
(
"basic.factories"
,
failure
);
verify
(
failureAnalyzer
).
setEnvironment
(
any
(
Environment
.
class
));
}
@Test
@Test
public
void
analyzerThatFailsDuringInitializationDoesNotPreventOtherAnalyzersFromBeingCalled
()
{
public
void
analyzerThatFailsDuringInitializationDoesNotPreventOtherAnalyzersFromBeingCalled
()
{
RuntimeException
failure
=
new
RuntimeException
();
RuntimeException
failure
=
new
RuntimeException
();
...
@@ -113,12 +123,17 @@ public class FailureAnalyzersTests {
...
@@ -113,12 +123,17 @@ public class FailureAnalyzersTests {
}
}
interface
BeanFactoryAwareFailureAnalyzer
extends
BeanFactory
Aware
,
FailureAnalyzer
{
interface
AwareFailureAnalyzer
extends
BeanFactoryAware
,
Environment
Aware
,
FailureAnalyzer
{
}
}
static
class
StandardBeanFactoryAwareFailureAnalyzer
extends
BasicFailureAnalyzer
static
class
StandardAwareFailureAnalyzer
extends
BasicFailureAnalyzer
implements
BeanFactoryAwareFailureAnalyzer
{
implements
AwareFailureAnalyzer
{
@Override
public
void
setEnvironment
(
Environment
environment
)
{
failureAnalyzer
.
setEnvironment
(
environment
);
}
@Override
@Override
public
void
setBeanFactory
(
BeanFactory
beanFactory
)
throws
BeansException
{
public
void
setBeanFactory
(
BeanFactory
beanFactory
)
throws
BeansException
{
...
...
spring-boot-project/spring-boot/src/test/resources/failure-analyzers-tests/basic.factories
View file @
6daad1f5
# Failure Analyzers
# Failure Analyzers
org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.diagnostics.FailureAnalyzersTests$BasicFailureAnalyzer,\
org.springframework.boot.diagnostics.FailureAnalyzersTests$BasicFailureAnalyzer,\
org.springframework.boot.diagnostics.FailureAnalyzersTests$Standard
BeanFactory
AwareFailureAnalyzer
org.springframework.boot.diagnostics.FailureAnalyzersTests$StandardAwareFailureAnalyzer
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment