Merge branch '1.4.x' into 1.5.x
This commit is contained in:
commit
d3e06c4627
|
@ -56,14 +56,13 @@ public class EmbeddedDataSourceConfigurationTests {
|
||||||
@Test
|
@Test
|
||||||
public void generateUniqueName() throws Exception {
|
public void generateUniqueName() throws Exception {
|
||||||
this.context = load("spring.datasource.generate-unique-name=true");
|
this.context = load("spring.datasource.generate-unique-name=true");
|
||||||
AnnotationConfigApplicationContext context2 =
|
AnnotationConfigApplicationContext context2 = load(
|
||||||
load("spring.datasource.generate-unique-name=true");
|
"spring.datasource.generate-unique-name=true");
|
||||||
try {
|
try {
|
||||||
DataSource dataSource = this.context.getBean(DataSource.class);
|
DataSource dataSource = this.context.getBean(DataSource.class);
|
||||||
DataSource dataSource2 = context2.getBean(DataSource.class);
|
DataSource dataSource2 = context2.getBean(DataSource.class);
|
||||||
assertThat(getDatabaseName(dataSource))
|
assertThat(getDatabaseName(dataSource))
|
||||||
.isNotEqualTo(getDatabaseName(dataSource2));
|
.isNotEqualTo(getDatabaseName(dataSource2));
|
||||||
System.out.println(dataSource2);
|
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
context2.close();
|
context2.close();
|
||||||
|
|
|
@ -478,8 +478,11 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
public Object postProcessAfterInitialization(Object bean, String beanName)
|
||||||
throws BeansException {
|
throws BeansException {
|
||||||
|
if (bean instanceof FactoryBean) {
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
return createSpyIfNecessary(bean, beanName);
|
return createSpyIfNecessary(bean, beanName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,40 +39,42 @@ class BeanCurrentlyInCreationFailureAnalyzer
|
||||||
@Override
|
@Override
|
||||||
protected FailureAnalysis analyze(Throwable rootFailure,
|
protected FailureAnalysis analyze(Throwable rootFailure,
|
||||||
BeanCurrentlyInCreationException cause) {
|
BeanCurrentlyInCreationException cause) {
|
||||||
List<BeanInCycle> beansInCycle = new ArrayList<BeanInCycle>();
|
List<BeanInCycle> cycle = new ArrayList<BeanInCycle>();
|
||||||
Throwable candidate = rootFailure;
|
Throwable candidate = rootFailure;
|
||||||
int cycleStart = -1;
|
int cycleStart = -1;
|
||||||
while (candidate != null) {
|
while (candidate != null) {
|
||||||
if (candidate instanceof BeanCreationException) {
|
BeanInCycle beanInCycle = BeanInCycle.get(candidate);
|
||||||
BeanCreationException creationEx = (BeanCreationException) candidate;
|
if (beanInCycle != null) {
|
||||||
if (StringUtils.hasText(creationEx.getBeanName())) {
|
int index = cycle.indexOf(beanInCycle);
|
||||||
BeanInCycle beanInCycle = new BeanInCycle(creationEx);
|
|
||||||
int index = beansInCycle.indexOf(beanInCycle);
|
|
||||||
if (index == -1) {
|
if (index == -1) {
|
||||||
beansInCycle.add(beanInCycle);
|
cycle.add(beanInCycle);
|
||||||
}
|
|
||||||
else {
|
|
||||||
cycleStart = index;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
cycleStart = (cycleStart == -1 ? index : cycleStart);
|
||||||
}
|
}
|
||||||
candidate = candidate.getCause();
|
candidate = candidate.getCause();
|
||||||
}
|
}
|
||||||
|
String message = buildMessage(cycle, cycleStart);
|
||||||
|
return new FailureAnalysis(message, null, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildMessage(List<BeanInCycle> beansInCycle, int cycleStart) {
|
||||||
StringBuilder message = new StringBuilder();
|
StringBuilder message = new StringBuilder();
|
||||||
message.append(String.format("The dependencies of some of the beans in the "
|
message.append(String.format("The dependencies of some of the beans in the "
|
||||||
+ "application context form a cycle:%n%n"));
|
+ "application context form a cycle:%n%n"));
|
||||||
for (int i = 0; i < beansInCycle.size(); i++) {
|
for (int i = 0; i < beansInCycle.size(); i++) {
|
||||||
|
BeanInCycle beanInCycle = beansInCycle.get(i);
|
||||||
if (i == cycleStart) {
|
if (i == cycleStart) {
|
||||||
message.append(String.format("┌─────┐%n"));
|
message.append(String.format("┌─────┐%n"));
|
||||||
}
|
}
|
||||||
else if (i > 0) {
|
else if (i > 0) {
|
||||||
message.append(String.format("%s ↓%n", i < cycleStart ? " " : "↑"));
|
String leftSide = (i < cycleStart ? " " : "↑");
|
||||||
|
message.append(String.format("%s ↓%n", leftSide));
|
||||||
}
|
}
|
||||||
message.append(String.format("%s %s%n", i < cycleStart ? " " : "|",
|
String leftSide = i < cycleStart ? " " : "|";
|
||||||
beansInCycle.get(i)));
|
message.append(String.format("%s %s%n", leftSide, beanInCycle));
|
||||||
}
|
}
|
||||||
message.append(String.format("└─────┘%n"));
|
message.append(String.format("└─────┘%n"));
|
||||||
return new FailureAnalysis(message.toString(), null, cause);
|
return message.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class BeanInCycle {
|
private static final class BeanInCycle {
|
||||||
|
@ -114,14 +116,10 @@ class BeanCurrentlyInCreationFailureAnalyzer
|
||||||
if (this == obj) {
|
if (this == obj) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (obj == null) {
|
if (obj == null || getClass() != obj.getClass()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (getClass() != obj.getClass()) {
|
return this.name.equals(((BeanInCycle) obj).name);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
BeanInCycle other = (BeanInCycle) obj;
|
|
||||||
return this.name.equals(other.name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -129,6 +127,20 @@ class BeanCurrentlyInCreationFailureAnalyzer
|
||||||
return this.name + this.description;
|
return this.name + this.description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static BeanInCycle get(Throwable ex) {
|
||||||
|
if (ex instanceof BeanCreationException) {
|
||||||
|
return get((BeanCreationException) ex);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static BeanInCycle get(BeanCreationException ex) {
|
||||||
|
if (StringUtils.hasText(ex.getBeanName())) {
|
||||||
|
return new BeanInCycle(ex);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue