Jacques — the "melancholy" lord in Duke Senior's forest court — delivers the speech in Act 2, Scene 7 (not Act 2, Scene 2).
All the world's a stage,
And all the men and women merely players;
They have their exits and their entrances,
And one man in his time plays many parts,
His acts being seven ages...
The speech is not Shakespeare's autobiography — it's a character's cynical worldview, later complicated by the play's comic resolutions.