This commit is contained in:
wentch
2016-01-28 17:44:18 +08:00
parent 2172ecf4c7
commit e54e673774

View File

@@ -7,8 +7,6 @@ package org.redkale.test.util;
import java.math.*; import java.math.*;
import javax.annotation.*; import javax.annotation.*;
import org.redkale.convert.*;
import org.redkale.convert.json.*;
import org.redkale.util.*; import org.redkale.util.*;
/** /**
@@ -17,45 +15,50 @@ import org.redkale.util.*;
*/ */
public class ResourceTest { public class ResourceTest {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
ResourceFactory factory = ResourceFactory.root(); ResourceFactory factory = ResourceFactory.root();
factory.register("property.id", "2345"); factory.register("property.id", "2345"); //注入String类型的property.id
TestService service = new TestService(); AService aservice = new AService();
TwinService twinService = new TwinService("eeeee"); BService bservice = new BService("eeeee");
factory.register(service); factory.register(aservice); //放进Resource池内默认的资源名name为""
factory.register(twinService); factory.register(bservice); //放进Resource池内默认的资源名name为""
factory.inject(service); factory.inject(aservice); //给aservice注入id、bservicebigint没有资源所以为null
System.out.println("--------------------------------------"); factory.inject(bservice); //给bservice注入id、aservice
factory.inject(twinService); System.out.println(aservice); //输出结果为:{id:"2345", intid: 2345, bigint:null, bservice:{name:eeeee}}
System.out.println(service); System.out.println(bservice); //输出结果为:{name:"eeeee", id: 2345, aserivce:{id:"2345", intid: 2345, bigint:null, bservice:{name:eeeee}}}
System.out.println(twinService);
factory.register("seqid", 200);
factory.register("bigint", new BigInteger("666666666666666"));
System.out.println(factory.find("seqid", int.class));
factory.register("property.id", "6789");
twinService = new TwinService("ffff");
factory.inject(twinService);
factory.register(twinService);
System.out.println(service);
System.out.println(twinService);
factory.register("seqid", int.class, null);
System.out.println(service);
}
public static class TwinService { factory.register("seqid", 200); //放进Resource池内, 同时ResourceFactory会自动更新aservice的seqid值
System.out.println(factory.find("seqid", int.class)); //输出结果为200
factory.register("bigint", new BigInteger("666666666666666")); //放进Resource池内, 同时ResourceFactory会自动更新aservice对象的bigint值
System.out.println(aservice); //输出结果为:{id:"2345", intid: 2345, bigint:666666666666666, bservice:{name:eeeee}} 可以看出seqid与bigint值都已自动更新
factory.register("property.id", "6789"); //更新Resource池内的id资源值, 同时ResourceFactory会自动更新aservice、bservice的id值
System.out.println(aservice); //输出结果为:{id:"6789", intid: 6789, bigint:666666666666666, bservice:{name:eeeee}}
System.out.println(bservice); //输出结果为:{name:"eeeee", id: 6789, aserivce:{id:"6789", intid: 6789, bigint:666666666666666, bservice:{name:eeeee}}}
bservice = new BService("ffff");
factory.register(bservice); //更新Resource池内name=""的BService资源, 同时ResourceFactory会自动更新aservice的bservice对象
factory.inject(bservice);
System.out.println(aservice); //输出结果为:{id:"6789", intid: 6789, bigint:666666666666666, bservice:{name:ffff}}
}
}
class BService {
@Resource(name = "property.id") @Resource(name = "property.id")
private String id; private String id;
@Resource @Resource
private TestService service; private AService aservice;
private String name = ""; private String name = "";
@java.beans.ConstructorProperties({"name"}) @java.beans.ConstructorProperties({"name"})
public TwinService(String name) { public BService(String name) {
this.name = name; this.name = name;
} }
@@ -75,26 +78,26 @@ public class ResourceTest {
this.id = id; this.id = id;
} }
public TestService getService() { public AService getAservice() {
return service; return aservice;
} }
public void setService(TestService service) { public void setAservice(AService aservice) {
this.service = service; this.aservice = aservice;
} }
@Override @Override
public String toString() { public String toString() {
return "{name:\"" + name + "\", id: " + id + ", serivce:" + service + "}"; return "{name:\"" + name + "\", id: " + id + ", aserivce:" + aservice + "}";
}
} }
}
public static class TestService { class AService {
@Resource(name = "property.id") @Resource(name = "property.id")
private String id; private String id;
@Resource(name = "property.id") @Resource(name = "property.id") //property.开头的资源名允许String自动转换成primitive数值类型
private int intid; private int intid;
@Resource(name = "bigint") @Resource(name = "bigint")
@@ -104,7 +107,12 @@ public class ResourceTest {
private int seqid; private int seqid;
@Resource @Resource
private TwinService service; private BService bservice;
@Override
public String toString() {
return "{id:\"" + id + "\", intid: " + intid + ", bigint:" + bigint + ", bservice:" + (bservice == null ? null : ("{name:" + bservice.getName() + "}")) + "}";
}
public String getId() { public String getId() {
return id; return id;
@@ -138,18 +146,8 @@ public class ResourceTest {
this.bigint = bigint; this.bigint = bigint;
} }
@ConvertColumn(ignore = true) public void setBservice(BService bservice) {
public TwinService getService() { this.bservice = bservice;
return service;
} }
public void setService(TwinService service) {
this.service = service;
}
@Override
public String toString() {
return JsonConvert.root().convertTo(this);
}
}
} }