How do I stop explaining situations

I get defensive when my boyfriend points out things I've said/done that has hurt him.

The problem is that in the heat of the moment, I definitely don't see it as getting defensive and more as like: I thought we were on the same page and now im confused so I'll try to explain.

But this is bad, and I'm aware of it. But how do I stop? I feel like if I don't explain what went thought my mind then and there ill just seem like an asshole that said that thing just to say it you know?

I feel like explaining feels more right then just apologizing and then leaving it at that as he wants me to do