2 Now to this Psalm is prefixed a title arising out of an historical event; but before the event is described we are instructed as to the scope, time and application of the incidents underlying it. First we have: For the end of the meaning of that David. Then there follows: When the Ziphims came and said to Saul: behold, is not David hid with us? Thus David's betrayal by the Ziphims awaits for its interpretation the end. This shows that what was actually being done to David contained a type of something yet to come; an innocent man is harassed by railing, a prophet is mocked by reviling words, one approved by God is demanded for execution, a king is betrayed to his foe. So the Lord was betrayed to Herod and Pilate by those very men in whose hands He ought to have been safe. The Psalm then awaits the end for its interpretation, and finds its meaning in the true David, in Whom is the end of the Law, that David who holds the keys and opens with them the gate of knowledge, in fulfilling the things foretold of Him by David.
Source: Homilies on the Psalms (New Advent)