You must apply to the Spanish consulate that is competent for your place of residence. It is not clear from your question where that is.
If you actually live and reside in India and are just on a short visit to China, you will not be able to get a visa from a Spanish consulate in China, but will have to apply at the consulate competent for your place of residence in India.
If you live and reside in China (being in possession of an Indian residence permit then becomes irrelevant), you must apply at the consulate competent for your place of residence in China.
The Schengen visa code provides some exceptions to this general rule, but the consulates are usually very strict in their interpretation.