Watch the Strategy 9: Compose Yourself online guitar lesson by Jon Herington from Ear IQ: Soloing Strategies
If I had to reduce all my teaching over the years to the best single nugget of advice I've had to give, it would be this: If you want to learn to improvise better on a song, compose what you wish you could improvise; do it again, and again, and again...
There have been several cases of songs I've had to perform where I've tried to improvise over a particularly difficult section and found myself quite unsatisfied with the results. If I haven't had the time to figure out a successful approach, I've sometimes composed several passages of material that I wished I could have improvised. That way on the gig, at least I have a few options that I know will work, until I feel I have it together enough to risk going for it again.
Recording solos on tracks in the studio is probably what led me to this practice, which has really worked for me. When you can stop time, fix what you don't like, hone and polish it until you love it, you're teaching yourself a lot about what works in that musical context, and if you make a regular practice of that kind of thing, you'll find your ability to improvise and spontaneously create new stuff in real time will improve dramatically. So, I really recommend it.