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