• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
today-context/src/main/java/infra/context/support/AbstractXmlApplicationContext.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.context.support;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.io.IOException;
23

24
import infra.beans.BeansException;
25
import infra.beans.factory.support.StandardBeanFactory;
26
import infra.beans.factory.xml.ResourceEntityResolver;
27
import infra.beans.factory.xml.XmlBeanDefinitionReader;
28
import infra.context.ApplicationContext;
29
import infra.core.io.Resource;
30

31
/**
32
 * Convenient base class for {@link infra.context.ApplicationContext}
33
 * implementations, drawing configuration from XML documents containing bean definitions
34
 * understood by an {@link XmlBeanDefinitionReader}.
35
 *
36
 * <p>Subclasses just have to implement the {@link #getConfigResources} and/or
37
 * the {@link #getConfigLocations} method. Furthermore, they might override
38
 * the {@link #getResourceByPath} hook to interpret relative paths in an
39
 * environment-specific fashion, and/or {@link #getPatternResourceLoader()}
40
 * for extended pattern resolution.
41
 *
42
 * @author Rod Johnson
43
 * @author Juergen Hoeller
44
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
45
 * @see #getConfigResources
46
 * @see #getConfigLocations
47
 * @see XmlBeanDefinitionReader
48
 * @since 4.0 2022/3/6 22:03
49
 */
50
public abstract class AbstractXmlApplicationContext extends AbstractRefreshableConfigApplicationContext {
51

52
  private boolean validating = true;
6✔
53

54
  /**
55
   * Create a new AbstractXmlApplicationContext with no parent.
56
   */
57
  public AbstractXmlApplicationContext() {
2✔
58
  }
1✔
59

60
  /**
61
   * Create a new AbstractXmlApplicationContext with the given parent context.
62
   *
63
   * @param parent the parent context
64
   */
65
  public AbstractXmlApplicationContext(@Nullable ApplicationContext parent) {
66
    super(parent);
3✔
67
  }
1✔
68

69
  /**
70
   * Set whether to use XML validation. Default is {@code true}.
71
   */
72
  public void setValidating(boolean validating) {
73
    this.validating = validating;
×
74
  }
×
75

76
  /**
77
   * Loads the bean definitions via an XmlBeanDefinitionReader.
78
   *
79
   * @see XmlBeanDefinitionReader
80
   * @see #initBeanDefinitionReader
81
   * @see #loadBeanDefinitions
82
   */
83
  @Override
84
  protected void loadBeanDefinitions(StandardBeanFactory beanFactory) throws BeansException, IOException {
85

86
    // Create a new XmlBeanDefinitionReader for the given BeanFactory.
87
    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
5✔
88

89
    // Configure the bean definition reader with this context's
90
    // resource loading environment.
91
    beanDefinitionReader.setEnvironment(this.getEnvironment());
4✔
92
    beanDefinitionReader.setResourceLoader(this);
3✔
93
    beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
6✔
94

95
    // Allow a subclass to provide custom initialization of the reader,
96
    // then proceed with actually loading the bean definitions.
97
    initBeanDefinitionReader(beanDefinitionReader);
3✔
98
    loadBeanDefinitions(beanDefinitionReader);
3✔
99
  }
1✔
100

101
  /**
102
   * Initialize the bean definition reader used for loading the bean
103
   * definitions of this context. Default implementation is empty.
104
   * <p>Can be overridden in subclasses, e.g. for turning off XML validation
105
   * or using a different XmlBeanDefinitionParser implementation.
106
   *
107
   * @param reader the bean definition reader used by this context
108
   * @see XmlBeanDefinitionReader#setDocumentReaderClass
109
   */
110
  protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
111
    reader.setValidating(this.validating);
4✔
112
  }
1✔
113

114
  /**
115
   * Load the bean definitions with the given XmlBeanDefinitionReader.
116
   * <p>The lifecycle of the bean factory is handled by the {@link #refreshBeanFactory}
117
   * method; hence this method is just supposed to load and/or register bean definitions.
118
   *
119
   * @param reader the XmlBeanDefinitionReader to use
120
   * @throws BeansException in case of bean registration errors
121
   * @throws IOException if the required XML document isn't found
122
   * @see #refreshBeanFactory
123
   * @see #getConfigLocations
124
   * @see #getResources
125
   * @see #getPatternResourceLoader()
126
   */
127
  protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
128
    Resource[] configResources = getConfigResources();
3✔
129
    if (configResources != null) {
2✔
130
      reader.loadBeanDefinitions(configResources);
4✔
131
    }
132
    String[] configLocations = getConfigLocations();
3✔
133
    if (configLocations != null) {
2✔
134
      reader.loadBeanDefinitions(configLocations);
4✔
135
    }
136
  }
1✔
137

138
  /**
139
   * Return an array of Resource objects, referring to the XML bean definition
140
   * files that this context should be built with.
141
   * <p>The default implementation returns {@code null}. Subclasses can override
142
   * this to provide pre-built Resource objects rather than location Strings.
143
   *
144
   * @return an array of Resource objects, or {@code null} if none
145
   * @see #getConfigLocations()
146
   */
147
  protected Resource @Nullable [] getConfigResources() {
148
    return null;
2✔
149
  }
150

151
}
152

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc