The bars in barred spiral galaxies are primarily thought to have formed through a process of slow evolution. This process involves the gradual redistribution of stars and gas within the galaxy, often influenced by the dynamics of the galaxy's rotation and the interaction between stars and gas. Over time, instabilities in the galactic disk can lead to the formation of a bar structure, as orbital resonances cause stars to orbit in a way that focuses mass into the central region, forming a bar-like shape.
Other mechanisms such as merging can also play a role in the evolution of some barred spirals, but the prevalent model for bar formation is the slow evolutionary processes driven by gravitational interactions and resonances.