Skip to content

Commit a6bba67

Browse files
committed
added setValidating to XmlBeanDefinitionReader itself as well (SPR-6336)
1 parent 1ddbfe0 commit a6bba67

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/XmlBeanDefinitionReader.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,31 @@ public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {
135135
}
136136

137137

138+
/**
139+
* Set whether to use XML validation. Default is <code>true</code>.
140+
* <p>This method switches namespace awareness on if validation is turned off,
141+
* in order to still process schema namespaces properly in such a scenario.
142+
* @see #setValidationMode
143+
* @see #setNamespaceAware
144+
*/
145+
public void setValidating(boolean validating) {
146+
this.validationMode = (validating ? VALIDATION_AUTO : VALIDATION_NONE);
147+
this.namespaceAware = !validating;
148+
}
149+
138150
/**
139151
* Set the validation mode to use by name. Defaults to {@link #VALIDATION_AUTO}.
152+
* @see #setValidationMode
140153
*/
141154
public void setValidationModeName(String validationModeName) {
142155
setValidationMode(constants.asNumber(validationModeName).intValue());
143156
}
144157

145158
/**
146159
* Set the validation mode to use. Defaults to {@link #VALIDATION_AUTO}.
160+
* <p>Note that this only activates or deactivates validation itself.
161+
* If you are switching validation off for schema files, you might need to
162+
* activate schema namespace support explicitly: see {@link #setNamespaceAware}.
147163
*/
148164
public void setValidationMode(int validationMode) {
149165
this.validationMode = validationMode;
@@ -159,6 +175,9 @@ public int getValidationMode() {
159175
/**
160176
* Set whether or not the XML parser should be XML namespace aware.
161177
* Default is "false".
178+
* <p>This is typically not needed when schema validation is active.
179+
* However, without validation, this has to be switched to "true"
180+
* in order to properly process schema namespaces.
162181
*/
163182
public void setNamespaceAware(boolean namespaceAware) {
164183
this.namespaceAware = namespaceAware;

org.springframework.context/src/main/java/org/springframework/context/support/AbstractXmlApplicationContext.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,11 @@ protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throw
9797
* definitions of this context. Default implementation is empty.
9898
* <p>Can be overridden in subclasses, e.g. for turning off XML validation
9999
* or using a different XmlBeanDefinitionParser implementation.
100-
* @param beanDefinitionReader the bean definition reader used by this context
100+
* @param reader the bean definition reader used by this context
101101
* @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setDocumentReaderClass
102102
*/
103-
protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
104-
if (!this.validating) {
105-
beanDefinitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
106-
beanDefinitionReader.setNamespaceAware(true);
107-
}
103+
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
104+
reader.setValidating(this.validating);
108105
}
109106

110107
/**

org.springframework.context/src/main/java/org/springframework/context/support/GenericXmlApplicationContext.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ public GenericXmlApplicationContext(String... resourceLocations) {
7272
* Set whether to use XML validation. Default is <code>true</code>.
7373
*/
7474
public void setValidating(boolean validating) {
75-
this.reader.setValidationMode(validating ?
76-
XmlBeanDefinitionReader.VALIDATION_AUTO : XmlBeanDefinitionReader.VALIDATION_NONE);
77-
this.reader.setNamespaceAware(!validating);
75+
this.reader.setValidating(validating);
7876
}
7977

8078

0 commit comments

Comments
 (0)