2012-11-01 18 views
6

Sto provando a leggere le proprietà da un file delle proprietà, il cui nome file sarà diverso per ciascuno dei nostri ambienti, come local.properties, dev. proprietà, ecc. Questi file di proprietà conterranno solo le informazioni di connessione per le istanze mongodb corrispondenti come host, porta e dbname. Normalmente questo tipo di cose si farebbe con una definizione JNDI nel nostro server delle app, ma attualmente non ci sono implementazioni di questo per Mongo.Spring 3.1 contextInitializerClasses non funziona su WebLogic 10.3.6 using web.xml Context-Param

Poiché utilizzo WebLogic 10.3.6, non sono in grado di utilizzare le specifiche Servlet 3.0 e, pertanto, non è possibile utilizzare la configurazione Java per Spring, ma solo XML in questo momento. Quindi l'approccio che sto tentando di utilizzare è di avere un context-param di contestoInitializerClass definito nel mio web.xml e quindi di impostarlo su una classe che implementa ApplicationContextInitializer e imposta manualmente il profilo attivo di Spring. Tuttavia, all'avvio di WebLogic o di ridistribuzione, nessuno sta invocando la mia classe di inizializzazione personalizzata e il mio profilo non viene impostato.

La mia domanda è, il contesto di SpringInitializerClass ha una dipendenza su Servlet 3.0 o c'è qualcos'altro che mi manca?

codice che ho definito:

web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

<context-param> 
    <param-name>contextInitializerClass</param-name> 
    <param-value>com.myapp.spring.SpringContextProfileInit</param-value> 
</context-param> 

<!-- Location of spring context config --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
</context-param> 
<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet>  
... 

SpringContextProfileInit.java

public class SpringContextProfileInit implements ApplicationContextInitializer<ConfigurableWebApplicationContext> { 

    private static final Logger log = LoggerFactory.getLogger(SpringContextProfileInit.class); 

    public SpringContextProfileInit() { 
     log.debug("Got the constructor"); 
    } 

    @Override 
    public void initialize(ConfigurableWebApplicationContext ctx) { 
     ConfigurableWebEnvironment environ = ctx.getEnvironment(); 
     log.debug("Got the environment, no profiles should be set: "+ environ.getActiveProfiles()); 

     /* 
     * Here I am setting the profile with a hardcoded name. In the real app, 
     * I would read from a separate properties file, always named app.properties 
     * which would live on the app server's classpath. That app.properties file 
     * would contain a property directing the Spring Profile to use. 
     */ 
     environ.setActiveProfiles("local"); 
     log.debug("Now should be set to local: "+ environ.getActiveProfiles()); 
     ctx.refresh(); 
    } 

} 

servlet-context.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
    xmlns:c="http://www.springframework.org/schema/c" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
xmlns:cache="http://www.springframework.org/schema/cache" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation=" 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
http://www.springframework.org/schema/data/mongo 
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd 
    http://www.springframework.org/schema/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"> 
<context:property-placeholder properties-ref="deployProperties" /> 
... 
<beans profile="local"> 
    <bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean" 
      p:location="WEB-INF/local.properties" /> 
</beans> 
<beans profile="beast, dev"> 
    <bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean" 
      p:location="WEB-INF/dev.properties" /> 
</beans> 
</beans> 

Quando provo a distribuire l'applicazione, ottengo l'eccezione: NoSuchBeanDefinitionException:No bean named 'deployProperties' is defined che ci si aspetterebbe se il profilo non è impostato. I miei registri non mostrano che nessuna delle mie istruzioni di debug è stata stampata. Ho anche provato a spostare il parametro contextInitializerClass per essere un init-param del mio DispatcherServlet, ma che ha dato gli stessi risultati.

I miei vincoli sono che

  1. non riesco a impostare il profilo da dentro il mio script Maven perché la nostra azienda utilizza lo stesso manufatto di spingere a tutti gli ambienti.

  2. Inoltre non è possibile modificare le versioni di WebLogic o utilizzare la più recente servlet specifica in quanto dipendente dal contenitore.

mie versioni attuali sono:

  • Primavera 3.1.2.RELEASE
  • WebLogic 10.3.6
  • javax.servlet-api 2,5

Qualcun altro ha visto questo problema e sa come posso caricare la mia classe di inizializzatore? O c'è un modo migliore per fare ciò che sto cercando di fare?

Questo sembra correlata alla domanda di un altro poster che non è stato risposto: Spring MVC 3.1 Using Profiles for environment specific Hibernate settings

risposta

6

Il nome del context-param è credo sbagliato, dovrebbe essere contextInitializerClasses non contextInitializerClass, che potrebbe essere il motivo per cui l'ApplicationContextInitializer è non ottenere raccolti

Inoltre è sembrano mancare la voce per ContextLoaderListener nel file web.xml, prova ad aggiungere anche questo:

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

Questo è quello che carica il file XML di configurazione del bean specificato nel tag contextConfigLocation

+0

Beh, questo è imbarazzante, hai ragione sul nome, deve essere * Classi. Grazie per l'aiuto. –