javassist 动态生成WebService

前言  

前面讲过一个简单的class文件怎么用javassist去生成详见 javassist动态生成class

运用  方面

 在项目中,我们有时候会用到一些webservice 但是由于这些webservice不是在WEB项目初始化的时候就需要被建立,而是在某种特定的条件被触发时候才去生成。为了减少资源的损耗,我们就要去让其动态生成并且生成完之后就销毁,此时就要用javassist去动态生成webservice了。

实例

/***
	 * 
	 * 动态生成Webservice
	 * @throws CannotCompileException 
	 * @throws IOException 
	
	 */
	public static Class dynamicCreateWebservice() 
			throws CannotCompileException, IOException {
		ClassPool classpool = ClassPool.getDefault();
		//类
		CtClass ctclass= classpool.makeClass("com.bsoft.DynamicHelloWordWebservice");
		//添加方法
		CtMethod ctMethod = CtMethod.make("public String invokeHello(String paramString){" +
		" System.out.print($1);"+
				"\n	return \" hello,\"+$1;"+"\n"+
				"}", ctclass);
		ctclass.addMethod(ctMethod);
		/**
		 * Returns a class file for this class.
		 */
		ClassFile cf = ctclass.getClassFile();
		ConstPool constPool=cf.getConstPool();
		//添加类的注释
		/**
		 *  * 类注解和方法注解生成流程:

		     1、 创建注解Annotation;
		
		     2、 注解队列AnnotationsAttribute添加注解Annotation;
		
		     3、 类ClassFile或方法信息CtMethod.getMethodInfo()添加注解队列AnnotationsAttribute。
			@WebService(name="dynamicCreateWebservice")
				public class DynamicHelloWordWebservice
		 */
		Annotation classAttr = new Annotation("javax.jws.WebService", constPool);
		classAttr.addMemberValue("name", new StringMemberValue("dynamicCreateWebservice",constPool));
		classAttr.addMemberValue("targetNamespace", new StringMemberValue("com.bsoft.com", constPool));
		AnnotationsAttribute classAttrBute = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
		classAttrBute.addAnnotation(classAttr);
		cf.addAttribute(classAttrBute);
		
		/***
		 * 添加方法注解
		 * *  * 类注解和方法注解生成流程:

		     1、 创建注解Annotation;
		
		     2、 注解队列AnnotationsAttribute添加注解Annotation;
		
		     3、 类ClassFile或方法信息CtMethod.getMethodInfo()添加注解队列AnnotationsAttribute。

		 * 	生成的格式: @WebMethod(operationName="invokeHello")
					  @WebResult(name="result")
					  public String invokeHello
		 */
		Annotation methodAttr = new Annotation("javax.jws.WebMethod", constPool);
		methodAttr.addMemberValue("operationName", new StringMemberValue("invokeHello", constPool));
		AnnotationsAttribute methodAnnoAttrBute = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
		methodAnnoAttrBute.addAnnotation(methodAttr);
		
		
		Annotation resultAttr = new Annotation("javax.jws.WebResult", constPool);
		resultAttr.addMemberValue("name", new StringMemberValue("result", constPool));
		methodAnnoAttrBute.addAnnotation(resultAttr);
		
		ctMethod.getMethodInfo().addAttribute(methodAnnoAttrBute);
		
		/***
		 *   参数注解生成流程:

     1、 创建注解二维数组Annotation[][]:第一维对应参数序列,第二维对应注解序列;

     2、 参数注解属性ParameterAnnotationsAttribute添加注解二维数组Annotation[][];

     3、 方法信息CtMethod.getMethodInfo()添加参数注解属性ParameterAnnotationsAttribute。
		 */
		
		Annotation[][] paramAnnotanArr = new  Annotation[1][1];
		Annotation paramAnno = new Annotation("javax.jws.WebParam", constPool);
		paramAnno.addMemberValue("name", new StringMemberValue("name",constPool));
		paramAnnotanArr[0][0]=paramAnno;
		ParameterAnnotationsAttribute paramAnnoTan = new ParameterAnnotationsAttribute(constPool, ParameterAnnotationsAttribute.visibleTag);
		paramAnnoTan.setAnnotations(paramAnnotanArr);
		ctMethod.getMethodInfo().addAttribute(paramAnnoTan);
		
		byte[] byter = ctclass.toBytecode();
		FileOutputStream fos = new FileOutputStream(new File("d:\\DynamicHelloWordWebservice.class"));
		fos.write(byter);
		fos.close();
		return ctclass.toClass();
	}

查看



若想修改方法,可以调用ctMethod.setBody方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章