The subtitle informs us that this song was composed for the dedication of the temple, a temple David never built, but for which he prepared by storing materials, etc. So David composed the song in anticipation of the temple's completion under his son Solomon.
The song never actually mentions the temple, but rather reads almost like an epitaph for David's entire life - a memorial to what the Lord had done to bring him to the throne of Israel and to preserve him during his shepherding of God's people until the end of his life and reign.
If indeed it is such a dedication of the temple as well as an epitaph psalm, then we can assume a great deal of faith on David's part in his preparing ahead of time for the temple's building. He must have trusted his Lord to continue to preserve him to the end of his life and to enable Solomon to do the actual building. We can also conclude that the epitaph David wanted for his life was one of praise to Jehovah.
When we're looking at this song we need to remember once again that anything that was true for David was true for God's people Israel as well, since they were recipients of God's blessings through David. They were given victory over their foes through David, and so they could also sing with him, "I will extol You, O Lord, for You have lifted me up, and have not let my foes rejoice over me!" There would have been many, many times when they should all sing, "I cried out to You, and You healed me. O Lord, You brought my soul up from the grave; You have kept me alive that I should not go down to the pit."
Always they could sing, "Sing praise to the Lord, you saints of His, and give thanks at the remembrance of His holy Name. For His anger is but for a moment, His favor is for life; weeping may endure for a night, but joy comes in the morning." They would have learned, through David's reign, to wait on the Lord through thick and thin.
There would undoubtedly have been times, during prosperity, when David - and they - would have boasted that they could stand strong through anything, but when, unbeknown to them, it was not they themselves who were strong, but it was the Lord Who kept them. Then, when He "hid His face" they would indeed be "troubled." Then, having fallen into trouble, they would have cried out to their Lord in the depths of their trouble, "Hear, O Lord, and have mercy on me; oh Lord be my helper!"
And the Lord, times without number, answered them. Time and time again, they could sing, "You have turned for me my mourning into dancing; You have put off my sackcloth and clothed me with gladness!"
The result of such experience, both for Israel and for David their king, was that they could sing, "O Lord my God, I will give thanks to You forever!" In other words, they could learn from God's ever-present help, throughout many, many years, that they could trust Him ALWAYS, even on into eternity!
And now? David's greater Son, our Lord Jesus Christ is King, and He HAS always upheld us, and, through Him, we WILL always be blessed. So - will you trust Him through times of darkness and trouble? Will you trust that, though "His anger is but for a moment, His favor is for life?" Will you remember that "weeping may endure for a night, but joy comes in the morning?" If you can thus trust Him, then, at the end of your life, this will be your epitaph of praise as well as David's!