Skip to content

Commit caa22b7

Browse files
committed
Properties loading with ignoreResourceNotFound covers SocketException as well
Closes gh-25717
1 parent 5866773 commit caa22b7

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.io.FileNotFoundException;
2020
import java.io.IOException;
2121
import java.lang.annotation.Annotation;
22+
import java.net.SocketException;
2223
import java.net.UnknownHostException;
2324
import java.util.ArrayDeque;
2425
import java.util.ArrayList;
@@ -458,7 +459,8 @@ private void processPropertySource(AnnotationAttributes propertySource) throws I
458459
catch (IOException ex) {
459460
// Resource not found when trying to open it
460461
if (ignoreResourceNotFound &&
461-
(ex instanceof FileNotFoundException || ex instanceof UnknownHostException)) {
462+
(ex instanceof FileNotFoundException || ex instanceof UnknownHostException ||
463+
ex instanceof SocketException)) {
462464
if (logger.isInfoEnabled()) {
463465
logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage());
464466
}
@@ -473,32 +475,35 @@ private void processPropertySource(AnnotationAttributes propertySource) throws I
473475
private void addPropertySource(PropertySource<?> propertySource) {
474476
String name = propertySource.getName();
475477
MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
476-
if (propertySources.contains(name) && this.propertySourceNames.contains(name)) {
478+
479+
if (this.propertySourceNames.contains(name)) {
477480
// We've already added a version, we need to extend it
478481
PropertySource<?> existing = propertySources.get(name);
479-
PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ?
480-
((ResourcePropertySource) propertySource).withResourceName() : propertySource);
481-
if (existing instanceof CompositePropertySource) {
482-
((CompositePropertySource) existing).addFirstPropertySource(newSource);
483-
}
484-
else {
485-
if (existing instanceof ResourcePropertySource) {
486-
existing = ((ResourcePropertySource) existing).withResourceName();
482+
if (existing != null) {
483+
PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ?
484+
((ResourcePropertySource) propertySource).withResourceName() : propertySource);
485+
if (existing instanceof CompositePropertySource) {
486+
((CompositePropertySource) existing).addFirstPropertySource(newSource);
487+
}
488+
else {
489+
if (existing instanceof ResourcePropertySource) {
490+
existing = ((ResourcePropertySource) existing).withResourceName();
491+
}
492+
CompositePropertySource composite = new CompositePropertySource(name);
493+
composite.addPropertySource(newSource);
494+
composite.addPropertySource(existing);
495+
propertySources.replace(name, composite);
487496
}
488-
CompositePropertySource composite = new CompositePropertySource(name);
489-
composite.addPropertySource(newSource);
490-
composite.addPropertySource(existing);
491-
propertySources.replace(name, composite);
497+
return;
492498
}
493499
}
500+
501+
if (this.propertySourceNames.isEmpty()) {
502+
propertySources.addLast(propertySource);
503+
}
494504
else {
495-
if (this.propertySourceNames.isEmpty()) {
496-
propertySources.addLast(propertySource);
497-
}
498-
else {
499-
String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1);
500-
propertySources.addBefore(firstProcessed, propertySource);
501-
}
505+
String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1);
506+
propertySources.addBefore(firstProcessed, propertySource);
502507
}
503508
this.propertySourceNames.add(name);
504509
}
@@ -675,7 +680,7 @@ SourceClass asSourceClass(Class<?> classType) throws IOException {
675680
}
676681

677682
/**
678-
* Factory method to obtain {@link SourceClass}s from class names.
683+
* Factory method to obtain a {@link SourceClass} collection from class names.
679684
*/
680685
private Collection<SourceClass> asSourceClasses(String[] classNames) throws IOException {
681686
List<SourceClass> annotatedClasses = new ArrayList<SourceClass>(classNames.length);

spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.io.FileNotFoundException;
2020
import java.io.IOException;
21+
import java.net.SocketException;
2122
import java.net.UnknownHostException;
2223
import java.util.Properties;
2324

@@ -180,7 +181,8 @@ protected void loadProperties(Properties props) throws IOException {
180181
catch (IOException ex) {
181182
// Resource not found when trying to open it
182183
if (this.ignoreResourceNotFound &&
183-
(ex instanceof FileNotFoundException || ex instanceof UnknownHostException)) {
184+
(ex instanceof FileNotFoundException || ex instanceof UnknownHostException ||
185+
ex instanceof SocketException)) {
184186
if (logger.isInfoEnabled()) {
185187
logger.info("Properties resource not found: " + ex.getMessage());
186188
}

0 commit comments

Comments
 (0)