After completing studies as a Cadet in the Infantry Corps School (later Academy) of Toledo, he was posted in 1865 to Cuba where he joined the garrison of Puerto Príncipe (Camagüey).
Widowed and with six children, Castellanos spent the following nine years in charge of the Campaign section of the War Ministry in Madrid.
Captain General of the Island Arsenio Martínez Campos called upon Castellanos for his immediate presence as a strategist with knowledge of the country, accumulated seniority and experience in war and Cuba’s circular campaigns.
With American intervention in the conflict, land battles in Santiago took place, as well as the sinking of the Cervera squadron in the bay, after which the Spanish surrendered.
On 1 January 1899, Castellanos officially ("in the name of his King") transferred the island to the United States: He embarked on the steamship Rabat to travel to Matanzas and Cienfuegos to complete the repatriation.