I have the following generic cglib method interceptor implementation:
public class EntityInterceptor<T, PK> implements MethodInterceptor{
private EntityChangeType changeType;
private T entity;
private GenericCrudMapper<T,PK> mapper;
public EntityInterceptor(T entity, GenericCrudMapper<T, PK> mapper){
this.entity = entity;
this.mapper = mapper;
}
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
// pass through
return proxy.invoke(entity, args);
}
public void setEntityChangeType(EntityChangeType changeType){
this.changeType = changeType;
}
public void saveChanges(){
// @todo
}
}
Which is used as follows:
@Override
public Airport get(String id) {
Airport airport = airportMapper.findById(id);
if(airport != null){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Airport.class);
enhancer.setCallback(new EntityInterceptor<>(airport, airportMapper));
airport = (Airport) enhancer.create();
return airport;
}
return airport;
}
Can I safely assume that the method interceptor created above will be instantiated per enhanced class instance (in other words, when assigning method interceptors, they are not shared between all class instances)?