JAVAssist---動態修改註解,javassist---註解
ITOOV3.0開始了,需要研究一些技術痛點,先來說一個我覺得比較有趣的技術點吧,需求是這樣的,我們需要動態切換資料來源,又由於我們是通過實體單元的注入來完成的,實體單元是通過註解的形式注入的,這樣如果我們想修改資料來源那麼必然就要動態修改註解(當然可能還有其他的解決方式,但是我覺得動態修改註解還是很有趣的)所以就從動態修改註解開始吧: 先來看看我們需要修改註解的代碼:
/** * EntityManager的執行個體化 * @author 陳麗娜 * @version 1.0.0 , 2015年3月30日 下午8:43:27 * @param <T> */public class CollectionBase<T> extends BaseEaoImpl<T> { /** * 注入實體單元 */ @PersistenceContext(unitName="collection-entity") protected EntityManager em; /**EntityManger * 執行個體化 */ @Override protected EntityManager getEntityManager() { return this.em; }}
需要修改的是unitName.
那麼該如何修改呢?開始是沒有任何思路的,但當時的我總有種感覺一定可以修改,所以就查了一下,發現了JAVAssist:開源的分析。編輯和建立java位元組碼的類庫。更深入的認識可以百度瞭解一下,下面一個小的demo來修改一下註解:
首先來看是如何獲得這個註解的:
@Testpublic void ReadTest() throws NotFoundException{ClassPool pool = ClassPool.getDefault();//擷取要修改的類的所有資訊CtClass ct = pool.get("com.tgb.itoo.collection.base.CollectionBase"); //擷取類中的方法CtMethod[] cms = ct.getDeclaredMethods(); //擷取第一個方法(因為只有一個方法) CtMethod cm = cms[0]; System.out.println("方法名稱====" + cm.getName()); //擷取方法資訊 MethodInfo methodInfo = cm.getMethodInfo(); //擷取類裡的em屬性 CtField cf = ct.getField("em"); //擷取屬性資訊 FieldInfo fieldInfo = cf.getFieldInfo(); System.out.println("屬性名稱===" + cf.getName()); //擷取註解屬性 AnnotationsAttribute attribute = (AnnotationsAttribute) fieldInfo.getAttribute(AnnotationsAttribute.visibleTag); System.out.println(attribute); //擷取註解 Annotation annotation = attribute.getAnnotation("javax.persistence.PersistenceContext"); System.out.println(annotation); //擷取註解的值 String text =((StringMemberValue) annotation.getMemberValue("unitName")).getValue() ; System.out.println("註解名稱===" + text); }
運行結果:
方法名稱====getEntityManager屬性名稱===em@javax.persistence.PersistenceContext(unitName="collection-entity")@javax.persistence.PersistenceContext(unitName="collection-entity")註解名稱===collection-entity
修改註解的方法與擷取的一樣,只是需要為擷取的註解賦值,代碼如下:
@Testpublic void UpdateTest() throws NotFoundException{ ClassPool pool = ClassPool.getDefault(); //擷取需要修改的類 CtClass ct = pool.get("com.tgb.itoo.collection.base.CollectionBase"); //擷取類裡的所有方法 CtMethod[] cms = ct.getDeclaredMethods(); CtMethod cm = cms[0]; System.out.println("方法名稱====" + cm.getName()); MethodInfo minInfo = cm.getMethodInfo(); //擷取類裡的em屬性 CtField cf = ct.getField("em"); FieldInfo fieldInfo = cf.getFieldInfo(); System.out.println("屬性名稱===" + cf.getName()); ConstPool cp = fieldInfo.getConstPool(); //擷取註解資訊 AnnotationsAttribute attribute2 = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag); Annotation annotation = new Annotation("javax.persistence.PersistenceContext", cp); //修改名稱為unitName的註解 annotation.addMemberValue("unitName", new StringMemberValue("basic-entity", cp)); attribute2.setAnnotation(annotation); minInfo.addAttribute(attribute2); //列印修改後方法 Annotation annotation2 = attribute2.getAnnotation("javax.persistence.PersistenceContext"); String text = ((StringMemberValue)annotation2.getMemberValue("unitName")).getValue(); System.out.println("修改後的註解名稱===" + text);}
運行結果:
方法名稱====getEntityManager屬性名稱===em修改後的註解名稱===basic-entity
多麼神奇的動態修改註解,運行時修改很多問題就迎刃而解了!