Concept of work done by spring While deriving work done by spring force, our main problem is we cannot take out ${\bf F}$ vector out of the integral because it is not constant.
Hence we adopt the following method-
Considering a situation where one end of the spring is attached to a fixed vertical support and the other end to a block which can move on a horizontal smooth table, we write work done during a small interval in which the block moves from $x$ to $x+dx$ (the origin is at the position of the block when the spring is at it's natural length). The force in this interval is $kx$ and the displacement is $dx$. The force and displacement are opposite in direction. 
So now to find the work done all we have to do is integrate $(-kx dx)$ with the limits we want to define. The negative arises since the $d{\bf r}$ vector and ${\bf F}$ vector are opposite in direction.
Now  on performing the integral on the limits say $x_1$ to $x_2$ we get,
$$
W = \frac{1}{2}k(x_1^2 - x_2^2) 
$$
Now here comes my problem. If we want to find work done by the spring when it is being brought back to it's normal length from it's maximum elongation (say $x$); on using the derived result we get work done by spring force  $W = (1/2)k(x^2)$. This makes complete sense as work done by spring force in this case is positive as displacement and force are in the same direction.
What I'm unable to understand is how did we get this correct result? In the beginning we considered a small interval when block went from $x$ to $x+dx$ and then we said $F$ is ALWAYS opposite to $dx$. How did that give us the correct answer to when $F$ is IN THE DIRECTION of $dx$. Even if we analyze when the spring is being brought back to it's normal length from it's maximum compression say $x$, work done by spring force = $1/2(k)(x^2)$. 
Can someone please help me out and answer why our initial step to start the derivation is giving the correct result when $F$ is in the direction of $dx$. What am I missing ? 
 A: I have extensively updated my answer because the OP has the same conceptual problem as many of us more often in the context of deriving the electric potential due to a point charge and the gravitational potential due to a point mass.
My answer might be long winded but I thought it necessary because it has cause so many problems in the past and, no doubt, will do so in the future.  

With the spring the change in elastic potential energy of a spring is equal to the work done by an external force whilst changing the length of the spring with the alternative definition the change in elastic potential energy of a spring is equal to the minus work done by the force exerted by the spring whilst changing the length of the spring.  
Things go well when considering the spring increasing in length to some extension $x$ from its natural length $x=0$ as an external force is applied the result being that the change in the elastic potential energy of the spring is $\frac 12 k x^2$ where $k$ is the spring constant.
As expected $\frac 12 k x^2$ is a positive quantity ie the elastic potential energy of the spring has increased.  
The OP has decided to look at the work done by spring when the length of the spring decreases and by using the fact that the force due to the spring is opposite in direction to the external force and the incremental displacement of the force $dx$ is such that it is also negative so that the dot product of the force and the incremental displacement is positive.
Doing the integration between the initial extension and the final extension leads to a negative value for the work done by the spring and hence an increase in the elastic potential energy of the spring which is obviously incorrect.  

Let me try and explain the error in the second derivation by looking at a much simpler example in which we have a “magic” spring which exerts a constant force $F$ when its length varies between $x_{\rm A}$ and $x_{\rm B}$ with $x_{\rm B} > x_{\rm A}$.  
To try and make it clearer as to what is happening I will also use a numerical example with the force being of magnitude $5\,\rm N$ and lengths $x_{\rm B}$ and $x_{\rm A}$ being $3\, \rm m$ and $1\, \rm m$ respectively.  
It is a one dimensional problem and I define a unit vector in the positive x-direction $\hat x$ and the external force acting on the spring is $\vec F_{\rm external} = F_{\rm external} \hat x = +5 \hat x$.
Note that $F_{\rm external}$ is the component of the force in the $\hat x$ direction.   
The position of the end of the spring changes from $\vec x_{\rm initial} = x_{\rm initial} \hat x$ to $\vec x_{\rm final} \hat x $ and the displacement of the end of the spring, which is the displacement of the external force, is $\Delta \vec x = \Delta x \hat x = \vec x_{\rm final} \hat x - \vec x_{\rm initial} \hat x$ which results in $\Delta x = x_{\rm final} - x_{\rm initial}$.
Again note that $ x_{\rm final}$ and $ x_{\rm initial}$ are components in the $\hat x$ direction.  
The work done by the external force is $\vec F_{\rm external} \cdot \Delta \vec x = F_{\rm external} \hat x\cdot \Delta x \hat x= F_{\rm external}\, \Delta x = F_{\rm external} (x_{\rm final} - x_{\rm initial})$.  
In the numerical example with the spring being stretched $F_{\rm external} = 5\, \rm N$, $x_{\rm initial} = 1 \, \rm m$ and $x_{\ rm final} = 3 \, \rm m$ so the work done by the external force is $5(3-1)= +10 \,\rm J$ and this is the increase in the elastic potential energy of the spring.
Note that we have evaluated $\displaystyle \int_ {x_{\rm initial}}^ {x_{\rm final}}F_{\rm external} \, dx$
But this formula also works when the length of the spring is decreasing.
This time you have $F_{\rm external} = 5\, \rm N$ (the external force direction is still the same), $x_{\rm initial} = 3 \, \rm m$ and $x_{\rm final} = 1 \, \rm m$ so the work done by the external force is $5(1-3)= -10 \,\rm J$ ie work is done on the external force and there is a decrease in the elastic potential energy of the spring.
Again we have evaluated $\displaystyle \int_ {x_{\rm initial}}^ {x_{\rm final}}F_{\rm external} \, dx$  

At the risk of repetition, I would like to show how the analysis used above works if one considers the work done by the force exerted by the spring.
In this case $\vec F_{\rm spring} = F_{\rm spring} \hat x$ where $F_{\rm spring}$ is the component of force due to the spring $\vec F_{\rm spring}$ in the $\hat x$ direction.  
The work done by the spring is $\vec F_{\rm spring} \cdot \Delta \vec x = F_{\rm spring}(x_{\rm final} - x_{\rm initial})$  
In the numerical example $\vec F_{\rm spring} = -5 \hat x$ and and the work done by the spring force stretching the spring comes out to be $-10\, \rm J$ and the work done by the spring force when the length of the spring decreases is $-10\, \rm J$ which is what one would expect.

Now I will try and explain what was wrong with the method used by the OP in the case where the spring decreased in length.
The force due to the springs was in effect written as $- F\hat x$ in the knowledge that the direction of the force due to the spring was in the opposite direction to $\hat x$.
This immediately means that $F$ is positive.  
The displacement was written as $–\Delta x \hat x$ on the assumption that the length of the spring would be decreasing.
If the minus sign is there what does that imply about $\Delta x$?
It must be positive!  
The OP evaluated $-F \hat x \cdot -\Delta x \hat x = F \Delta x = F (x_{\rm final} – x_{\rm initial})$ using this integral $\displaystyle \int_ {x_{\rm initial}}^ {x_{\rm final}}F \, dx$ with $ x_{\rm final} < x_{\rm initial}$.
Using this integral with a constant force (for simplicity) produces a value for $\Delta x= x_{\rm final} – x_{\rm initial}$ which is negative when the spring is contracting.  
What has been done is that by stating the incremental displacement as $-\Delta x \hat x$ then the implication is that $ x_{\rm final} > x_{\rm initial}$ and yet when the integral is set up $ x_{\rm final}$ was made smaller than $x_{\rm initial}$.  
The resolution of this contradiction is fairly straightforward.  
If you want to evaluate the correct integral when you use $-dx$ you must reverse the order of the limits which is equivalent to $\displaystyle \int_b^a f(x) (-dx) = \int_a^b f(x) dx$  

Update as a clarification asked for by the OP 
Still with $\hat x$ defining a direction I want to show that the work done is still $$\vec F \cdot \Delta\vec x = F \hat x \cdot \Delta x \hat x = F\Delta x = F (x_{\rm final} – x_{\rm initial})$$  
This time considering the work done by the spring when it is compressed from $\vec x_{\rm initial} = -1 \hat x$ to $\vec x_{\rm final}=-3 \hat x$ with the spring exerting a force $\vec F_{\rm spring} = + 5 \hat x$  
Work done by the spring
$$F\Delta x = F_{\rm {spring}} (x_{\rm final} – x_{\rm initial}) = = (+5)(-3-(-1)) = -10$$  
The minus sign means that work has been done on the spring ie the elastic potential of the spring has increased.  
In terms of an integral we have 
$$\displaystyle \int ^{x_{\rm final}}_{x_{\rm initial}} F_{\rm spring} dx = \int ^{-3}_{-1} +5\, dx = -10$$  

Further update as a clarification asked for by the @user35508 
The work done by a spring is $\displaystyle \int ^{x_{\rm final}}_{x_{\rm initial}} \vec F_{\rm spring} \cdot d \vec x$ so what one needs is an expression for $\vec F_{\rm spring}$ in terms of the displacement of the end of the spring $\vec x = x \hat x$ and it is  
$$\vec F_{\rm spring}= -kx \hat x$$  
This is the correct form for the force exerted by the spring in that when the spring is stretched (x positive) it exerts a force in the $-\hat x$ direction and when the spring is compressed (x negative) it exerts a force in the $+\hat x$ direction.  
$\text{Work done by spring} = \displaystyle \int ^{x_{\rm final}}_{x_{\rm initial}} \vec F_{\rm spring} \cdot d \vec x = \int ^{x_{\rm final}}_{x_{\rm initial}} -kx\hat x \cdot dx \hat x = \int ^{x_{\rm final}}_{x_{\rm initial}} -kx \, dx = \frac 12 k\left (x_{\rm initial}^2 -x_{\rm final}^2\right )$  
For example if $x_{\rm initial} = -3$ and $x_{\rm final} = 0$ then this represents a compressed spring extending to its natural length and the work done by the spring is positive.

This was my original answer 
Let the spring be the system and there be an external variable force $F \hat x$ acting on the spring in the direction $\hat x$.
The magnitude of the external force is $kx$ where $x$ is the extension of the spring.
This external variable force undergoes a displacement $dx \hat x$ and the work done by the external force is $F \hat x \cdot dx \hat x = F \, dx = kx \, dx$ and this is the change in the elastic potential energy stored in the spring.  
Note that this equation is true when the spring is getting longer and also when the spring is getting shorter.
When the integration is done the limits of the integration define whether the spring is getting longer or shorter ie giving the correct sign to the incremental change in the length of the spring $dx$.
Work done by external force increasing the length of the spring from it natural length by $x$ is 
$$\int ^x_0 kx \, dx = + \frac 12 kx^2$$  
and this is the change in the elastic potential energy stored in the spring.
Now do the same again but have the spring start with extension $x$ and go back to its natural length.  
Work done by external force is now  
$$\int _x^0 kx \, dx = - \frac 12 kx^2$$
and this again is the change in the elastic potential energy stored in the spring.  
If you consider the work done by the force exerted by the spring $-kx \hat x$ then this amount of work is equal to minus the change in the elastic potential energy stored in the spring. 
A: you are forgetting "X" & "F" are vector quantaties so they have a magnitude too....if you solve the integral keeping this in mind and calculate the integral for calculating work done....your question is resolved
A: I believe you are confusing the relationship $\vec F$ has to $\vec x$ with $dx$. The spring force is given by Hooke's Law: $\vec F = -k \vec x$. It has no relationship to $dx$.For example:


*

*A 1-dimensional spring is stretched from $x_1$ to $x_2$ and $x_2 > x_1 > 0$. Then $W = \int_{x_1}^{x_2} F dx < 0$ because:


*

*$x > 0$  throughout the integral

*$F < 0$ throught the integral, since $F = - k x $

*$dx > 0$ as the upper limit is greater than the lower limit of integration. 


*A 1-dimensional spring is relaxed from $x_2$ to $x_1$ and $x_2 > x_1 > 0$. Then $W = \int_{x_2}^{x_1} F dx > 0$ because:


*

*$x > 0$  throughout the integral

*$F < 0$ throught the integral, since $F = - k x $

*$dx < 0$ as the lower limit is greater than the upper limit of integration. 



So you can see the integral flips sign because the limits switched; $F$ never changed direction, however, since it is totally related to the current value of $x$, not the direction of integration $dx$. 
