Jbriggs444 has the situation I am describing, the spring does have mass and is anchored. All I am saying is I think that the force the spring applies cannot be equal to kx when it is accelerating or experiencing a net force. This is my thinking. If you were to compress the spring an infintesimal amount, dx, you would still have to apply a nonzero force. Sure, the force does not have to be constant, it can gradually increase. But you cannot possibly have any compression when the force is zero. That means that at some time interval the force must be nonzero while x=0. This means that the force of the spring is only kx when it is not accelerating.