这篇文章里面说的东西,其实很多都是比较简单的。它的重点在于对于Spring的配置文件applicationContext.xml如何来管理。因为如果把它加入到源代码管理(CVS或Subversion等)中,则对此文件的频繁更改会造成源代码管理的混乱。
一种解决方法是使用配置文件来保存特定的信息,比如数据库的连接信息。这种方式为大多数人所知道,关键在于使用PropertyPlaceholderConfigurer。
另外一种做法就是把原来的applicationContext.xml进行拆分,这种方式也应用广泛。一种方式就是添加import:<import resource=”applicationContext-somehost.com.xml”/>;另外的方式就是在web.xml中使用contextConfigLocation来指定使用的配置文件名称。
如果不想修改web.xml话,可以编写自己的XmlWebApplicationContext的子类,由它来指定加载某些特定的配置文件。比如下面的例子:
import java.net.InetAddress;
import org.springframework.web.context.support.XmlWebApplicationContext;
public class PerHostXmlWebApplicationContext
extends XmlWebApplicationContext {
protected String[] getDefaultConfigLocations() {
String hostname = “localhost”;
try {
hostname = InetAddress.getLocalHost().getHostName();
} catch (Exception e) {
}
String perHostConfiguration = DEFAULT_CONFIG_LOCATION_PREFIX
+ “applicationContext-”
+ hostname
+ DEFAULT_CONFIG_LOCATION_SUFFIX
;
logger.debug(
“Adding per host configuration file: ”
+ perHostConfiguration
);
if (getNamespace() != null) {
return new String[] {
DEFAULT_CONFIG_LOCATION_PREFIX
+ getNamespace()
+ DEFAULT_CONFIG_LOCATION_SUFFIX
, perHostConfiguration};
}
else {
return new String[] {
DEFAULT_CONFIG_LOCATION
, perHostConfiguration};
}
}
}
在配置文件web.xml中,可以这样写:
<context-param>
<param-name>contextClass</param-name>
<param-value>
net.nighttale.spring.util.PerHostXmlWebApplicationContext
</param-value>
一种解决方法是使用配置文件来保存特定的信息,比如数据库的连接信息。这种方式为大多数人所知道,关键在于使用PropertyPlaceholderConfigurer。
另外一种做法就是把原来的applicationContext.xml进行拆分,这种方式也应用广泛。一种方式就是添加import:<import resource=”applicationContext-somehost.com.xml”/>;另外的方式就是在web.xml中使用contextConfigLocation来指定使用的配置文件名称。
如果不想修改web.xml话,可以编写自己的XmlWebApplicationContext的子类,由它来指定加载某些特定的配置文件。比如下面的例子:
import java.net.InetAddress;
import org.springframework.web.context.support.XmlWebApplicationContext;
public class PerHostXmlWebApplicationContext
extends XmlWebApplicationContext {
protected String[] getDefaultConfigLocations() {
String hostname = “localhost”;
try {
hostname = InetAddress.getLocalHost().getHostName();
} catch (Exception e) {
}
String perHostConfiguration = DEFAULT_CONFIG_LOCATION_PREFIX
+ “applicationContext-”
+ hostname
+ DEFAULT_CONFIG_LOCATION_SUFFIX
;
logger.debug(
“Adding per host configuration file: ”
+ perHostConfiguration
);
if (getNamespace() != null) {
return new String[] {
DEFAULT_CONFIG_LOCATION_PREFIX
+ getNamespace()
+ DEFAULT_CONFIG_LOCATION_SUFFIX
, perHostConfiguration};
}
else {
return new String[] {
DEFAULT_CONFIG_LOCATION
, perHostConfiguration};
}
}
}
在配置文件web.xml中,可以这样写:
<context-param>
<param-name>contextClass</param-name>
<param-value>
net.nighttale.spring.util.PerHostXmlWebApplicationContext
</param-value>