最近想把一些常用的工具整理成通用的工具类,比如jedis,MNS 等,但是 这些工具都依赖于配置文件:比如jedis需要配置ip,port等。如果把这些写死就失去了封装工具类的意义。
我尝试过把这些 参数写成变量,然后用构造方法初始化这些变量,得到JedisPool 等。但是这样的封装 又依赖于对象,在业务中每次都要new出来 ,无法像常用的工具类那样 变成static 方法引用。
如下图,所以想问问各位 大神有什么 好的封装思路?
ringa_lee2017-04-18 10:26:53
General approach:
Write the parameters that need to be configured into the configuration file (for example: redis.properties)
Load this file when the project starts, read the values inside,
Initialize redisPool
The spring-data-redis spring package used in my project here is okay!
PHPz2017-04-18 10:26:53
These are all configurations, just write them in the configuration file. At most, different profile configurations can be switched according to the environment.