To live and work in Spain, non EU citizens whom do not have the rights of free movement and residence in the EU, must apply for a residence and work visa. There are various types of work visas.
If you wish to start your own business in Spain you can apply for the self employment visa (por cuenta propia) or you can apply for the employment work visa (por cuenta ajena).
There is also the entrepreneur visa and the highly skilled visa.
However, you must apply for a visa before moving to Spain.