Energy efficiency is an extremely important performance indicator for device-to-device communications due to the limited battery capacity of user equipments (UEs). In this paper, we focus on the two-stage EE optimization problem, which consists of a channel and power allocation problem in the first stage, and a context-aware D2D peer recovery problem in the second stage. We incorporate a many-to-one matching framework to solve the nonconvex combinatorial problem with dynamically varying preferences. At each iteration of the first-stage match, D2D transmitters and cellular UEs are firstly matched by assuming that the preferences remain unchanged, which are used later to update the preferences. The second-stage matching problem between D2D transmitters and receivers can also be solved by the proposed algorithm with little modifications. We validate the proposed algorithm through numerical results and compare it with several heuristic algorithms.