This commit is contained in:
@@ -7,8 +7,6 @@ package org.redkale.test.util;
|
||||
|
||||
import java.math.*;
|
||||
import javax.annotation.*;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
@@ -17,139 +15,139 @@ import org.redkale.util.*;
|
||||
*/
|
||||
public class ResourceTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ResourceFactory factory = ResourceFactory.root();
|
||||
factory.register("property.id", "2345");
|
||||
TestService service = new TestService();
|
||||
TwinService twinService = new TwinService("eeeee");
|
||||
public static void main(String[] args) throws Exception {
|
||||
ResourceFactory factory = ResourceFactory.root();
|
||||
factory.register("property.id", "2345"); //注入String类型的property.id
|
||||
AService aservice = new AService();
|
||||
BService bservice = new BService("eeeee");
|
||||
|
||||
factory.register(service);
|
||||
factory.register(twinService);
|
||||
factory.register(aservice); //放进Resource池内,默认的资源名name为""
|
||||
factory.register(bservice); //放进Resource池内,默认的资源名name为""
|
||||
|
||||
factory.inject(service);
|
||||
System.out.println("--------------------------------------");
|
||||
factory.inject(twinService);
|
||||
System.out.println(service);
|
||||
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);
|
||||
factory.inject(aservice); //给aservice注入id、bservice,bigint没有资源,所以为null
|
||||
factory.inject(bservice); //给bservice注入id、aservice
|
||||
System.out.println(aservice); //输出结果为:{id:"2345", intid: 2345, bigint:null, bservice:{name:eeeee}}
|
||||
System.out.println(bservice); //输出结果为:{name:"eeeee", id: 2345, aserivce:{id:"2345", intid: 2345, bigint:null, bservice:{name:eeeee}}}
|
||||
|
||||
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")
|
||||
private String id;
|
||||
|
||||
@Resource
|
||||
private AService aservice;
|
||||
|
||||
private String name = "";
|
||||
|
||||
@java.beans.ConstructorProperties({"name"})
|
||||
public BService(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static class TwinService {
|
||||
|
||||
@Resource(name = "property.id")
|
||||
private String id;
|
||||
|
||||
@Resource
|
||||
private TestService service;
|
||||
|
||||
private String name = "";
|
||||
|
||||
@java.beans.ConstructorProperties({"name"})
|
||||
public TwinService(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public TestService getService() {
|
||||
return service;
|
||||
}
|
||||
|
||||
public void setService(TestService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{name:\"" + name + "\", id: " + id + ", serivce:" + service + "}";
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public static class TestService {
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Resource(name = "property.id")
|
||||
private String id;
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Resource(name = "property.id")
|
||||
private int intid;
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Resource(name = "bigint")
|
||||
private BigInteger bigint;
|
||||
public AService getAservice() {
|
||||
return aservice;
|
||||
}
|
||||
|
||||
@Resource(name = "seqid")
|
||||
private int seqid;
|
||||
public void setAservice(AService aservice) {
|
||||
this.aservice = aservice;
|
||||
}
|
||||
|
||||
@Resource
|
||||
private TwinService service;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getIntid() {
|
||||
return intid;
|
||||
}
|
||||
|
||||
public void setIntid(int intid) {
|
||||
this.intid = intid;
|
||||
}
|
||||
|
||||
public int getSeqid() {
|
||||
return seqid;
|
||||
}
|
||||
|
||||
public void setSeqid(int seqid) {
|
||||
this.seqid = seqid;
|
||||
}
|
||||
|
||||
public BigInteger getBigint() {
|
||||
return bigint;
|
||||
}
|
||||
|
||||
public void setBigint(BigInteger bigint) {
|
||||
this.bigint = bigint;
|
||||
}
|
||||
|
||||
@ConvertColumn(ignore = true)
|
||||
public TwinService getService() {
|
||||
return service;
|
||||
}
|
||||
|
||||
public void setService(TwinService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{name:\"" + name + "\", id: " + id + ", aserivce:" + aservice + "}";
|
||||
}
|
||||
}
|
||||
|
||||
class AService {
|
||||
|
||||
@Resource(name = "property.id")
|
||||
private String id;
|
||||
|
||||
@Resource(name = "property.id") //property.开头的资源名允许String自动转换成primitive数值类型
|
||||
private int intid;
|
||||
|
||||
@Resource(name = "bigint")
|
||||
private BigInteger bigint;
|
||||
|
||||
@Resource(name = "seqid")
|
||||
private int seqid;
|
||||
|
||||
@Resource
|
||||
private BService bservice;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{id:\"" + id + "\", intid: " + intid + ", bigint:" + bigint + ", bservice:" + (bservice == null ? null : ("{name:" + bservice.getName() + "}")) + "}";
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getIntid() {
|
||||
return intid;
|
||||
}
|
||||
|
||||
public void setIntid(int intid) {
|
||||
this.intid = intid;
|
||||
}
|
||||
|
||||
public int getSeqid() {
|
||||
return seqid;
|
||||
}
|
||||
|
||||
public void setSeqid(int seqid) {
|
||||
this.seqid = seqid;
|
||||
}
|
||||
|
||||
public BigInteger getBigint() {
|
||||
return bigint;
|
||||
}
|
||||
|
||||
public void setBigint(BigInteger bigint) {
|
||||
this.bigint = bigint;
|
||||
}
|
||||
|
||||
public void setBservice(BService bservice) {
|
||||
this.bservice = bservice;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user