Java tutorial
/* * Copyright (C) 2012 Dario Scoppelletti, <http://www.scoppelletti.it/>. * * 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 it.scoppelletti.programmerpower.web.spring.config; import org.springframework.context.*; import org.springframework.core.env.*; import org.springframework.web.context.support.*; import it.scoppelletti.programmerpower.data.*; import it.scoppelletti.programmerpower.spring.config.*; /** * Personalizzazione del contesto Spring di un’applicazione Web. * * <P>Il componente {@code WebApplicationContextInitializer} esegue le seguenti * personalizzazioni:</P> * * <OL TYPE="1"> * <LI>Aggiunge i profili {@code it-scoppelletti-web} e * {@code it-scoppelletti-data} nell’ambiente del contesto Spring (gli * eventuali altri profili impostati attraverso la proprietà di sistema * {@code spring.profiles.active} sono comunque considerati). * <LI>Inserisce come sorgenti delle proprietà di ambiente i seguenti * componenti: * <OL TYPE="a"> * <LI>File di proprietà * {@code it-scoppelletti-programmerpower-beans.properties} rilevato * attraverso il class-path. * <LI>Componente {@code WebPropertySource}. * </OL> * </OL> * * <P>Le applicazioni Web possono gestire la personalizzazione del contesto * Spring configurando il componente Spring {@code ContextLoaderListener}; * questo componente può rilevare il nome della classe che implementa la * personalizzazione dal parametro {@code contextInitializerClasses} del * contesto dell’applicazione impostato nel descrittore di deployment * {@code web.xml}.</P> * * <BLOCKQUOTE><PRE> * <web-app ... > * ... * <context-param> * <param-name>contextInitializerClasses</param-name> * <param-value>it.scoppelletti.programmerpower.web.spring.config.WebApplicationContextInitializer</param-value> * <context-param> * ... * <listener> * <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> * </listener> * ... * </web-app> * </PRE><BLOCKQUOTE> * * @see it.scoppelletti.programmerpower.web.spring.config.WebPropertySource * @see it.scoppelletti.programmerpower.web.spring.ApplicationContextListener * @see it.scoppelletti.programmerpower.web.spring.ContextLoaderListenerEx * @see it.scoppelletti.programmerpower.spring.config.BeanConfigUtils#loadPropertySource * @see <A HREF="{@docRoot}/../reference/setup/envprops.html" * TARGET="_top">Proprietà di ambiente</A> * @since 1.0.0 */ public final class WebApplicationContextInitializer implements ApplicationContextInitializer<AbstractRefreshableWebApplicationContext> { /** * Profilo aggiunto nell’ambiente del contesto Spring. Il valore * della costante è <CODE>{@value}</CODE>. */ public static final String PROFILE = "it-scoppelletti-web"; /** * Costruttore. */ public WebApplicationContextInitializer() { } /** * Inizializzazione. * * @param applCtx Contesto dell’applicazione. */ public void initialize(AbstractRefreshableWebApplicationContext applCtx) { ConfigurableEnvironment env; PropertySource<?> propSource; MutablePropertySources propSources; env = applCtx.getEnvironment(); // Acquisisco gli eventuali profili configurati prima di aggiungere // quelli di Programmer Power env.getActiveProfiles(); env.addActiveProfile(WebApplicationContextInitializer.PROFILE); env.addActiveProfile(DataUtils.PROFILE); propSources = env.getPropertySources(); propSource = BeanConfigUtils.loadPropertySource(); if (propSource != null) { propSources.addFirst(propSource); } propSources.addLast(new WebPropertySource(WebPropertySource.class.getName(), applCtx)); } }