基本上你有一个完成的 JAR,你想把它放到另一个环境中,并且在没有任何修改的情况下让它在运行时获取适当的属性。如果这是正确的,那么以下方法是有效的:
1) 依赖于用户主目录中存在的属性文件。
配置 PropertyPlaceholderConfigurer 以引用 JAR 外部的属性文件,如下所示:
<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="false"/>
<property name="order" value="1"/>
<property name="locations">
<list>
<!-- User home holds secured information -->
<value>file:${user.home}/MyApp/application.properties</value>
</list>
</property>
</bean>
操作系统将保护 application.properties 文件的内容,以便只有合适的人才能访问它。由于第一次运行应用程序时该文件不存在,因此创建一个简单的脚本,在启动时向用户询问关键值(例如用户名、密码、Hibernate 方言等)。为命令行界面提供广泛的帮助和合理的默认值。
2) 如果您的应用程序处于受控环境中以便可以看到数据库,那么问题可以简化为使用上述技术 1) 创建基本凭据之一,以便在上下文启动期间连接到数据库,然后执行替换使用通过 JDBC 读取的值。您将需要一个两阶段的方法来启动应用程序:阶段 1 调用父上下文,其中 application.properties 文件填充 JdbcTemplate 和关联的 DataSource;第 2 阶段调用引用父级的主上下文,以便可以按照 JdbcPropertyPlaceholderConfigurer 中的配置使用 JdbcTemplate。
这种代码的一个例子是这样的:
public class JdbcPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
private Logger log = Logger.getLogger(JdbcPropertyPlaceholderConfigurer.class);
private JdbcTemplate jdbcTemplate;
private String nameColumn;
private String valueColumn;
private String propertiesTable;
/**
* Provide a different prefix
*/
public JdbcPropertyPlaceholderConfigurer() {
super();
setPlaceholderPrefix("#{");
}
@Override
protected void loadProperties(final Properties props) throws IOException {
if (null == props) {
throw new IOException("No properties passed by Spring framework - cannot proceed");
}
String sql = String.format("select %s, %s from %s", nameColumn, valueColumn, propertiesTable);
log.info("Reading configuration properties from database");
try {
jdbcTemplate.query(sql, new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
String name = rs.getString(nameColumn);
String value = rs.getString(valueColumn);
if (null == name || null == value) {
throw new SQLException("Configuration database contains empty data. Name='" + name + "' Value='" + value + "'");
}
props.setProperty(name, value);
}
});
} catch (Exception e) {
log.fatal("There is an error in either 'application.properties' or the configuration database.");
throw new IOException(e);
}
if (props.size() == 0) {
log.fatal("The configuration database could not be reached or does not contain any properties in '" + propertiesTable + "'");
}
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void setNameColumn(String nameColumn) {
this.nameColumn = nameColumn;
}
public void setValueColumn(String valueColumn) {
this.valueColumn = valueColumn;
}
public void setPropertiesTable(String propertiesTable) {
this.propertiesTable = propertiesTable;
}
}
然后将在 Spring 中像这样配置上述内容(注意 order 属性位于通常的 $ 前缀占位符之后):
<!-- Enable configuration through the JDBC configuration with fall-through to framework.properties -->
<bean id="jdbcProperties" class="org.example.JdbcPropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="false"/>
<property name="order" value="2"/>
<property name="nameColumn" value="name"/>
<property name="valueColumn" value="value"/>
<property name="propertiesTable" value="my_properties_table"/>
<property name="jdbcTemplate" ref="configurationJdbcTemplate"/> <!-- Supplied in a parent context -->
</bean>
这将允许在 Spring 配置中发生以下情况
<!-- Read from application.properties -->
<property name="username">${username}</property>
...
<!-- Read in from JDBC as part of second pass after all $'s have been fulfilled -->
<property name="central-thing">#{name.key.in.db}</property>
3) 当然,如果您在 Web 应用程序容器中,那么您只需使用 JNDI。但你不是,所以你不能。
希望这会有所帮助!