The characteristic of a spring is that the force needed to stretch or compress a spring by an amount x is equal to k x and is in the direction opposite to the extension or compression. Here k is called the spring constant.
Restoration Force of the spring (compression or tensile) Fs = - k x = - external force F
Work done by the external force in compressing the spring from x = 0 to x = L can be obtained by:
is the energy given by the external force to the spring with an extension x.
This energy is stored as potential energy in the spring.