001/* 002 * Copyright 2012-2015 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.context.embedded; 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 EmbeddedWebApplicationContext} which takes its configuration from XML documents, 027 * understood by an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}. 028 * <p> 029 * Note: In case of multiple config locations, later bean definitions will override ones 030 * defined in earlier loaded files. This can be leveraged to deliberately override certain 031 * bean definitions via an extra XML file. 032 * 033 * @author Phillip Webb 034 * @see #setNamespace 035 * @see #setConfigLocations 036 * @see EmbeddedWebApplicationContext 037 * @see XmlWebApplicationContext 038 */ 039public class XmlEmbeddedWebApplicationContext extends EmbeddedWebApplicationContext { 040 041 private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this); 042 043 /** 044 * Create a new {@link XmlEmbeddedWebApplicationContext} that needs to be 045 * {@linkplain #load loaded} and then manually {@link #refresh refreshed}. 046 */ 047 public XmlEmbeddedWebApplicationContext() { 048 this.reader.setEnvironment(this.getEnvironment()); 049 } 050 051 /** 052 * Create a new {@link XmlEmbeddedWebApplicationContext}, loading bean definitions 053 * from the given resources and automatically refreshing the context. 054 * @param resources the resources to load from 055 */ 056 public XmlEmbeddedWebApplicationContext(Resource... resources) { 057 load(resources); 058 refresh(); 059 } 060 061 /** 062 * Create a new {@link XmlEmbeddedWebApplicationContext}, loading bean definitions 063 * from the given resource locations and automatically refreshing the context. 064 * @param resourceLocations the resources to load from 065 */ 066 public XmlEmbeddedWebApplicationContext(String... resourceLocations) { 067 load(resourceLocations); 068 refresh(); 069 } 070 071 /** 072 * Create a new {@link XmlEmbeddedWebApplicationContext}, loading bean definitions 073 * from the given resource locations and automatically refreshing the context. 074 * @param relativeClass class whose package will be used as a prefix when loading each 075 * specified resource name 076 * @param resourceNames relatively-qualified names of resources to load 077 */ 078 public XmlEmbeddedWebApplicationContext(Class<?> relativeClass, 079 String... resourceNames) { 080 load(relativeClass, resourceNames); 081 refresh(); 082 } 083 084 /** 085 * Set whether to use XML validation. Default is {@code true}. 086 * @param validating if validating the XML 087 */ 088 public void setValidating(boolean validating) { 089 this.reader.setValidating(validating); 090 } 091 092 /** 093 * {@inheritDoc} 094 * <p> 095 * Delegates the given environment to underlying {@link XmlBeanDefinitionReader}. 096 * Should be called before any call to {@link #load}. 097 */ 098 @Override 099 public void setEnvironment(ConfigurableEnvironment environment) { 100 super.setEnvironment(environment); 101 this.reader.setEnvironment(this.getEnvironment()); 102 } 103 104 /** 105 * Load bean definitions from the given XML resources. 106 * @param resources one or more resources to load from 107 */ 108 public final void load(Resource... resources) { 109 this.reader.loadBeanDefinitions(resources); 110 } 111 112 /** 113 * Load bean definitions from the given XML resources. 114 * @param resourceLocations one or more resource locations to load from 115 */ 116 public final void load(String... resourceLocations) { 117 this.reader.loadBeanDefinitions(resourceLocations); 118 } 119 120 /** 121 * Load bean definitions from the given XML resources. 122 * @param relativeClass class whose package will be used as a prefix when loading each 123 * specified resource name 124 * @param resourceNames relatively-qualified names of resources to load 125 */ 126 public final void load(Class<?> relativeClass, String... resourceNames) { 127 Resource[] resources = new Resource[resourceNames.length]; 128 for (int i = 0; i < resourceNames.length; i++) { 129 resources[i] = new ClassPathResource(resourceNames[i], relativeClass); 130 } 131 this.reader.loadBeanDefinitions(resources); 132 } 133 134}