동적 프록시가 AOP에 사용됨을 알고있었습니다.
그러나 게으른로드에도 사용할 수 있습니다.
기사의 다음 예제는이를 입증하기위한 것입니다.
그러나 이것이 일반적인 접근 자와 다른 점과 정확히 무엇이 '느슨하게'로드되고 있는지 이해하지 못합니까?
작성자가 지연로드로 의도 한 내용을 이해하는 데 도움이되는 정보가 있습니다. 이 다음에서 어떤 다를 수 woul 어떻게동적 대 지연로드
private Category tupleToObject(Serializable[] tuple) {
Category category = new Category((String)tuple[1],
(YearMonthDay) tuple[2]);
category.setId((Long) tuple[0]);
category.setParent(lazyGet((Long) tuple[3]));
return category;
}
protected CategoryItf lazyGet(Long id) {
if (id == null) {
return null;
}
return (CategoryItf)Proxy.newProxyInstance(
CategoryItf.class.getClassLoader(),
new Class[] { CategoryItf.class },
new LazyLoadedObject() {
protected Object loadObject() {
return get(id);
}
});
}
public abstract class LazyLoadedObject implements InvocationHandler {
private Object target;
public Object invoke(Object proxy,
Method method, Object[] args)
throws Throwable {
if (target == null) {
target = loadObject();
}
return method.invoke(target, args);
}
protected abstract Object loadObject();
}
: 필요한 경우 두 경우 모두
private Category tupleToObject(Serializable[] tuple) {
Category category = new Category((String)tuple[1],
(YearMonthDay) tuple[2]);
category.setId((Long) tuple[0]);
category.setParent(get((Long) tuple[3]));
return category;
}
은 부모 만 생성됩니다.
private Object target;
public Object invoke(Object proxy,
Method method, Object[] args)
throws Throwable {
if (target == null) {
target = loadObject();
}
당신은 상관없이 당신이 동일한 개체마다 시간을 얻을이 코드를 호출하는 방법을 여러 번 볼 수 있습니다 :
내 질문에 더 초점을 맞추기 위해 편집되었습니다. – IUnknown
AlexR, 나는 내 대답을 얻지 못하고 새로운 것으로 질문을 제기 할 수 없다. 지금은 너무 낮아서 여기서 도와 줘. http://stackoverflow.com/questions/17220399/cannot-instantiate-a -class-using-a-button –