javassist 动态生成WebService
来源:互联网 发布:淘宝网店开店流程2017 编辑:程序博客网 时间:2024/05/29 16:34
前言
前面讲过一个简单的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);"+"\nreturn \" 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方法
相关连接:Javassist操作方法总结
阅读全文
1 0
- javassist 动态生成WebService
- Javassist 动态生成类
- javassist动态生成class
- 动态生成webservice
- CXF生成webservice动态客户端
- JAVAssist---动态修改注解
- JAVAssist---动态修改注解
- Javassist实现动态代理
- 利用Java反射机制和Javassist实现Java对象及其属性的动态创建生成
- 利用Java反射机制和Javassist实现Java对象及其属性的动态创建生成
- 部署WebService生成DLL动态库步骤
- C# 动态生成WebService 的引用
- 使用javassist生成新类
- javassist生成的对象回收
- 使用javassist生成新类
- 使用javassist生成实体对象
- 使用javassist动态注入代码
- 使用javassist动态注入代码
- Java IO
- ssl和tsl区别
- Mac/Windows下,Python3安装Scrapy模块
- PHP 开发环境搭建
- 一个测试工程师的职业生涯感悟
- javassist 动态生成WebService
- codeM题目-寻找最小difference
- MySQL删除列无响应问题
- [转]Java 枚举7常见种用法
- pptp iptables snat记录
- PHP 压缩文件夹
- compile vim with python
- C++搜索与回溯算法之置棋问题
- make[1]: warning: Clock skew detected. Your build may be incomplete.