简体   繁体   中英

how do I set global variables in Java/Spring web MVC?

I want to have a set of global variables on my Java/Spring-based website.

I'm thinking of things like:

  1. email sender (will only be 1 value as it's the office's generic email)

  2. email server IP/host (we use our own smtp server)

  3. and other things I can't think of in short notice.

I did a search but nothing useful came up on where/how to do this.

Thanks! :)

For simple and straight-forward solution, I suggest you make use of a properties file stored in a (config) directory under your WebContent directory. Below is a basic code on how you can read the properties from the file. (Feel free to modify it according to your needs.) Create an interface or class to contain the static property keys.

For a more complex approach, you may also consider Apache Commons Configuration library.

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigurationReader {

private static final String FILE_SEPARATOR_KEY = "file.separator";  
private String configDirectory; // initialised with ServletContext absolute path
private String configFileName;  // filename of the properties file
private Properties properties;

public void init() throws IOException {
    InputStream inputStream = new FileInputStream(getFileConfigPath(configDirectory, configFileName));
    try {
        properties = new Properties();
        properties.load(inputStream);
    }
    finally {
        try {
            inputStream.close();
        }
        catch(Exception e) {}
    }
}

public String getProperty(String key) {
    return properties.getProperty(key);
}

private String getFileConfigPath(String directory, String fileName) {
    StringBuilder sb = new StringBuilder();
    sb.append(directory);
    if(!sb.substring((sb.length() - 1)).equals(System.getProperty(FILE_SEPARATOR_KEY))) {
        sb.append(System.getProperty(FILE_SEPARATOR_KEY));
    }
    sb.append(fileName);        
    return sb.toString();
}

public String getConfigDirectory() {
    return configDirectory;
}

public void setConfigDirectory(String configDirectory) {
    this.configDirectory = configDirectory;
}

public String getConfigFileName() {
    return configFileName;
}

public void setConfigFileName(String configFileName) {
    this.configFileName = configFileName;
}
}

Create a Class having public static variables and use them as Global variables. If you want Global variable for each transaction set the variables on java ThreadLocal and use them.

IF you want it to be configurable, try configuring those in the DeploymentDescripter (web.xml) as context parameters.

<context-param>
    <param-name>email</param-name>
    <param-value>xxx@yyyy.com</param-value>
  </context-param>

Else you can have a Util class containing constants and declare constant variables as

public static final String EMAIL="xxx@yyy.com";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM