WASHINGTON -- President Obama promised graduating midshipmen at the U.S. Naval Academy on Friday that, as their commander in chief, he will only send them "into harm's way when it is absolutely necessary."

In his first address to military graduates, Obama also pledged to invest in the men and women who defend America's liberty, not just in the weapons they take with them into battle.

"I will only send you into harm's way when it is absolutely necessary, and with the strategy, the well-defined goals, the equipment and the support that you need to get the job done," the president told more than 1,000 graduates during a sun-splashed ceremony at Navy-Marine Corps Memorial Stadium on the shores of the Chesapeake Bay.

Obama said he has halted reductions in the Navy, is building up the Marine Corps and investing in the hardware -- combat ships, submarines and fighter aircraft -- they'll need to do their jobs. He also promised higher pay, enhanced child care and improved support and other benefits.

"In short, we will maintain America's military dominance and keep you the finest fighting force the world has ever seen," Obama said, as more than 30,000 watched from the stands.

The ceremony included the son of Obama's presidential rival, Sen. John McCain, who was among the proud parents. Had the Arizona Republican and Naval Academy graduate defeated Obama, McCain could have been the speaker.

Obama did not recognize McCain in his speech. But he did so before leaving the White House, praising the senator as he signed into law legislation giving the Pentagon new power to curtail wasteful defense spending. McCain was a sponsor of the bill.

"Senator McCain couldn't be here today because he's making sure he has a good seat to watch his son graduate from the Naval Academy in a few hours, and that's where I'm headed as soon as I catch my ride over here," Obama said at the bill signing in the Rose Garden.

Presidents typically deliver the commencement address at one of the service academies each year. Friday's speech was the third graduation address by Obama in the past nine days. He used the previous two to tackle issues that threatened to overshadow both events.

At the University of Notre Dame last Sunday, abortion opponents protested the president's appearance because he supports abortion rights. Obama jumped into the debate, telling graduates of the Roman Catholic university that people on both sides of the abortion issue must stop demonizing one another.

At Arizona State University, where Obama spoke on May 13, the issue was the school's decision not to award him an honorary degree on grounds that he hadn't accomplished enough. Obama said he agreed, saying no one's body of work is ever complete.

On Thursday, Obama delivered a different kind of speech, one in which he sought to regain control of the emotional debate over closing the detention center for suspected terrorists in Cuba. He denounced "fear-mongering" by political opponents and insisted that maximum-security prisons on the U.S. mainland can safely house the dangerous detainees he wants transferred out of Guantanamo Bay.

Former Vice President Dick Cheney countered the same day with a speech denouncing some of Obama's actions as "unwise in the extreme" and repeating his contention that the new president is endangering the country by turning aside Bush-era policies.