设计一个对接系统,通过动态模型的增删改触发业务系统相应服务的调用。模型增删改方法动态发布为WebService服务。WebService服务采用CXF发布,动态类生成采用Javassist。由于WebService服务类需要添加WebService相关注解,而国内关于Javassist生成包含注解的动态类介绍少之又少,于是花费一下午研究Javassist接口,终于让我找到了办法。

类注解和方法注解生成流程:

1、 创建注解Annotation;

2、 注解队列AnnotationsAttribute添加注解Annotation;

3、 类ClassFile或方法信息CtMethod.getMethodInfo()添加注解队列AnnotationsAttribute。

参数注解生成流程:

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

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

3、 方法信息CtMethod.getMethodInfo()添加参数注解属性ParameterAnnotationsAttribute。

一、动态WebService服务生成类。

package com.coshaho.learn.javassist;

import java.io.File;
import java.io.FileOutputStream; import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.Modifier;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.ClassFile;
import javassist.bytecode.ConstPool;
import javassist.bytecode.ParameterAnnotationsAttribute;
import javassist.bytecode.annotation.Annotation;
import javassist.bytecode.annotation.StringMemberValue; public class DynamicWebserviceGenerator
{
public Class<?> createDynamicClazz() throws Exception
{
ClassPool pool = ClassPool.getDefault(); // 创建类
CtClass cc = pool.makeClass("com.coshaho.learn.DynamicHelloWorld"); // 创建方法
CtClass ccStringType = pool.get("java.lang.String");
// 参数: 1:返回类型 2:方法名称 3:传入参数类型 4:所属类CtClass
CtMethod ctMethod=new CtMethod(ccStringType,"sayHello",new CtClass[]{ccStringType},cc);
ctMethod.setModifiers(Modifier.PUBLIC);
StringBuffer body=new StringBuffer();
body.append("{");
body.append("\n System.out.println($1);");
body.append("\n return \"Hello, \" + $1;");
body.append("\n}");
ctMethod.setBody(body.toString());
cc.addMethod(ctMethod); ClassFile ccFile = cc.getClassFile();
ConstPool constPool = ccFile.getConstPool(); // 添加类注解
AnnotationsAttribute bodyAttr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
Annotation bodyAnnot = new Annotation("javax.jws.WebService", constPool);
bodyAnnot.addMemberValue("name", new StringMemberValue("HelloWoldService", constPool));
bodyAttr.addAnnotation(bodyAnnot); ccFile.addAttribute(bodyAttr); // 添加方法注解
AnnotationsAttribute methodAttr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
Annotation methodAnnot = new Annotation("javax.jws.WebMethod", constPool);
methodAnnot.addMemberValue("operationName", new StringMemberValue("sayHelloWorld", constPool));
methodAttr.addAnnotation(methodAnnot); Annotation resultAnnot = new Annotation("javax.jws.WebResult", constPool);
resultAnnot.addMemberValue("name", new StringMemberValue("result", constPool));
methodAttr.addAnnotation(resultAnnot); ctMethod.getMethodInfo().addAttribute(methodAttr); // 添加参数注解
ParameterAnnotationsAttribute parameterAtrribute = new ParameterAnnotationsAttribute(
constPool, ParameterAnnotationsAttribute.visibleTag);
Annotation paramAnnot = new Annotation("javax.jws.WebParam", constPool);
paramAnnot.addMemberValue("name", new StringMemberValue("name",constPool));
Annotation[][] paramArrays = new Annotation[1][1];
paramArrays[0][0] = paramAnnot;
parameterAtrribute.setAnnotations(paramArrays); ctMethod.getMethodInfo().addAttribute(parameterAtrribute); //把生成的class文件写入文件
byte[] byteArr = cc.toBytecode();
FileOutputStream fos = new FileOutputStream(new File("D://DynamicHelloWorld.class"));
fos.write(byteArr);
fos.close(); return cc.toClass();
}
}

二、动态WebService服务发布。

package com.coshaho.learn.javassist;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean; public class DynamicWebServiceServer
{
public static void main(String[] args) throws Exception
{
DynamicWebserviceGenerator javassistLearn = new DynamicWebserviceGenerator();
Class<?> webservice = javassistLearn.createDynamicClazz(); JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean(); // Web服务的地址
factoryBean.setAddress("http://localhost:8081/dynamicHello"); // Web服务对象调用接口
factoryBean.setServiceClass(webservice);
Server server = factoryBean.create();
server.start();
}
}

三、SoapUI测试。

注:反编译查看Javassist生成的动态类。

最新文章

  1. ckeditor使用
  2. performSelector的原理以及用法
  3. Jquery 之 使用选择器
  4. coocs2d-x资源压缩笔记
  5. Canvas 和 SVG 都允许您在浏览器中创建图形,但是它们在根本上是不同的
  6. 由window.history.back()引发的问题
  7. [转]C#如何在ListView失去焦点的情况下仍然保持Item高亮
  8. 快速构建ASP.NET MVC Admin主页
  9. Jquery 源码学习
  10. visual studio for mac在线安装网络错误
  11. 两种实现方式mycat多租户,枚举分片,注解拦截
  12. wireshark抓包看ECN
  13. TensorFlow实验环境搭建
  14. 剑指Offer-- 二叉搜索树中和为某一值的路径
  15. express框架以及配置项
  16. mysql中 where in 用法详解
  17. Python 模块EasyGui详细介绍
  18. memcache分布式布置方案
  19. VMware Player 12.5.0 中文免费版
  20. Exception in thread &quot;main&quot; java.lang.UnsatisfiedLinkError:

热门文章

  1. linux系统中关于shell变量$*与$@的区别
  2. 使用shell数据处理数据实例①-------手把手教学版
  3. HUST 1605 Gene recombination(广搜,位运算)
  4. hadoop(角色)各个组件配置信息
  5. Guideline 2.1 - Information Needed
  6. MongoDB的数据恢复
  7. 正则表达式(二):Unicode诸问题上篇(转)
  8. servlet输出请求头
  9. mysql 约束条件目录
  10. vue学习之npm