Ab initio calculations of gj factors for Li, Be+, and Ba+