經測試確認,當一個接口有多個實現時,調用時只會執行一個
有時候需要多個實現調用,方法示例如下:
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(); } } }