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

Tre*_*ley 6 java weblogic spring-mvc mongodb weblogic-10.x

我正在尝试从属性文件中读取属性,其文件名对于我们的每个环境都是不同的,例如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>      
...
Run Code Online (Sandbox Code Playgroud)

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();
    }

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

当我尝试部署应用程序时,我得到了异常: 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设置

Bij*_*men 6

context-param的名称是我认为错误的,它应该contextInitializerClasses不是contextInitializerClass,这可能是你的ApplicationContextInitializer没有被拿起的原因

您似乎也错过了ContextLoaderListenerweb.xml文件中的条目,尝试添加它:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

这是加载contextConfigLocation标记中指定的bean配置xml文件的那个