Java 是否基于在app war文件外部指定的属性加载变量spring applicationContext.xml文件?
我需要更改基于属性使用的spring applicationContext.xml文件,此属性必须定义在war文件之外的某个地方(即,它不能在web.xml中)。目前,我已经找到了以下解决方案(请参阅下面的答案),不知道是否有更好的方法来实现这一点?我的解决方案有4个部分。首先,在我的应用程序的web.xml中,我定义了以下内容:Java 是否基于在app war文件外部指定的属性加载变量spring applicationContext.xml文件?,java,spring,tomcat6,Java,Spring,Tomcat6,我需要更改基于属性使用的spring applicationContext.xml文件,此属性必须定义在war文件之外的某个地方(即,它不能在web.xml中)。目前,我已经找到了以下解决方案(请参阅下面的答案),不知道是否有更好的方法来实现这一点?我的解决方案有4个部分。首先,在我的应用程序的web.xml中,我定义了以下内容: <context-param> <param-name>contextConfigLocation</param-nam
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation1</param-name>
<param-value>classpath:applicationContext-1.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation2</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>com.my.package.MyContextLoaderListener</listener-class>
</listener>
和上下文加载器
package com.my.package;
import javax.servlet.ServletContext;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextLoader;
public class LnvContextLoader extends ContextLoader {
private static final String APP_CONTEXT_PROP = "MY_CONTEXT_LOAD_PARAM";
@Override
protected void customizeContext(ServletContext servletContext,
ConfigurableWebApplicationContext wac) {
//check for system property first, if not defined, check for env variable
String appContextParam = System.getProperty(APP_CONTEXT_PROP);
if(appContextParam==null)
{
appContextParam = System.getenv(APP_CONTEXT_PROP);
}
if(appContextParam!=null && !appContextParam.equals("")){
String initParam = servletContext.getInitParameter(appContextParam);
wac.setConfigLocation(initParam);
}
}
}
最后,在我的tomcat启动中,我在setenv.bat中定义了环境变量
set MY_CONTEXT_LOAD_PARAM=contextConfigLocation1
此解决方案从环境变量加载它,但代码很灵活,允许在系统属性中进行设置。如果通过类路径加载应用程序上下文,可以通过在服务器的类路径中放置另一版本的applicationContext.xml来覆盖它 我的解决方案是使用一个非常简单的applicationContext,其中包括真实的应用程序上下文: applicationContext.xml:
<beans>
<import resource="classpath:realContext.xml"/>
</beans>
或
您是否考虑过使用beanRefContext方法。(上下文SingletonBeanFactoryLocator)。这样,您可以通过另一个spring配置文件配置spring配置文件(及其名称) 然后,您可以通过您认为合适的任何方式对该文件进行参数化,并以这种方式切换文件名 该文件如下所示:
<beans>
<bean id="businessBeanFactory" class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="${NameOfBeanConfigFile}" />
</bean>
</beans>
您可以使用PropertyPlaceHolderConfiger设置BeanConfigFile的名称值
我喜欢这种方法,因为它意味着我可以混合使用静态bean配置文件名和动态bean配置文件名,因此不必重复bean配置
当我不得不做类似的事情时,我会通过作为URL资源加载的配置文件(通过jndi)进行参数化
<beans>
<import resource="classpath:realContext2.xml"/>
</beans>
<beans>
<bean id="bean1" .../>
<bean id="bean2" .../>
<bean id="otherBean">
<property name="injectDifferentBean" ref="${property.containing.bean.name" />
</bean>
</beans>
property.containing.bean.name=bean1
property.containing.bean.name=bean2
<beans>
<bean id="businessBeanFactory" class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="${NameOfBeanConfigFile}" />
</bean>
</beans>