You have beset me and shut me in—behind and before,
and You have laid Your hand upon me.
In the beginning of this Psalm, in the verses that precede this one, David acknowledges that God has perfect knowledge of him as a man. God has searched him and knows everything about him -- his thoughts, all his ways, even his words before he speaks them. And then in this verse 5, David reverts to his warrior personality.
He uses terminology that he knows well. He describes God's knowledge of him in terms of battle. God has "beset" him and "shut him in". It's as though David [and we] are caught in an ambush, or besieged by an army which has totally encircled the city walls; we are surrounded by the Lord and His knowledge of us.
"Behind and before".... Behind us there is God recording our sins, but in grace blotting out the remembrance of them. And before us there is God knowing everything we will ever do, and providing for all our wants. We can't turn back and escape Him, because He is present in our past. We can't go forward and outrun him, because He will always be there in our future. He not only observes us, but He surrounds us.
And if you are foolish enough to think you can escape His presence, or that His act of surrounding you is in the distant future, David adds this truth .... "And You have laid Your hand upon me." Far from a judgmental or condemning hand, I see this hand of God as a protective hand. He is caressing His child and leading me, lifting me up, protecting me; restoring me. I see His hand actively involved in my growth as a Christian, in my walk with Him, in my failures, in my pain, and in my despair. Ultimately I see His hand shaping me to become someone who is acceptable to Him, and above all else, always loved by Him.
I think maybe David used these military terms because this life is a battle. We fight our own sinful nature, the arrows of the Adversary, and assaults from our culture and the world. But isn't it nice to know that our Lord encircles us and surrounds us? To know that He is behind us, covering our sins of the past, and going before us to prepare a defense against future attacks? And all the while, He has a protective hand on us, to reassure us that He is there; to remind us that He will never leave us. That's a battle plan for the Ages!