“A good man out of the good treasure of the heart bringeth forth good things: and an evil man out of the evil treasure bringeth forth evil things.” - Matthew 12:35.
As David was fleeing from Absalom, Shimei, met him on the way and cursed him as he came, and cast stones at him and at all the servants of king David, saying, “Come out, come out, thou bloody man, and thou man of Belial: The LORD hath returned upon thee all the blood of the house of Saul, in whose stead thou hast reigned; and the LORD hath delivered the kingdom into the hand of Absalom thy son: and, behold, thou art taken in thy mischief, because thou art a bloody man.” The insult was too much for David generals and they wanted to kill Shimei but David stopped them saying among other things, “... Behold, my son, which came forth of my bowels, seeketh my life: how much more now may this Benjamite do it? It may be that the LORD will look on mine affliction, and that the LORD will requite me good for his cursing this day.” - 2 Samuel 16:5-14.
Evidently, Shimei did what he did because he thought the attempted overthrow that was the end of David. When the tide turned and David was restored to the throne, the same man came back to beg for forgiveness. David spared his life. This teaches us not to rejoice over someone's misfortune, particularly a man of God. (Proverbs 24:17,18) We should also not talk ill of people just because they are down. If not for King David's godly disposition, Shimei would have been killed for his wicked action.