The allusion comes from The Biography of Mencius’ Mother Biographies of Outstanding Women by Liu Xiang, one of the greatest letters in the Han Dynasty.
When Meng Ke, known later on as Mencius, was still a child, his mother and him lived near a graveyard. As Meng Ke played in the yard, what he imitated were all the rites of funerals. So his mother said:” This is not the right place for me and my son to live.” She moved to a house near a market. Then her son took pleasure in imitating the peddlers’ hawking and salesmen’s bargaining. Again the mother said:” This is not the place me and my son to live.” And she made another migration and housed themselves near a school. There her son behaved in the same polite manner as the teachers and students. The mother said:” Here we should live!” So they settled there. In the following years, Meng Ke acquired a wide range of knowledge, such as etiquette, music, archery, charioteer, reading, writing and arithmetic. When he grew up, he became one of the most famous scholars and representatives of Confucianism.
Later, the phrase was used to sing praises of mothers who teach their children by means of selecting good environment.