Bear spray won't stop a predatory bear. Then again, either will a couple rushed shots from a hand gun.
Bear spray is effective TEMPORARILY. It will ruin the bear's sense of smell which generally causes them to break off the attack as that is their primary sense.
However it wears off and if the bear was actually predatory it will be back on your trail in ten or fifteen minutes. Unless that is enough time to get to your car, you need plan B, since that isn't long enough to get far away enough for them not to stalk you again.
A gun is effective two ways. If the bear is stalking around firing it into the ground makes a really loud noise, one the bear may recognize BTW, and the bear will usually then run off.
If it doesn't run off you do still have a plan B, particularly with a semi auto.
I have scared bears off by firing. Have never had to shoot one. I would rather fire a warning shot to scare off a bear then spray one. Proactively spraying one might provoke an attack, you get this unpleasant grey area where you don't know whether to spray. With a gun the loud noise of a warning shot is less likely to provoke.

