In the configuration file for the configuration of the system parameters, sometimes need to better understand the specific meaning of the parameter values, often in the form of calculation expressions, such as 1 days into seconds to 86400, if written 24 * 60 * 60 is a clear expression is the number of seconds per day. However, this expression is obtained as a string by the properties, and this expression needs to be evaluated dynamically.
ScriptEngine This object is specifically designed to handle dynamic execution expressions, primarily calling its Eval method to execute dynamically (similar to the Eval method in JavaScript), which returns a result of object, and the return result for a computed expression is a double type. So here we first convert the object to a double and then cast it according to the actual need.
The configuration file is as follows:
#标准缓存时长 (unit: seconds) #默认为1天 (24 * 60 * 60) or 86400STD_CACHE_TIME_OUT=24 * 60 * 60
Its usage is as follows:
Scriptenginemanager _mgr = new Scriptenginemanager ();//Only JavaScript actuators are called here, and JavaScript scripts can be executed, Other types of scripts can also be called scriptengine _enginer = _mgr.getenginebyname ("javascript"); try{String expression = Properties.getproperty ("Std_cache_time_out"); Object result = _enginer.eval (expression); _max_cache_time_out = (long) double.parsedouble (result.tostring ()); }}catch (Scriptexception e) {e.printstacktrace (); }
See the ScriptEngine using the API with the method provided by the ScriptEngine interface
This article from "Good memory than bad writing" blog, please be sure to keep this source http://gytian.blog.51cto.com/1065457/1604847
Java Dynamic Execution computation expression weapon--scriptengine