我試圖從屬性文件中讀取屬性,其文件名對於我們的每個環境(如local.properties,dev)來說都是不同的。屬性等。這些屬性文件將僅包含其對應的mongodb實例(如host,port和dbname)的連接信息。通常情況下,這類事情將在我們的應用服務器中使用JNDI定義來完成,但是目前沒有用於Mongo的實現。Spring 3.1 contextInitializerClasses不使用web.xml工作在WebLogic 10.3.6上下文參數
由於我使用的是WebLogic 10.3.6,因此我無法使用Servlet 3.0規範,因此無法在Spring中使用Java配置,此時只能使用XML。所以我試圖使用的方法是在web.xml中定義contextInitializerClass上下文參數,然後將其設置爲實現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
如果配置文件未設置這將可以預期。我的日誌不顯示任何我的調試語句打印。我也嘗試將contextInitializerClass參數移動到我的DispatcherServlet的init-param中,但是得到了相同的結果。
我的約束是
我無法從我的Maven的腳本中,因爲我們的 公司使用相同的神器推送到所有的環境設置的配置文件。
我也不能更改WebLogic的版本或使用最新的servlet 規範,因爲它是依賴於容器的。
我現在的版本是:
- 春3.1.2.RELEASE
- 的WebLogic 10.3.6
- 的javax.servlet-API 2.5
有其他人看到這個問題,並知道如何讓我的初始化類加載?還是有更好的方法來做我想做的事情?
這看起來與另一海報的問題還沒有得到回答:Spring MVC 3.1 Using Profiles for environment specific Hibernate settings
那麼這是令人尷尬的,你對名稱是正確的,它需要是*類。謝謝您的幫助。 –