📄 defaultfaceswebapplicationcontext.java
字号:
/* * Copyright 2002-2004 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package de.mindmatters.faces.spring.context.support;import java.io.IOException;import java.net.URL;import java.util.ArrayList;import java.util.Arrays;import java.util.Enumeration;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;import javax.faces.FactoryFinder;import javax.faces.application.Application;import javax.faces.application.ApplicationFactory;import org.springframework.beans.FatalBeanException;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.beans.factory.support.DefaultListableBeanFactory;import org.springframework.beans.factory.xml.DelegatingEntityResolver;import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;import org.springframework.util.ObjectUtils;import org.springframework.web.context.support.XmlWebApplicationContext;import de.mindmatters.faces.FacesDtdResolver;import de.mindmatters.faces.spring.context.FacesWebApplicationContext;import de.mindmatters.faces.spring.factory.ApplicationScope;import de.mindmatters.faces.spring.factory.DelegatingVariableResolver;import de.mindmatters.faces.spring.factory.ManagedBeanFactory;import de.mindmatters.faces.spring.factory.ValueBindingResolvingInstantiationStrategy;import de.mindmatters.faces.spring.factory.xml.ManagedBeanDefinitionDocumentReader;/** * Default implementation of the FacesWebApplicationContext interface. * * <p> * Uses the * {@link de.mindmatters.faces.spring.factory.xml.ManagedBeanDefinitionDocumentReader} * for loading the configured BeanDefinitions. * </p> * * <p> * Uses and enables * {@link de.mindmatters.faces.spring.factory.DelegatingVariableResolver} as JSF * VariableResolver. The DelegatingVariableResolver cares about bean resolution * from a view (e.g. a jsp resource). * </p> * * @author Andreas Kuhrwahl * * @see de.mindmatters.faces.spring.context.ContextLoader * @see de.mindmatters.faces.spring.context.ContextLoaderListener * @see de.mindmatters.faces.spring.context.ContextLoaderServlet * @see de.mindmatters.faces.spring.factory.xml.ManagedBeanDefinitionDocumentReader * @see ManagedBeanFactory */public class DefaultFacesWebApplicationContext extends XmlWebApplicationContext implements FacesWebApplicationContext { /** Paths to the spring beans DTD based XML configuration files. */ private String[] facesSpringConfigLocations; /** * Standard constructor in JavaBeans-style. */ public DefaultFacesWebApplicationContext() { super(); setDisplayName("Root FacesWebApplicationContext"); } /** * {@inheritDoc} */ protected final DefaultListableBeanFactory createBeanFactory() { return createManagedBeanFactory(); } /** * Create an internal managed bean factory for this context. * <p> * The default implementation creates a {@link ManagedBeanFactory} with the * internal bean factory of this context's parent as parent bean factory. * Can be overridden in subclasses. * </p> * * @return the bean factory for this context */ protected ManagedBeanFactory createManagedBeanFactory() { ManagedBeanFactory beanFactory = new ManagedBeanFactory( getInternalParentBeanFactory()); beanFactory .setInstantiationStrategy(new ValueBindingResolvingInstantiationStrategy()); return beanFactory; } /** * Sets the document-reader class of the <code>beanDefinitionReader</code> * to the {@link ManagedBeanDefinitionDocumentReader} class. * * @param beanDefinitionReader * the reader to initialize * @see org.springframework.context.support.AbstractXmlApplicationContext * #initBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) * @see ManagedBeanDefinitionDocumentReader */ protected final void initBeanDefinitionReader( final XmlBeanDefinitionReader beanDefinitionReader) { beanDefinitionReader .setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE); beanDefinitionReader.setEntityResolver(new FacesDtdResolver()); beanDefinitionReader .setDocumentReaderClass(ManagedBeanDefinitionDocumentReader.class); } /** * {@inheritDoc} */ public final void setFacesSpringConfigLocations(final String[] locations) { this.facesSpringConfigLocations = new String[locations.length]; for (int i = 0; i < locations.length; i++) { this.facesSpringConfigLocations[i] = resolvePath(locations[i]); } } /** * Returns the locations of the jsf-spring configuration files. * * @return The locations of the jsf-spring configuration files */ protected final String[] getFacesSpringConfigLocations() { return facesSpringConfigLocations; } /** * {@inheritDoc} */ protected final void loadBeanDefinitions( final XmlBeanDefinitionReader reader) throws IOException { super.loadBeanDefinitions(reader); String[] configLocations = this.facesSpringConfigLocations; if (configLocations != null) { reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_AUTO); reader.setEntityResolver(new DelegatingEntityResolver( getClassLoader())); for (int i = 0; i < configLocations.length; i++) { reader.loadBeanDefinitions(getResources(configLocations[i])); } } } /** * Initialize and enables faces-spring's delegating * {@link javax.faces.el.VariableResolver}. * * @param beanFactory * internal BeanFactory * @see DelegatingVariableResolver */ private void initDelegatingVariableResolver( final ConfigurableListableBeanFactory beanFactory) { if (logger.isInfoEnabled()) { logger.info("Setting custom DelegatingVariableResolver"); } Application facesApplication = ((ApplicationFactory) FactoryFinder .getFactory(FactoryFinder.APPLICATION_FACTORY)) .getApplication(); facesApplication.setVariableResolver(new DelegatingVariableResolver( facesApplication.getVariableResolver(), beanFactory)); } /** * {@inheritDoc} */ protected final void postProcessBeanFactory( final ConfigurableListableBeanFactory beanFactory) { beanFactory.registerScope(SCOPE_APPLICATION, new ApplicationScope( getServletContext())); super.postProcessBeanFactory(beanFactory); initDelegatingVariableResolver(beanFactory); onPostProcessBeanFactory(beanFactory); } /** * Hook method for postprocessing the internal bean factory. * * @param beanFactory * the bean factory used by this application context * @throws org.springframework.beans.BeansException * in case of errors */ protected void onPostProcessBeanFactory( final ConfigurableListableBeanFactory beanFactory) { } /** * {@inheritDoc} */ protected final String[] getDefaultConfigLocations() { List standardConfigLocations = new ArrayList(); try { for (final Enumeration e = this.getClassLoader().getResources( "META-INF/faces-config.xml"); e.hasMoreElements();) { standardConfigLocations.add(0, ((URL) e.nextElement()) .toExternalForm()); } } catch (IOException ex) { throw new FatalBeanException(ex.getMessage(), ex); } if (getNamespace() != null) { standardConfigLocations.add(DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX); } else { standardConfigLocations.add("/WEB-INF/faces-config.xml"); } for (final Iterator i = standardConfigLocations.iterator(); i.hasNext();) { if (!getResource((String) i.next()).exists()) { i.remove(); } } return (String[]) standardConfigLocations .toArray(new String[standardConfigLocations.size()]); } /** * The default location for jsf-spring config files is * "/WEB-INF/faces-spring-context.xml", and all * "/META-INF/faces-spring-context.xml". * * @return The locations. */ protected final String[] getDefaultFacesSpringConfigLocations() { List standardConfigLocations = new ArrayList(); try { for (final Enumeration e = this.getClassLoader().getResources( "META-INF/faces-spring-context.xml"); e.hasMoreElements();) { standardConfigLocations.add(0, ((URL) e.nextElement()) .toExternalForm()); } } catch (IOException ex) { throw new FatalBeanException(ex.getMessage(), ex); } standardConfigLocations.add("/WEB-INF/faces-spring-context.xml"); for (final Iterator i = standardConfigLocations.iterator(); i.hasNext();) { if (!getResource((String) i.next()).exists()) { i.remove(); } } return (String[]) standardConfigLocations .toArray(new String[standardConfigLocations.size()]); } /** * {@inheritDoc} */ public void refresh() { if (!ObjectUtils.isEmpty(getConfigLocations())) { Set configLocations = new HashSet(Arrays .asList(getDefaultConfigLocations())); configLocations.addAll(Arrays.asList(getConfigLocations())); setConfigLocations((String[]) configLocations .toArray(new String[configLocations.size()])); } if (ObjectUtils.isEmpty(getFacesSpringConfigLocations())) { setFacesSpringConfigLocations(getDefaultFacesSpringConfigLocations()); } else { Set configLocations = new HashSet(Arrays .asList(getDefaultFacesSpringConfigLocations())); configLocations.addAll(Arrays .asList(getFacesSpringConfigLocations())); setFacesSpringConfigLocations((String[]) configLocations .toArray(new String[configLocations.size()])); } super.refresh(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -