经测试确认,当一个接口有多个实现时,调用时只会执行一个
有时候需要多个实现调用,方法示例如下:
public interface TransCallbackInterface { public void callback(String taskId, int code, String fixed); }
@Component public class TransCallbackDy implements InitializingBean,TransCallbackInterface{ @Override public void callback(String taskId, int code, String fixed) { System.out.println("TransCallback"); }
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("callback registerCallbackProcessor .");
FileTransferShedule.registerCallbackProcessor(this);
}
}
@Component public class TransCallbackDy implements InitializingBean, TransCallbackInterface{ @Override public void callback(String taskId, int code, String fixedInfo) { System.out.println("TransCallback"); }
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("callback registerCallbackProcessor .");
FileTransferShedule.registerCallbackProcessor(this);
}
}
调用方式:
@Component public class FileTransferShedule implements InitializingBean, DisposableBean { @Override public void afterPropertiesSet() throws Exception { } @Override public void destroy() throws Exception { logger.debug("service closed"); } private static List<TransCallbackInterface> processors = new ArrayList<TransCallbackInterface>(); public static void registerCallbackProcessor( TransCallbackInterface processor) { synchronized (processors) { processors.add(processor); } } public static void unregisterCallbackProcessor( TransCallbackInterface processor) { synchronized (processors) { processors.remove(processor); } } public void callback(HttpServletRequest request) { logger.debug("回调接口测试"); try { Throwable t = null; synchronized (processors) { for (TestCallbackInterface processor : processors) { try { processor.callback(); } catch (Throwable e) { t = e; } } } if (t != null) { throw new IOException(t); } System.out.println("test"); } catch (Exception e) { e.printStackTrace(); } } }