나는 파스칼의 대답을 정답으로 확인하고 있는데, 그는 세부 사항과 예제 코드를 제공 한 문서의 요점을 직접 지적했다. 그러나 독자를 위해 여기에 더 자세히 설명하겠습니다.
나는 인터셉터뿐만 아니라 최대 절전 모드 이벤트를 시도하고 인터셉터가 내 상황에서 더 잘 작동한다는 것을 알았다. 구현하기가 비교적 쉬웠습니다.
도움 주셔서 감사합니다.
public class AuditInterceptor extends EmptyInterceptor {
private Log log = LogFactory.getLog(this.getClass());
private int updates;
private int creates;
@Override
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) {
if (entity instanceof AuditableVO) {
creates++;
// Find the create date and change it
for (int i=0; i < propertyNames.length; i++) {
if (propertyNames[i].equals("createDate")) {
state[i] = new Date();
return true;
}
}
}
return false;
}
@Override
public boolean onFlushDirty(Object entity, Serializable id,
Object[] currentState, Object[] previousState,
String[] propertyNames, Type[] types) {
if (entity instanceof AuditableVO) {
updates++;
// Find the update date and change it
for (int i=0; i < propertyNames.length; i++) {
if (propertyNames[i].equals("updateDate")) {
currentState[i] = new Date();
return true;
}
}
}
return false;
}
@Override
public void afterTransactionCompletion(Transaction tx) {
if (tx.wasCommitted()) {
log.info("Creations: " + creates + ", Updates: " + updates);
}
creates = 0;
updates = 0;
super.afterTransactionCompletion(tx);
}
}
그래서 당신은'setUpdateDate (...)'호출로 ** 모든 ** 당신의 세터를 오염 : 아래
는 인터셉터에 대한 코드? 나는 이것을 (당신이 설정하고 완료 한) 요격기보다 쉽게 발견하지 못하고 매우 추악합니다. –일종의 이벤트를 유발하는 것 이상은 아닙니다. –
그게 무슨 소리 죠? 이러한 이벤트가 있으며 최대 절전 모드로 인해 트리거됩니다. 따라서 리스너를 차별화하여 활용할 수는 없습니다. 어쩌면 당신은 문서를 읽어야합니다. –