我正在开发一项增强功能。如何在第一次运行应用程序时设置数据库配置。 如果我使用用户在网页中设置的值更新db.properties,那么tomcat服务器将自动重启。 所以请告诉我如何将这些值注册到spring环境中,以便datasource-tx-jpa.xml可以使用类似的东西:$ {db.url}没有占位符db.properties
我测试如下,但它不起作用
Properties props = new Properties();
props.setProperty("webform.hibernate.dialect", "org.hibernate.dialect.SQLServer2008Dialect");
System.setProperties(props);
WebApplicationContext webApplicationContext = (WebApplicationContext) appContext;
XmlWebApplicationContext xmlWebApplicationContext = (XmlWebApplicationContext) webApplicationContext;
xmlWebApplicationContext.setConfigLocations(new String[] {
"classpath:spring/datasource-tx-jpa.xml",
"classpath:spring/application-config.xml",
"/WEB-INF/mvc-config.xml" });
xmlWebApplicationContext.refresh();
答案 0 :(得分:0)
您可以在加载上下文之前以编程方式将PropertySource添加到环境中。
Properties properties = new Properties();
//initialize properties
PropertySource<?> propertySource = new PropertiesPropertySource("myprops", properties);
applicationContext.getEnvironment().getPropertySources().addLast(propertySource);