Those are the moments with my own children that I thank God I took that Child Psych class in college! :)
Younger children hit, bit, and generally lash out because they don't possess the capacity to express anger or frustration in a verbal way. It is important to repeatedly (and I mean repeatedly, but then you know that since you have a 2 yo ;) reinforce that hitting is a bad choice and that they should use their words instead of their hands. Spanking reinforces just the opposite. It says "you hit so in so because you were angry and now I'm going to hit you because I'm angry."
I do the bookkeeping for my daughters preschool, and I fill in for the teachers from time to time. I have watched the teachers responses and it seems that a very firm and swift response works best. The director, who has been in the field for 13+ years responds to each incident with each child in the same way. She sits on the floor with both kids (the child who was hit usually in her lap) and explains that it is NEVER okay to hit her friends (the director refers to all of the kids as her friends) because it hurts them. She reinforces that hands are not for hitting and then asks the hitter to explain why they hit. She provides words to replace the hit and then suggests he/she apologize. She never forces the apology because that can cause more frustration and anger. Instead she says something like "is there something you could say to make my friend feel better?"
It's a slow learning process, but eventually the kids catch on that hitting is a bad idea.