Sean Lawless (41) went to grab his driving licence off Garda Paul Doona, and then pushed the door of the patrol car back, preventing the garda from getting out.
Gda Doona had initially stopped Lawless for driving too fast down a slip road, and then moving from lane to lane without indicating.
Judge Deirdre Gearty ordered Lawless to donate €1,000 to the Irish Kidney Association and said she would strike out the charges.
Lawless, of Royal Canal Way, Ashtown, Dublin 15, admitted assaulting Garda Paul Doona and a breach of the peace at Finglas Road, Dublin 11 on November 17, 2019.
Gda Doona told Blanchardstown District Court that he was on mobile patrol when a car drove down the slip road at excessive speed. The car then drove between lanes one and two without indicating.
Gda Doona said he spoke to the driver about his erratic driving, and asked for his driving licence to check it.
Gda Doona went back to the patrol car to check the defendant's details. Lawless banged on the car window, asked for the driving licence back, and then became aggressive and went to grab it.
When Gda Doona tried to open the patrol car door, Lawless pushed the door back and he was unable to get out.
Gda Doona said he spoke to Lawless, but he did not reply in a mannerly way, and told the garda he was a public servant and to hurry up.
The court heard Lawless was arrested and placed in a patrol car.
Gda Doona said he was reversing into a spot at Finglas garda station when he heard Lawless try to build up phlegm in his mouth.
He then spat and the garda felt the phlegm on the back of his neck.
Defence lawyer Ciaran MacLoughlin said Gda Doona was only doing his job, and Lawless wished to sincerely apologise for his behaviour, which was completely unacceptable.
In a victim impact statement, Gda Doona said he was "shocked and stunned" a man of the defendant's age would behave in such a manner.
Mr MacLoughlin said Lawless worked for a homeless charity and a conviction would impact on his future career.