Suppose the above system moves with a velocity $v$ and collides into a wall.
The spring compresses and stores the energy, so energy is conserved.
However during the collision the spring compresses.
How is momentum conserved as during the compression the speed of the block reduces?