Sure, you can be a legal resident in Spain and a tax resident elsewhere. You can potentially spend the majority of the year in Spain, even. It is possible, yes, but it is not easy. Definitely not recommended.
Assuming that you plan to spend the majority of the year in Spain, the country where...