Bạn có thể thêm lớp này, thêm tất cả các thuộc tính từ tệp của bạn vào JVM. Và thêm lớp này như bối cảnh người nghe để web.xml
public class InitVariables implements ServletContextListener
{
@Override
public void contextDestroyed(final ServletContextEvent event)
{
}
@Override
public void contextInitialized(final ServletContextEvent event)
{
final String props = "/file.properties";
final Properties propsFromFile = new Properties();
try
{
propsFromFile.load(getClass().getResourceAsStream(props));
}
catch (final IOException e)
{
// can't get resource
}
for (String prop : propsFromFile.stringPropertyNames())
{
if (System.getProperty(prop) == null)
{
System.setProperty(prop, propsFromFile.getProperty(prop));
}
}
}
}
trong web.xml
<listener>
<listener-class>
com.company.InitVariables
</listener-class>
</listener>
bây giờ bạn có thể nhận được tất cả các thuộc tính trong bạn dự án sử dụng
System.getProperty(...)
hoặc trong web.xml
<param-name>param-name</param-name>
<param-value>${param-name}</param-value>
Bản sao có thể có của [Sử dụng thuộc tính trong web.xml] (http://stackoverflow.com/questions/2948992/using-properties-in-web-xml) – imgx64