例如读取配置文件,配置redis,
列出部分代码如下:
1.初始化对象:
public class RedisInst { public RedisInst() { } public static IMyRedis getInstance() { FileConf fileConf = FileConf.getInstance();//获取配置文件的内容 RedisConfig config = new RedisConfig(); //redis配置集 config.setCluster(fileConf.isCluster()); config.setHost(fileConf.getHost()); config.setPort(fileConf.getPort()); config.setAuth(fileConf.getAuth()); config.setDataBase(fileConf.getDataBase()); config.setHoststr(fileConf.getHoststr()); config.setMaxIdle(fileConf.getMaxIdle()); config.setMinIdle(fileConf.getMinIdle()); config.setTestOnBorrow(fileConf.isTestOnBorrow()); config.setTimeout(fileConf.getTimeout()); config.setMaxRedirections(fileConf.getMaxRedirections()); config.setMaxWaitMillis(fileConf.getMaxWaitMillis()); config.setMaxTotal(fileConf.getMaxTotal()); config.setSoTimeout(fileConf.getSoTimeout()); return RedisClient.getInstance(config); } static { FileConf.getInstance(); } }
2.单例模式构造
public class FileConf { public static Logger logger = Logger.getLogger(FileConf.class.getName()); private static final String REDIS_IS_CLUSTER = "REDIS_IS_CLUSTER"; private static final String REDIS_MAX_ACTIVE = "REDIS_MAX_ACTIVE"; private static final String REDIS_MAX_IDLE = "REDIS_MAX_IDLE"; private static final String REDIS_MIN_IDLE = "REDIS_MIN_IDLE"; private static final String REDIS_TIMEOUT = "REDIS_TIMEOUT"; private static final String REDIS_HOST = "REDIS_HOST"; private static final String REDIS_PORT = "REDIS_PORT"; private static final String REDIS_PWD = "REDIS_PWD"; private static final String REDIS_DATABASE = "REDIS_DATABASE"; private static final String REDIS_TESTONBORROW = "TESTONBORROW"; private static final String REDIS_MAX_WAIT = "REDIS_MAX_WAIT"; private static final String REDIS_HOST_LIST = "REDIS_HOST_LIST"; private static final String REDIS_SOTIMEOUT = "REDIS_SOTIMEOUT"; private static final String REDIS_MAX_REDIRECTIONS = "REDIS_MAX_REDIRECTIONS"; private String REDIS_CONF_FILE = "redis_config.properties"; private boolean isCluster = false; private int timeout = 5000; private int maxTotal = 5; private int maxIdle = 2; private String hoststr = "127.0.0.1:6379"; private int soTimeout = 5000; private int maxRedirections = 6; private int minIdle = 10; private String host = "127.0.0.1"; private String auth; private long maxWaitMillis = 10000L; private int port = 6379; private int dataBase = 0; private static final Object initLock = new Object(); private static volatile FileConf instance = null; private boolean testOnBorrow = false; private FileConf() { this.loadProperties(); } public static FileConf getInstance() { if (null == instance) { Object var0 = initLock; synchronized(initLock) { if (null == instance) { instance = new FileConf(); } } } return instance; } public void reloadProperties(String confFileName) { this.REDIS_CONF_FILE = confFileName; this.loadProperties(); } private void loadProperties() { Object stream = null; try { File f = new File(System.getProperty("user.dir") + File.separator + this.REDIS_CONF_FILE); if (f.exists()) { stream = new BufferedInputStream(new FileInputStream(f)); } else { stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(this.REDIS_CONF_FILE); } Properties props = new Properties(); props.load((InputStream)stream); this.maxTotal = Integer.parseInt(props.getProperty("REDIS_MAX_ACTIVE", "" + this.maxTotal).trim()); this.maxIdle = Integer.parseInt(props.getProperty("REDIS_MAX_IDLE", "" + this.maxIdle).trim()); this.minIdle = Integer.parseInt(props.getProperty("REDIS_MIN_IDLE", "" + this.minIdle).trim()); this.maxWaitMillis = (long)Integer.parseInt(props.getProperty("REDIS_MAX_WAIT", "" + this.maxWaitMillis).trim()); this.timeout = Integer.parseInt(props.getProperty("REDIS_TIMEOUT", "" + this.timeout).trim()); this.host = props.getProperty("REDIS_HOST", "Cluster").trim(); String[] ipport = this.host.split(":"); if (ipport != null && ipport.length > 1) { this.host = ipport[0]; this.port = Integer.parseInt(ipport[1]); } else { this.port = Integer.parseInt(props.getProperty("REDIS_PORT", "" + this.port).trim()); } this.auth = props.getProperty("REDIS_PWD"); this.isCluster = Integer.parseInt(props.getProperty("REDIS_IS_CLUSTER", "1").trim()) == 1; this.hoststr = props.getProperty("REDIS_HOST_LIST", "Cluster").trim(); this.soTimeout = Integer.parseInt(props.getProperty("REDIS_SOTIMEOUT", "" + this.soTimeout).trim()); this.maxRedirections = Integer.parseInt(props.getProperty("REDIS_MAX_REDIRECTIONS", "" + this.maxRedirections).trim()); this.dataBase = Integer.parseInt(props.getProperty("REDIS_DATABASE", "" + this.dataBase).trim()); } catch (Exception var12) { throw new RuntimeException(var12); } finally { if (null != stream) { try { ((InputStream)stream).close(); } catch (IOException var11) { ; } } } } public boolean isCluster() { return isCluster; } public void setCluster(boolean cluster) { isCluster = cluster; } public int getTimeout() { return timeout; } public void setTimeout(int timeout) { this.timeout = timeout; } public int getMaxTotal() { return maxTotal; } public void setMaxTotal(int maxTotal) { this.maxTotal = maxTotal; } public int getMaxIdle() { return maxIdle; } public void setMaxIdle(int maxIdle) { this.maxIdle = maxIdle; } public String getHoststr() { return hoststr; } public void setHoststr(String hoststr) { this.hoststr = hoststr; } public int getSoTimeout() { return soTimeout; } public void setSoTimeout(int soTimeout) { this.soTimeout = soTimeout; } public int getMaxRedirections() { return maxRedirections; } public void setMaxRedirections(int maxRedirections) { this.maxRedirections = maxRedirections; } public int getMinIdle() { return minIdle; } public void setMinIdle(int minIdle) { this.minIdle = minIdle; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getAuth() { return auth; } public void setAuth(String auth) { this.auth = auth; } public long getMaxWaitMillis() { return maxWaitMillis; } public void setMaxWaitMillis(long maxWaitMillis) { this.maxWaitMillis = maxWaitMillis; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public int getDataBase() { return dataBase; } public void setDataBase(int dataBase) { this.dataBase = dataBase; } public boolean isTestOnBorrow() { return testOnBorrow; } public void setTestOnBorrow(boolean testOnBorrow) { this.testOnBorrow = testOnBorrow; } }