簡體   English   中英

Spring 3.1 contextInitializerClasses不使用web.xml Context-Param在WebLogic 10.3.6上工作

[英]Spring 3.1 contextInitializerClasses not Working on WebLogic 10.3.6 using web.xml Context-Param

我正在嘗試從屬性文件中讀取屬性,其文件名對於我們的每個環境都是不同的,例如local.properties,dev.properties等。這些屬性文件將只包含其對應的mongodb實例(如主機)的連接信息,port和dbname。 通常這種事情將在我們的應用服務器中使用JNDI定義完成,但目前沒有針對Mongo的實現。

由於我使用的是WebLogic 10.3.6,因此我無法使用Servlet 3.0規范,因此不能在Spring中使用Java配置,此時只能使用XML。 因此,我嘗試使用的方法是在我的web.xml中定義contextInitializerClass context-param,然后將其設置為實現ApplicationContextInitializer的類,並手動設置Spring活動配置文件。 但是,在啟動WebLogic或重新部署時,都沒有調用我的自定義初始化程序類,並且我的配置文件沒有設置。

我的問題是 ,Spring的contextInitializerClass是否依賴於Servlet 3.0還是還有其他我缺少的東西?

我定義的代碼:

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>

當我嘗試部署應用程序時,我得到異常: NoSuchBeanDefinitionException:No bean named 'deployProperties' is defined ,如果未設置配置文件,則會預期該NoSuchBeanDefinitionException:No bean named 'deployProperties' is defined 我的日志沒有顯示我的任何調試語句都被打印出來。 我也嘗試將contextInitializerClass參數移動到我的DispatcherServlet的init-param,但是給出了相同的結果。

我的約束是那樣的

  1. 我無法在Maven腳本中設置配置文件,因為我們公司使用相同的工件推送到所有環境。

  2. 我也無法更改WebLogic的版本或使用最新的servlet規范,因為它依賴於容器。

我目前的版本是:

  • 春季3.1.2.RELEASE
  • WebLogic 10.3.6
  • javax.servlet-api 2.5

有沒有其他人看到這個問題,知道如何加載我的初始化程序類? 或者有更好的方法來做我想做的事情?

這看起來與另一個尚未回答的海報問題有關: Spring MVC 3.1將Profiles用於特定於環境的Hibernate設置

context-param的名稱是我認為錯誤的,它應該是contextInitializerClasses而不是contextInitializerClass ,這可能是你的ApplicationContextInitializer沒有被拿起的原因

此外,您似乎在web.xml文件中缺少ContextLoaderListener的條目,嘗試添加它:

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

這是加載contextConfigLocation標記中指定的bean配置xml文件的那個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM