设计一个对接系统,通过动态模型的增删改触发业务系统相应服务的调用。模型增删改方法动态发布为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生成的动态类。

Javassist注解(Annotation)的使用:CXF WebService动态生成的更多相关文章

  1. SOA 下实现分布式 调用 cxf+ webService +动态调用

    近期项目间隙 自学了  webservice   一下 是我写的  一个demo 首先我们在web.xml 里配置如下 <servlet> <servlet-name>CXFS ...

  2. CXF WebService整合SpringMVC的maven项目

    首先推荐博客:http://www.cnblogs.com/xdp-gacl/p/4259481.html   http://blog.csdn.net/hu_shengyang/article/de ...

  3. Spring 3 整合Apache CXF WebService[转]

    http://www.cnblogs.com/hoojo/archive/2012/07/13/2590593.html 在CXF2版本中,整合Spring3发布CXF WebService就更加简单 ...

  4. (转)Spring对注解(Annotation)处理源码分析1——扫描和读取Bean定义

    1.从Spring2.0以后的版本中,Spring也引入了基于注解(Annotation)方式的配置,注解(Annotation)是JDK1.5中引入的一个新特性,用于简化Bean的配置,某些场合可以 ...

  5. Spring对注解(Annotation)处理【转】

    1.从Spring2.0以后的版本中,spring也引入了基于注解(Annotation)方式的配置,注解(Annotation)是JDK1.5中引入的一个新特性,用于简化Bean的配置,某些场合可以 ...

  6. java调用CXF WebService接口的两种方式

    通过http://localhost:7002/card/services/HelloWorld?wsdl访问到xml如下,说明接口写对了. 2.静态调用 // 创建WebService客户端代理工厂 ...

  7. Java学习之注解Annotation实现原理

    前言: 最近学习了EventBus.BufferKinfe.GreenDao.Retrofit 等优秀开源框架,它们新版本无一另外的都使用到了注解的方式,我们使用在使用的时候也尝到不少好处,基于这种想 ...

  8. Java注解Annotation学习

    学习注解Annotation的原理,这篇讲的不错:http://blog.csdn.net/lylwo317/article/details/52163304 先自定义一个运行时注解 @Target( ...

  9. CXF WebService 教程

    业务需求:常见WEB服务: 手机淘宝.京东…. 天气预报 手机号归属地 股票查询 发手机短消息 手机充值功能 中英文翻译 银行转账业务 公司的“进销存系统”在某商品缺货时自动给供应商下订单 ..... ...

随机推荐

  1. 【BZOJ4355】Play with sequence 线段树

    [BZOJ4355]Play with sequence Description 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a ...

  2. 生存分析(survival analysis)

    一.生存分析(survival analysis)的定义 生存分析:对一个或多个非负随机变量进行统计推断,研究生存现象和响应时间数据及其统计规律的一门学科. 生存分析:既考虑结果又考虑生存时间的一种统 ...

  3. ASP.NET MVC中ViewBag和ViewData的区别

    在MVC3.0以上我们会用到ViewBag或者ViewData进行页面传值,对比一下二者的差距: ViewData ViewBag 基于key/value的字典集合 dynamic类型对象 从ASP. ...

  4. Centos6.10安装tomcat

    1.  下载tomcat 2.  解压到相应的路径下 tar -xzvf apache-tomcat-8.5.34.tar.gz 3.  启动tomcat # 进入"apache-tomca ...

  5. JAVA补充-抽象类

    1.抽象类基本概念 package com.neusoft.abstracted; /** * 抽象类:在class之前加abstract关键字 * 抽象方法语法: 修饰符 abstract 返回值类 ...

  6. POJ 2987 - Firing - [最大权闭合子图]

    题目链接:http://poj.org/problem?id=2987 Time Limit: 5000MS Memory Limit: 131072K Description You’ve fina ...

  7. HDU 3746 - Cyclic Nacklace & HDU 1358 - Period - [KMP求最小循环节]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  8. Reading table information for completion of table and column names

    mysql> use ad_detail_page;Reading table information for completion of table and column namesYou c ...

  9. shuffle

    <?php function genArr($len) { $arr = array(); $len = $len; for ($w=1; $w < $len ; $w++) { $arr ...

  10. REDIS 六. 修复方案

    六. 修复方案 6.1 禁止一些高危命令(重启redis才能生效) 修改 redis.conf 文件,禁用远程修改 DB 文件地址 rename-command FLUSHALL "&quo ...