Spain replaced the nation's four top military officers today in a surprise move that appeared to be aimed at strengthening discipline within the armed forces and at silencing hawkish critics within the politically conservative officer corps.

In what represented the most radical shake-up of the top command structure in post-Franco Spain, the government last night requested the resignations of the chairman of the Joint Chiefs of Staff and the chiefs of staff of the Army, Air Force and Navy.

A Defense Ministry communique said they were all too close to the statutory retirement age and that new men were needed to provide continuity at a time when defense policy was being rethought in the light of negotiations to join NATO.

Well-informed sources close to the military stressed that the moral authority of the previous senior officers had been undermined gravely by sharp divisions within the officer corps stemming from last February's attempted military coup.

The new president of the Joint Chiefs of Staff, appointed after a Cabinet meeting today, was Gen. Alvaro Lacalle, the commander of the Valladolid command in north-central Spain. The sources said he wields influence within the military and is respected by politicians.

With two years of active service still due him before retirement, Lacalle, 63, can be expected to retain his top command post until after the spring 1983 general elections, when the opposition Socialist Party could, according to current voting trends, win a parliamentary majority.

In the shorter term, Lacalle will be heading the military during both the NATO-entry negotiations and the forthcoming court-martial of the February coup rebels scheduled for the middle of next month.

The outgoing general staff team, headed by Air Force Gen. Ignacio Alfaro and with Gen. Jose Gabeiras in the sensitive post of Army chief of staff, remained loyal to King Juan Carlos and the democratic order at the time of the coup attempt. As a result, they were criticized in extreme-right periodicals.

Such criticism reflected the feeling of hard-line sectors of the Army. Last month this spilled into the open when 100 junior officers released a statement to the press that was full of praise of the putsch plotters and was critical of the government.

Gens. Alfaro and Gabeiras arrested the junior officers who signed the statement, thus increasing divisions and tension within the officer corps. The uneasy situation prompted calls for unity, discipline and respect for the constitution by King Juan Carlos at a military ceremony last week.

The sources close to the military said the government had gambled by provoking the shake-up and depending on Lacalle to restore morale.

Civilians note that Lacalle has unusual experience, for a Spanish officer, of public administration and served as a director general in the Treasury during the 1960s. Since democracy was restored after Franco's death, Lacalle built up contacts with the Socialist opposition, where he is held to be a "safe" and "constitutionalist" officer.