001/* 002 * Copyright 2012-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.web.servlet.context; 018 019import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; 020import org.springframework.core.env.ConfigurableEnvironment; 021import org.springframework.core.io.ClassPathResource; 022import org.springframework.core.io.Resource; 023import org.springframework.web.context.support.XmlWebApplicationContext; 024 025/** 026 * {@link ServletWebServerApplicationContext} which takes its configuration from XML 027 * documents, understood by an 028 * {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}. 029 * <p> 030 * Note: In case of multiple config locations, later bean definitions will override ones 031 * defined in earlier loaded files. This can be leveraged to deliberately override certain 032 * bean definitions via an extra XML file. 033 * 034 * @author Phillip Webb 035 * @see #setNamespace 036 * @see #setConfigLocations 037 * @see ServletWebServerApplicationContext 038 * @see XmlWebApplicationContext 039 */ 040public class XmlServletWebServerApplicationContext 041 extends ServletWebServerApplicationContext { 042 043 private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this); 044 045 /** 046 * Create a new {@link XmlServletWebServerApplicationContext} that needs to be 047 * {@linkplain #load loaded} and then manually {@link #refresh refreshed}. 048 */ 049 public XmlServletWebServerApplicationContext() { 050 this.reader.setEnvironment(this.getEnvironment()); 051 } 052 053 /** 054 * Create a new {@link XmlServletWebServerApplicationContext}, loading bean 055 * definitions from the given resources and automatically refreshing the context. 056 * @param resources the resources to load from 057 */ 058 public XmlServletWebServerApplicationContext(Resource... resources) { 059 load(resources); 060 refresh(); 061 } 062 063 /** 064 * Create a new {@link XmlServletWebServerApplicationContext}, loading bean 065 * definitions from the given resource locations and automatically refreshing the 066 * context. 067 * @param resourceLocations the resources to load from 068 */ 069 public XmlServletWebServerApplicationContext(String... resourceLocations) { 070 load(resourceLocations); 071 refresh(); 072 } 073 074 /** 075 * Create a new {@link XmlServletWebServerApplicationContext}, loading bean 076 * definitions from the given resource locations and automatically refreshing the 077 * context. 078 * @param relativeClass class whose package will be used as a prefix when loading each 079 * specified resource name 080 * @param resourceNames relatively-qualified names of resources to load 081 */ 082 public XmlServletWebServerApplicationContext(Class<?> relativeClass, 083 String... resourceNames) { 084 load(relativeClass, resourceNames); 085 refresh(); 086 } 087 088 /** 089 * Set whether to use XML validation. Default is {@code true}. 090 * @param validating if validating the XML 091 */ 092 public void setValidating(boolean validating) { 093 this.reader.setValidating(validating); 094 } 095 096 /** 097 * {@inheritDoc} 098 * <p> 099 * Delegates the given environment to underlying {@link XmlBeanDefinitionReader}. 100 * Should be called before any call to {@link #load}. 101 */ 102 @Override 103 public void setEnvironment(ConfigurableEnvironment environment) { 104 super.setEnvironment(environment); 105 this.reader.setEnvironment(this.getEnvironment()); 106 } 107 108 /** 109 * Load bean definitions from the given XML resources. 110 * @param resources one or more resources to load from 111 */ 112 public final void load(Resource... resources) { 113 this.reader.loadBeanDefinitions(resources); 114 } 115 116 /** 117 * Load bean definitions from the given XML resources. 118 * @param resourceLocations one or more resource locations to load from 119 */ 120 public final void load(String... resourceLocations) { 121 this.reader.loadBeanDefinitions(resourceLocations); 122 } 123 124 /** 125 * Load bean definitions from the given XML resources. 126 * @param relativeClass class whose package will be used as a prefix when loading each 127 * specified resource name 128 * @param resourceNames relatively-qualified names of resources to load 129 */ 130 public final void load(Class<?> relativeClass, String... resourceNames) { 131 Resource[] resources = new Resource[resourceNames.length]; 132 for (int i = 0; i < resourceNames.length; i++) { 133 resources[i] = new ClassPathResource(resourceNames[i], relativeClass); 134 } 135 this.reader.loadBeanDefinitions(resources); 136 } 137 138}