The principle is simple to state and demanding to follow: not a single drop must reach the system. In practice, the best approach is to remove the plant from the system when it is in a pot or basket, treat it away from the setup, let it drain completely, then replace it once the leaves are dry. When the plant cannot be moved, slide a sheet of plastic film or cardboard under the foliage to cover the entire surface of the grow bed and the water below, and spray directing the jet upwards, under the leaves, which is precisely where aphids and mites tend to be found anyway.
Timing matters as much as the precautions taken. University technical data sheets on insecticidal soaps recommend avoiding applications in hot weather, above around 32 °C, in full sun or in dry conditions, and treating early in the morning or in the late afternoon. They also advise testing a small area of foliage and waiting twenty-four hours before treating the whole plant, as phytotoxicity varies considerably from one species to another. Aim for complete leaf coverage without excessive run-off, which is precisely the opposite of the broad, generous spray one instinctively tends to use.
One final point: switch off the pump during treatment if your setup allows it, and wait until everything is dry before restarting it. An isolated splash in a large volume is rarely catastrophic, but the habit of spraying over tanks will always end up costing you fish.