David was a man after Gods heart . When he was young he had to deal with a lot of things as a Shepard that trained him to be a warrior in the flesh. I also think all that alone time gave him time to worship God and hear Gods voice and fear God. He also knew God was his source. He even fell a time or two but he knew that he had to repent and get back in the favor of God. A good warrior knows where his strength come from.