public class Tuz
extends java.lang.Object
整个项目的核心类
The core of the whole project
Example 1:
加载资源文件 API: load(Loadable resource) throws IOException test 是命名空间,后面一个是资源加载器 "test.properties" 文件中有一个属性:number=16
Tuz.load(new ClasspathPropertiesLoader("test.properties", "test"));
当然,你也可以不指定命名空间,内部会自动生成一个命名空间 不过,为了性能和正确性,还是建议您使用自定义的命名空间 当你不指定命名空间时,就会使用文件名作为命名空间
Tuz.load(new ClasspathPropertiesLoader("test2.properties"));或者,您也可以使用另外一个加载器去加载文件系统中的资源文件 Tuz.load(new FileSystemPropertiesLoader("Z:/test.properties", "test"));
下面就是激动人心的时刻了!您可以在任意地方使用您的资源! 上面提到,初始化的资源文件中有一个属性:number=16 您可以在任意地方使用这个资源,像这样: API: use(String key, String namespace) 其中上面的代码中 number 是资源名字,test 是命名空间
String number = Tuz.use("number", "test"); // ===> 返回 16
System.out.println(number);
同样,您可以不指定命名空间,但是这不被推荐 具体原因请看 cn.com.fishin.tuz.core.Tuz.use(java.lang.String) String number = Tuz.use("number"); // ===> 返回 16
Example 2:
传统的方式使用接口: xxxService service = new xxxServiceImpl(); 这种方式并没有真正解耦,使用 Spring 这类框架可以达到解耦效果,但是需要引入大量框架 而使用 Tuz 可以轻松做到解耦,请看下面:
Tuz.load(new ClasspathPropertiesLoader("test.properties", "test"));
// 直接获取实现类,而不用注入实现类的细节
xxxService service = DiPlugin.useInstance("xxxService", "test", xxxService.class);
service.say("Hello, Tuz!");
同样的,你可以不指定命名空间,但是,真的不推荐!!! 注意这里的 xxxService 在配置文件中的 key 就是 xxxService //Tuz.load(new ClasspathPropertiesLoader("test.properties")); //xxxService service = DiPlugin.useInstance(xxxService.class); //service.say("Hello, Tuz!");
Email: fishinlove@163.com
created by 2019/03/28 14:20:32
| 构造器和说明 |
|---|
Tuz() |
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
appendResource(java.util.Map<java.lang.String,java.lang.String> resource,
java.lang.String namespace)
往指定的 namespace 中添加新的资源值
Add one value to the pointed namespace
|
static void |
appendResource(java.lang.String key,
java.lang.String value,
java.lang.String namespace)
往指定的 namespace 中添加新的资源值
Add one value to the pointed namespace
|
static void |
destroy()
清除一切已经加载的资源
建议在程序关闭时调用!!!
|
static TuzConfig |
getConfig()
获取全局配置
Get global setting
|
static void |
init()
重新载入默认配置
当你更改了 Tuz 的配置之后,希望回到最原始的默认配置
另外,这里也可以解决一下一些问题,比如,在使用 NetPlugin 的时候,
由于 config 的加载早于 load 方法执行,所以会导致默认配置有些找不到,
当你也遇到类似的问题时,可以尝试在执行 load 方法之后,执行一下这个方法!
|
static void |
load(Loadable resource)
加载一个资源文件
Load a resource
|
static void |
reLoad(Loadable resource)
重新载入一个资源文件
ReLoad a resource
|
static void |
setConfig(TuzConfig config)
设置全局配置
Set global setting
|
static void |
unLoad(Loadable resource)
卸载一个资源文件
UnLoad a resource
|
static void |
unLoad(java.lang.String namespace)
卸载一个命名空间
UnLoad a namespace
|
static java.lang.String |
unUse(java.lang.String key,
java.lang.String namespace)
移除指定的 namespace 的资源值
Remove the value of the namespace's key
|
static java.lang.String |
use(java.lang.String key)
获取指定 key 的属性值
强烈注意:
如果不指定命名空间,当多个配置文件中的 key 值一样的时候,
这个方法无法保证返回你需要的那个 key 值对应的 value!!
|
static java.lang.String |
use(java.lang.String key,
java.lang.String namespace)
获取指定 namespace 的资源值
Fetch the value of the namespace's key
|
static java.lang.String |
useGracefully(java.lang.String key,
java.lang.String defaultValue)
获取指定 key 的属性值
强烈注意:
如果不指定命名空间,当多个配置文件中的 key 值一样的时候,
这个方法无法保证返回你需要的那个 key 值对应的 value!!
|
static java.lang.String |
useGracefully(java.lang.String key,
java.lang.String namespace,
java.lang.String defaultValue)
获取指定 key 的属性值
Fetch the value of the key
|
public static TuzConfig getConfig()
获取全局配置
Get global setting
全局配置信息
Global config
public static void setConfig(TuzConfig config)
设置全局配置
Set global setting
config - 全局配置信息
Global config
public static void load(Loadable resource) throws java.lang.Throwable
加载一个资源文件
Load a resource
resource - 要被加载的资源文件
The resource to be loaded
java.lang.Throwable - 找不到资源文件就会抛出这个异常
The resource is not found
public static void unLoad(Loadable resource) throws java.lang.Throwable
卸载一个资源文件
UnLoad a resource
resource - 要被卸载的资源文件
The resource to be unloaded
java.lang.Throwable - 找不到资源文件就会抛出这个异常
The resource is not found
public static void unLoad(java.lang.String namespace)
throws java.lang.Throwable
卸载一个命名空间
UnLoad a namespace
namespace - 要被卸载的命名空间
The namespace to be unloaded
java.lang.Throwable - 找不到资源文件就会抛出这个异常
The resource is not found
public static void reLoad(Loadable resource) throws java.lang.Throwable
重新载入一个资源文件
ReLoad a resource
resource - 要被重新载入的资源文件
The resource to be reloaded
java.lang.Throwable - 找不到资源文件就会抛出这个异常
The resource is not found
public static java.lang.String use(java.lang.String key,
java.lang.String namespace)
获取指定 namespace 的资源值
Fetch the value of the namespace's key
key - 指定资源值的 key
The key of the value
namespace - 指定的命名空间,用于区分不同的资源文件
Appointed namespace to different resource
返回获取到的资源值,找不到返回 null
Return the value of the key, null if not found
public static java.lang.String unUse(java.lang.String key,
java.lang.String namespace)
throws java.lang.Throwable
移除指定的 namespace 的资源值
Remove the value of the namespace's key
key - 指定资源值的 key
The key of the value
namespace - 指定的命名空间,用于区分不同的资源文件
Appointed namespace to different resource
返回移除前的资源值,找不到返回 null
Return the value of the removed key, null if not found
java.lang.Throwable - 找不到资源文件就会抛出这个异常
The resource is not found
public static void appendResource(java.util.Map<java.lang.String,java.lang.String> resource,
java.lang.String namespace)
throws java.lang.Throwable
往指定的 namespace 中添加新的资源值
Add one value to the pointed namespace
resource - 要被添加的资源值
The resource to be appended
namespace - 指定的命名空间,用于区分不同的资源文件
Appointed namespace to different resource
java.lang.Throwable - 找不到资源文件就会抛出这个异常
The resource is not found
public static void appendResource(java.lang.String key,
java.lang.String value,
java.lang.String namespace)
throws java.lang.Throwable
往指定的 namespace 中添加新的资源值
Add one value to the pointed namespace
key - 要被添加的资源的 key
The key of resource to be appended
value - 要被添加的资源值
、The resource to be appended
namespace - 指定的命名空间,用于区分不同的资源文件
Appointed namespace to different resource
java.lang.Throwable - 找不到资源文件就会抛出这个异常
The resource is not found
public static java.lang.String useGracefully(java.lang.String key,
java.lang.String namespace,
java.lang.String defaultValue)
获取指定 key 的属性值
Fetch the value of the key
key - 指定的 key
The key of the value
namespace - 指定的命名空间,用于区分不同的资源文件
Appointed namespace to different resource
defaultValue - 如果找不到这个 key 返回的默认值
If the key is not found, return defaultValue
返回获取到的属性值,找不到返回 null
Return the value of the key, null if not found
public static java.lang.String use(java.lang.String key)
获取指定 key 的属性值
强烈注意: 如果不指定命名空间,当多个配置文件中的 key 值一样的时候, 这个方法无法保证返回你需要的那个 key 值对应的 value!! 由于哈希算法存在随机性,所以有可能返回另外一个值!! 因此,不管是出于性能还是运行的正常性,强烈推荐使用自定义命名空间!:)
Fetch the value of the key
Notice: If you do not appoint a namespace, and you have some same key in some resources, then you may get a wrong value of the key! Due to hash, this gonna be a possible thing! So, listen to me, you are recommended to use a namespace! That's my boy:)
key - 指定的 key
The key of the value
返回获取到的属性值,找不到返回 null
Return the value of the key, null if not found
public static java.lang.String useGracefully(java.lang.String key,
java.lang.String defaultValue)
获取指定 key 的属性值
强烈注意: 如果不指定命名空间,当多个配置文件中的 key 值一样的时候, 这个方法无法保证返回你需要的那个 key 值对应的 value!! 由于哈希算法存在随机性,所以有可能返回另外一个值!! 因此,不管是出于性能还是运行的正常性,强烈推荐使用自定义命名空间!:)
Fetch the value of the key
Notice: If you do not appoint a namespace, and you have some same key in some resources, then you may get a wrong value of the key! Due to hash, this gonna be a possible thing! So, listen to me, you are recommended to use a namespace! That's my boy:)
key - 指定的 key
The key of the value
defaultValue - 找不到返回这个值
Return this value if not found
返回获取到的属性值,找不到返回 null
Return the value of the key, null if not found
public static void init()
重新载入默认配置
当你更改了 Tuz 的配置之后,希望回到最原始的默认配置
另外,这里也可以解决一下一些问题,比如,在使用 NetPlugin 的时候,
由于 config 的加载早于 load 方法执行,所以会导致默认配置有些找不到,
当你也遇到类似的问题时,可以尝试在执行 load 方法之后,执行一下这个方法!
Reload default config
If change config of Tuz, and you want it back to default.
Also, some problems happen because load method is back of initializing config.
So, if you have some problems with this config, try to invoke this method after invoking load()
public static void destroy()
清除一切已经加载的资源
建议在程序关闭时调用!!!
Clear all resources
You should invoke this method before closing program
Copyright © 2019. All Rights Reserved.