דאמר רבי אבא בר כהנא אילמלא דוד לא עשה יואב מלחמה ואילמלא יואב לא עסק דוד בתורה דכתיב (שמואל ב ח, טו) ויהי דוד עושה משפט וצדקה לכל עמו ויואב בן צרויה על הצבא מה טעם דוד עשה משפט וצדקה לכל עמו משום דיואב על הצבא ומה טעם יואב על הצבא משום דדוד עושה משפט וצדקה לכל עמו
.... as Rabbi Abba bar Kahana says: Were it not for David, who studied Torah, Yoav would not have been able to wage war successfully, and were it not for the military acumen of Yoav, David would not have been able to study Torah. As it is written: “And David executed judgment and justice to all his people, and Yoav, son of Zeruiah, was over the army” (II Samuel 8:15–16). What is the reason that David “executed judgment and justice to all his people”? He was able to do so because “Yoav, son of Zeruiah, was over the army,” assisting him and fighting his battles. And what is the reason that “Yoav, son of Zeruiah, was over the army”? He was able to do so because “David executed judgment and justice to all his people.”
[Sanhedrin 49a]