My method also uses dot product. If two vectors, A and B, have no y or z components, then it is easy to show that A.B = AxBx. The components, Ax and Bx, are scalar, but can be positive or negative.
Suggest you read my post carefully again, with this in mind. Or, if you prefer, use magnitudes (scalar and non-negative) of vectors, and put in the cosine value, +1 or -1, separately as you follow my argument. You'll find it gives the same result as my method each time. The argument shows that the more you deform the spring - either by extending it or compressing it - the more energy it stores. If you repeat the argument, changing the sign of dx, you'll find that if you reduce the deformation - either the extension or the compression - the spring stores less energy.
What I found confusing in your last post - and it may have some bearing on your own difficulty - is what you mean by 'displacement'. Are you using it to mean displacement of the mass from its equilibrium position (that's the usual meaning) or are you using it to mean the incremental displacement, dx?