public class VersionableTreePojo<T> {
/**
* 直接父节点
*/
@Column
private long parentId;
@One(target = T, field = "parentId")
private T parent;
@One(target = T, field = "parentId") 此处 T 如何写,一般写 Pojo.class ,这处不会写了
我的想法是用泛型描述一类Tree结构的Class,所以注解也需要泛型,有办法么 @wendal
在问一个问题:
能否有不通过异常方式处理的。getBean方法不传参数。
class Trans<T> {
Object obj;
public Trans(Object obj) {
this.obj = obj;
}
public T getBean() {
try {
T t = (T) obj;
return t;
} catch (Exception e) {
return null;
}
}
}
最近在弄一些项目。大致意思见代码:
public class Test {
public static void main(String[] args) {
Test t = new Test();
// begin
MessageHolder messageHolder = t.new MessageHolder("从QUEUE中得到的其中一条");
ServiceLoader<MessageReceiver> messageReceiverServiceLoader = ServiceLoader.load(MessageReceiver.class);
Iterator<MessageReceiver> receiverIterator = messageReceiverServiceLoader.iterator();
while (receiverIterator.hasNext()) {
receiverIterator.next().doReceive(messageHolder);
}
}
class MessageHolder<T> {
Object obj;
public MessageHolder(Object obj) {
this.obj = obj;
}
public T getBean() {
//将obj做各种处理或转化等,然后返回T
try {
T t = (T) obj;
return t;
} catch (Exception e) {
return null;
}
}
}
abstract class MessageReceiver<T> {
public void parse(MessageHolder<T> message) {
T t = message.getBean();
if (t != null) {
doReceive(t);
}
}
public abstract void doReceive(T messageVo);
}
/**
* SPI配置中的一个实现类
*/
class UserMsgReceiver extends MessageReceiver<Test> {
public void doReceive(Test userInfo) {
//do something with userInfo.
}
}
}