Actually, casting a spell in melee range would not provoke an attack of opportunity. It's only when you leave it's melee range without taking the Disengage action that provokes it.