Skip to content

Commit 56c6618

Browse files
committed
Avoid package cycle through dedicated ResourcePropertiesPersister
See gh-25151
1 parent 8eedd9d commit 56c6618

File tree

7 files changed

+100
-41
lines changed

7 files changed

+100
-41
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/PropertiesBeanDefinitionReader.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
import org.springframework.beans.factory.config.RuntimeBeanReference;
3636
import org.springframework.core.io.Resource;
3737
import org.springframework.core.io.support.EncodedResource;
38+
import org.springframework.core.io.support.ResourcePropertiesPersister;
3839
import org.springframework.lang.Nullable;
39-
import org.springframework.util.DefaultPropertiesPersister;
4040
import org.springframework.util.PropertiesPersister;
4141
import org.springframework.util.StringUtils;
4242

@@ -148,7 +148,7 @@ public class PropertiesBeanDefinitionReader extends AbstractBeanDefinitionReader
148148
@Nullable
149149
private String defaultParentBean;
150150

151-
private PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
151+
private PropertiesPersister propertiesPersister = ResourcePropertiesPersister.INSTANCE;
152152

153153

154154
/**
@@ -187,12 +187,12 @@ public String getDefaultParentBean() {
187187

188188
/**
189189
* Set the PropertiesPersister to use for parsing properties files.
190-
* The default is DefaultPropertiesPersister.
191-
* @see org.springframework.util.DefaultPropertiesPersister
190+
* The default is ResourcePropertiesPersister.
191+
* @see ResourcePropertiesPersister#INSTANCE
192192
*/
193193
public void setPropertiesPersister(@Nullable PropertiesPersister propertiesPersister) {
194194
this.propertiesPersister =
195-
(propertiesPersister != null ? propertiesPersister : new DefaultPropertiesPersister());
195+
(propertiesPersister != null ? propertiesPersister : ResourcePropertiesPersister.INSTANCE);
196196
}
197197

198198
/**

spring-context/src/main/java/org/springframework/context/support/ReloadableResourceBundleMessageSource.java

Lines changed: 7 additions & 7 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.
@@ -33,8 +33,8 @@
3333
import org.springframework.core.io.DefaultResourceLoader;
3434
import org.springframework.core.io.Resource;
3535
import org.springframework.core.io.ResourceLoader;
36+
import org.springframework.core.io.support.ResourcePropertiesPersister;
3637
import org.springframework.lang.Nullable;
37-
import org.springframework.util.DefaultPropertiesPersister;
3838
import org.springframework.util.PropertiesPersister;
3939
import org.springframework.util.StringUtils;
4040

@@ -80,7 +80,7 @@
8080
* @see #setFileEncodings
8181
* @see #setPropertiesPersister
8282
* @see #setResourceLoader
83-
* @see org.springframework.util.DefaultPropertiesPersister
83+
* @see ResourcePropertiesPersister
8484
* @see org.springframework.core.io.DefaultResourceLoader
8585
* @see ResourceBundleMessageSource
8686
* @see java.util.ResourceBundle
@@ -98,7 +98,7 @@ public class ReloadableResourceBundleMessageSource extends AbstractResourceBased
9898

9999
private boolean concurrentRefresh = true;
100100

101-
private PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
101+
private PropertiesPersister propertiesPersister = ResourcePropertiesPersister.INSTANCE;
102102

103103
private ResourceLoader resourceLoader = new DefaultResourceLoader();
104104

@@ -143,12 +143,12 @@ public void setConcurrentRefresh(boolean concurrentRefresh) {
143143

144144
/**
145145
* Set the PropertiesPersister to use for parsing properties files.
146-
* <p>The default is a DefaultPropertiesPersister.
147-
* @see org.springframework.util.DefaultPropertiesPersister
146+
* <p>The default is ResourcePropertiesPersister.
147+
* @see ResourcePropertiesPersister#INSTANCE
148148
*/
149149
public void setPropertiesPersister(@Nullable PropertiesPersister propertiesPersister) {
150150
this.propertiesPersister =
151-
(propertiesPersister != null ? propertiesPersister : new DefaultPropertiesPersister());
151+
(propertiesPersister != null ? propertiesPersister : ResourcePropertiesPersister.INSTANCE);
152152
}
153153

154154
/**

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 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.
@@ -27,7 +27,6 @@
2727
import org.springframework.core.io.Resource;
2828
import org.springframework.lang.Nullable;
2929
import org.springframework.util.CollectionUtils;
30-
import org.springframework.util.DefaultPropertiesPersister;
3130
import org.springframework.util.PropertiesPersister;
3231

3332
/**
@@ -56,7 +55,7 @@ public abstract class PropertiesLoaderSupport {
5655
@Nullable
5756
private String fileEncoding;
5857

59-
private PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
58+
private PropertiesPersister propertiesPersister = ResourcePropertiesPersister.INSTANCE;
6059

6160

6261
/**
@@ -130,12 +129,12 @@ public void setFileEncoding(String encoding) {
130129

131130
/**
132131
* Set the PropertiesPersister to use for parsing properties files.
133-
* The default is DefaultPropertiesPersister.
134-
* @see org.springframework.util.DefaultPropertiesPersister
132+
* The default is ResourcePropertiesPersister.
133+
* @see ResourcePropertiesPersister#INSTANCE
135134
*/
136135
public void setPropertiesPersister(@Nullable PropertiesPersister propertiesPersister) {
137136
this.propertiesPersister =
138-
(propertiesPersister != null ? propertiesPersister : new DefaultPropertiesPersister());
137+
(propertiesPersister != null ? propertiesPersister : ResourcePropertiesPersister.INSTANCE);
139138
}
140139

141140

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.springframework.lang.Nullable;
3030
import org.springframework.util.Assert;
3131
import org.springframework.util.ClassUtils;
32-
import org.springframework.util.DefaultPropertiesPersister;
3332
import org.springframework.util.PropertiesPersister;
3433
import org.springframework.util.ResourceUtils;
3534

@@ -79,7 +78,7 @@ public static Properties loadProperties(EncodedResource resource) throws IOExcep
7978
public static void fillProperties(Properties props, EncodedResource resource)
8079
throws IOException {
8180

82-
fillProperties(props, resource, new DefaultPropertiesPersister());
81+
fillProperties(props, resource, ResourcePropertiesPersister.INSTANCE);
8382
}
8483

8584
/**
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2002-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.io.support;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.io.OutputStream;
22+
import java.util.Properties;
23+
24+
import org.springframework.core.SpringProperties;
25+
import org.springframework.util.DefaultPropertiesPersister;
26+
27+
/**
28+
* Spring-aware subclass of the plain {@link DefaultPropertiesPersister},
29+
* adding a conditional check for disabled XML support through the shared
30+
* "spring.xml.ignore" property.
31+
*
32+
* <p>This is the standard implementation used in Spring's resource support.
33+
*
34+
* @author Juergen Hoeller
35+
* @author Sebastien Deleuze
36+
* @since 5.3
37+
*/
38+
public class ResourcePropertiesPersister extends DefaultPropertiesPersister {
39+
40+
/**
41+
* A convenient constant for a default {@code ResourcePropertiesPersister} instance,
42+
* as used in Spring's common resource support.
43+
* @since 5.3
44+
*/
45+
public static final ResourcePropertiesPersister INSTANCE = new ResourcePropertiesPersister();
46+
47+
/**
48+
* Boolean flag controlled by a {@code spring.xml.ignore} system property that instructs Spring to
49+
* ignore XML, i.e. to not initialize the XML-related infrastructure.
50+
* <p>The default is "false".
51+
*/
52+
private static final boolean shouldIgnoreXml = SpringProperties.getFlag("spring.xml.ignore");
53+
54+
55+
@Override
56+
public void loadFromXml(Properties props, InputStream is) throws IOException {
57+
if (shouldIgnoreXml) {
58+
throw new UnsupportedOperationException("XML support disabled");
59+
}
60+
super.loadFromXml(props, is);
61+
}
62+
63+
@Override
64+
public void storeToXml(Properties props, OutputStream os, String header) throws IOException {
65+
if (shouldIgnoreXml) {
66+
throw new UnsupportedOperationException("XML support disabled");
67+
}
68+
super.storeToXml(props, os, header);
69+
}
70+
71+
@Override
72+
public void storeToXml(Properties props, OutputStream os, String header, String encoding) throws IOException {
73+
if (shouldIgnoreXml) {
74+
throw new UnsupportedOperationException("XML support disabled");
75+
}
76+
super.storeToXml(props, os, header, encoding);
77+
}
78+
79+
}

spring-core/src/main/java/org/springframework/util/DefaultPropertiesPersister.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import java.io.Writer;
2424
import java.util.Properties;
2525

26-
import org.springframework.core.SpringProperties;
27-
2826
/**
2927
* Default implementation of the {@link PropertiesPersister} interface.
3028
* Follows the native parsing of {@code java.util.Properties}.
@@ -48,22 +46,14 @@
4846
* "defaultEncoding" and "fileEncodings" properties).
4947
*
5048
* @author Juergen Hoeller
51-
* @author Sebastien Deleuze
5249
* @since 10.03.2004
5350
* @see java.util.Properties
5451
* @see java.util.Properties#load
5552
* @see java.util.Properties#store
53+
* @see org.springframework.core.io.support.ResourcePropertiesPersister
5654
*/
5755
public class DefaultPropertiesPersister implements PropertiesPersister {
5856

59-
/**
60-
* Boolean flag controlled by a {@code spring.xml.ignore} system property that instructs Spring to
61-
* ignore XML, i.e. to not initialize the XML-related infrastructure.
62-
* <p>The default is "false".
63-
*/
64-
private static final boolean shouldIgnoreXml = SpringProperties.getFlag("spring.xml.ignore");
65-
66-
6757
@Override
6858
public void load(Properties props, InputStream is) throws IOException {
6959
props.load(is);
@@ -86,25 +76,16 @@ public void store(Properties props, Writer writer, String header) throws IOExcep
8676

8777
@Override
8878
public void loadFromXml(Properties props, InputStream is) throws IOException {
89-
if (shouldIgnoreXml) {
90-
throw new UnsupportedOperationException("XML support disabled");
91-
}
9279
props.loadFromXML(is);
9380
}
9481

9582
@Override
9683
public void storeToXml(Properties props, OutputStream os, String header) throws IOException {
97-
if (shouldIgnoreXml) {
98-
throw new UnsupportedOperationException("XML support disabled");
99-
}
10084
props.storeToXML(os, header);
10185
}
10286

10387
@Override
10488
public void storeToXml(Properties props, OutputStream os, String header, String encoding) throws IOException {
105-
if (shouldIgnoreXml) {
106-
throw new UnsupportedOperationException("XML support disabled");
107-
}
10889
props.storeToXML(os, header, encoding);
10990
}
11091

spring-core/src/main/java/org/springframework/util/PropertiesPersister.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 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.
@@ -35,6 +35,7 @@
3535
* @author Juergen Hoeller
3636
* @since 10.03.2004
3737
* @see DefaultPropertiesPersister
38+
* @see org.springframework.core.io.support.ResourcePropertiesPersister
3839
* @see java.util.Properties
3940
*/
4041
public interface PropertiesPersister {

0 commit comments

Comments
 (0)