The Slovenian international has signed on at the San Siro in a deal that has seen Luca Antonini move in the opposite direction.
"After 23 years in Rossonero, today I salute the team I support. The time has come for me to go. Goodbye my Milan, I gave my heart for you. Thank you for everything," the departing full-back wrote on the social networking site Twitter.
Both players have agreed three-year contracts at their respective new clubs.