设计一个对接系统,通过动态模型的增删改触发业务系统相应服务的调用。模型增删改方法动态发布为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. Unity3D笔记 GUI 三、实现选项卡二窗口

    实现目标: 1.使用个性化Box控件 2.个性化Lable控件 3.添加纵向滚动条 4.新建SelectedItem样式 一.最终效果: 二.主要代码 using UnityEngine; using ...

  2. Java虚拟机九 java.lang.String在虚拟机中的实现

    在Java中,Java的设计者对String对象进行了大量的优化,主要有三个特点: 1.不变性: 不变性是指String对象一旦生成,则不能再对它进行改变.String的这个特点可以泛化成不变(imm ...

  3. chrome扩展写法

    最近看到公司同事经常写chrome扩展,来提高生成效率,回想想自己以前也写过chrome扩展,但是由于不经常写,也没做积累也都忘记了,现在重新回顾一下. 一.chrome扩展基本概念 chrome扩展 ...

  4. iOS - Reveal逆向分析任意iOS应用的UI界面

    在iOS逆向工程中,Reveal扮演着重要角色,一般情况下,Reveal在iOS开发过程中可以分析UI界面的状态,同样也可以应用于分析其他任意的App.Reveal是一个很强大的UI分析工具,可非常直 ...

  5. web.xml之context-param,listener,filter,servlet加载顺序及其周边

    先以加载spring为例子看看加载顺序的作用: Spring加载可以利用ServletContextListener 实现,也可以采用load-on-startup Servlet 实现,但比如fil ...

  6. pandas 中的DataFrame.where()使用

    pandas.DataFrame.where DataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, try_ca ...

  7. jdk 与jre

    1. 定义JRE(Java Runtime Enviroment)是Java的运行环境.面向Java程序的使用者,而不是开发者.如果你仅下载并安装了JRE,那么你的系统只能运行Java程序.JRE是运 ...

  8. 把Asp.Net Core 2.0部署在Linux上,使用Nginx代理服务器,并且用Systemctl命令以服务的方式监听项目

    在Linux上部署.net core 2.0程序: 第一步:配置Nginx代理 在/etc/nginx/sites-available/default 中添加 server { listen ; lo ...

  9. hdu6390GuGuFishtion【数论】

    GuGuFishtion Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  10. 微软、谷歌、亚马逊、Facebook等硅谷大厂91个开源软件盘点(附下载地址)

    开源软件中有大量专家构建的代码,大大节省了开发人员的时间和成本,热衷于开源的大厂们总是能够带给我们新的惊喜.2016年9月GitHub报告显示,GitHub已经有超过 520 万的用户和超 30 万的 ...