Springs come to their equilibrium condition after being deformed. One limitation for this is the force applied should be small enough.
A box is attached to one end of a spring in Figure 1 and the position of the box is at the origin of our coordinate system without any force being applied. Now an external net force is applied on the box towards positive x-direction. And let the magnitude of the force applied on the spring is \(F\).
The force applied stretches the spring a distance \(x\) at a particular point and it is found that the force on the spring is directly proportional to the elongation \(x\) of the spring also called Hooke's law, that is, \(F \propto x\) and
\[F = kx \tag{1} \label{1}\]
In \eqref{1}, \(k\) is the proportionality constant. Note that the Eq. \eqref{1} is ture for small elongation \(x\) or the force is small enough so that the value of \(k = \frac{F}{x}\) remains constant. We introduced the direct proportionality of the force on the spring and the elongation as Hooke's law but you should also consider the limitation of the validity of this law which is "the force applied should be small enough".
The force exerted by the spring is equal and opposite to the force applied on the spring according to Newton's third law. The same magnitude of force should be applied on the spring on both of its ends to stretch it. You saw the direct proportionality of the force and the elongation in Eq. \eqref{1} and know that the force is not constant during the displacement or elongation.
If you suppose an infinitesimally small elongation \(dx\) towards positive x-axis, the force during this elongation or displacement can be taken as constant and we can use the definition of work done \(dW\) to find the work done for the small displacement which is,
\[dW = Fdx\]
Now the total work done within the limits \(x_1\) and \(x_2\) is obtained by integrating the above expression from \(x_1\) to \(x_2\).
\[W_{\text{force}} = \int\limits_{{x_1}}^{{x_2}} {Fdx} = \int\limits_{{x_1}}^{{x_2}} {k{\kern 1pt} xdx} = \frac{1}{2}kx_2^2 - \frac{1}{2}kx_1^2 \tag{2} \label{2}\]
The Eq. \eqref{2} is the work done on the spring by the force of magnitude \(F\), so the work done by the spring is the negative of the work done in Eq. \eqref{2} which is
\[{W_{{\rm{spring}}}} = \frac{1}{2}kx_1^2 - \frac{1}{2}kx_2^2 \tag{3} \label{3}\]
The work done by a conservative force is the negative of the change in potential energy that is, \(W = {U_1} - {U_2} = - ({U_2} - {U_1}) = - \Delta U\). If you compare Eq. \eqref{3} with this, you'll find the elastic potential energy due to the spring force at a particular elongation to be
\[U = \frac{1}{2}kx_{}^2 \tag{4} \label{4}\]
If a stretched spring does work on a block and the block moves in the direction of the force, the work done should be positive and this requires that the initial potential energy should be greater than the final potential energy which means \(x_1\) should be greater than \(x_2\) in Eq. \eqref{3}.