President Trump paid homage to the military Friday in a speech to the graduating class at the U.S. Naval Academy in Annapolis, declaring to more than 1,000 incoming officers, “You are winners; you are warriors,” and invoking Navy legends to urge them on in their careers.

Trump, relishing the traditions of Annapolis, cited Adm. David Farragut’s famous cry and added his own punctuating twist for emphasis. “Damn the torpedoes — full speed ahead!” he said. “Boom.”

Over and over, the president voiced confidence in the nation’s military might, in a 34-minute address that hit rhetorical notes familiar from many of his other speeches. “In case you haven’t noticed, we have become a lot stronger lately,” he told the graduates. “A lot.”

At other points, he said: “We are witnessing the great reawakening of the American spirit and of American might. . . . Yes, they’re respecting us again. Yes, America is back. . . . Victory, winning — beautiful words. But that’s what it’s all about.”

The president praised the resolve of the nation’s armed forces and said there is nothing Americans can’t do when they stick together. “Our ancestors trounced an empire, tamed a continent and triumphed over the worst evils in history,” he said. He added: “We are not going to apologize for America. We are going to stand up for America.”

Trump pledged to support better services for veterans and fire those who fail to deliver. He boasted of higher spending on military equipment. “That means new ships — you like that,” he said, drawing laughter.

Trump drew applause and cheers from the crowd as he entered Navy-Marine Corps Memorial Stadium shortly before 10 a.m. and again as he took the stage.

He was given a 21-gun salute, and then the Navy’s famed Blue Angels flew overhead in formation.

There were 1,042 graduates in the class, 783 men and 259 women. More than 780 will be serving in the Navy and more than 230 in the Marine Corps. One received an interservice commission with the Air Force. A smattering of other graduates were from foreign countries or were not receiving military commissions.

Friday’s ceremony marked the first presidential appearance at a Naval Academy graduation since Barack Obama addressed the Class of 2013. Many presidents have addressed graduates since the academy’s founding in 1845.

The ceremonies took place under a blue sky with temperatures climbing into the 80s, at an athletic venue where hallowed events in Navy and Marine Corps history are commemorated in blue capital lettering on facades next to spectators: Midway, Okinawa, the Cuban missile crisis and more.

In their last hours as midshipmen, members of the graduating class gathered on a plaza overlooking the turf field, milling around a bronze statue of the Navy mascot, Bill the Goat. Known still as “firsties,” shorthand for first-class midshipmen, and clad in dress uniform, they were relishing the final moments before their commissioning as naval ensigns or Marine second lieutenants, and departure to assignments around the world.

Jane Kirkham, 22, of Rockville was one of 136 bound for service as ensigns with the submarine force. She said she felt “excitement, nostalgia, nervousness for the future.” She said she knows the new officers will be heading into unknown challenges at a moment of global tensions in the Middle East, the Korean Peninsula and elsewhere.

“I understand the kind of world we’re going into,” she said. “We’ll be carrying out our mission to protect the U.S. It’s our job.”

Kirkham, who majored in operations research, said she was drawn to the Naval Academy from the first time she visited, in seventh grade, during a soccer camp in Annapolis. She said it was a “huge honor” to have Trump at the graduation. “He’s our commander in chief,” she said. “That he’s taking time to shake everybody’s hand, we’ll remember for the rest of our lives.”

Other graduating midshipmen shared her assessment of the encounter with Trump. Whatever their views of the president, all said Friday’s occasion was decidedly nonpolitical.

“It’s a big deal,” said Kenneth Edmond, 21, of Suffolk, Va. “It’s like the CEO of your company coming to give you your diploma. It’s exciting.”

Like Kirkham, Edmond was bound for a submarine assignment, starting in South Carolina. He majored in political science, minored in Arabic and spent a semester abroad in Jordan. Well aware of conflicts and tensions in the Middle East and elsewhere, he said his military service at a fraught moment in international affairs “worries my relatives more than it worries me. I knew exactly what I was getting into. This is what I signed up for.”

At 9 a.m., the firsties walked in procession onto the field, hats in hand, and took their seats. Trump started speaking to them shortly after 10:30. By 11:17, they were commissioned officers.

Toward the end of his speech, Trump joked that he had been given a few options for how to handle the ceremonial duties. One was to leave quickly. Another was to stay a long while and shake everybody’s hand, one at a time.

“What should I do? What should I do?” he teased the crowd. “I’ll stay. I’ll stay.” And he did, shaking hands into the noon hour with a long line of ensigns and second lieutenants and flashing a broad smile.

He gave the last one in the queue, Marine 2nd Lt. Logan J. Zeuner, a hug.

Seung Min Kim contributed to this report.