读书人

spring运用注解暴露remoting服务

发布时间: 2012-12-14 10:33:08 作者: rapoo

spring使用注解暴露remoting服务

? spring提供了多种序列化方式的基于http协议的rometing服务并且在使用的接口方式,服务端使用Exporter,客户端使用FactoryBean,例如hessian的HessianExporter和HessianProxyFactoryBean。exporter有两个参数比较关键一个是serviceInterface,另外一个是service bean本身。无论是HessianExporter还是HttpInvokerExporter都需要配置这两参数,目前spring是在配置文件中进行配置:

?

<bean?id="helloService"?class="lavasoft.suths.service.HelloService"/>?
????????<bean?name="/hello"?class="org.springframework.remoting.caucho.HessianServiceExporter">
????????????????<property?name="service"?ref="helloService"/>?
????????????????<property?name="serviceInterface"?value="lavasoft.suths.service.Hello"/>?
????????</bean>?

?

很显然有时候配置总是烦人,并且容易出错,有没有更简单的方式,通过注解的方式来暴露服务。如用注解又该来如何做呢?我们知道spring先生成beandefinition,再初始化bean。这样我们先扫描带有注解的service并根据该service构造exporter的bean definition并且注册。还是看看代码吧

?

?

  
那个是用来导出json-rpc服务。如果你用的是httpinvoker,
请将

bd.setBeanClass(JsonRpcExporter.class);//如果你用的hessian或者httpinvoker请换成相应的exporter

换成bd.setBeanClass(HttpInvokerServiceExporter.class);

并且创建一个类似JsonRpc的注解例如httpinvoker,同时修改
addIncludeFilter(new AnnotationTypeFilter(JsonRpc.class));//只扫描带有JsonRpc的注解

我会重构一下这个类,让通过配置参数扫描响应的服务。
那个是用来导出json-rpc服务。如果你用的是httpinvoker,
请将

bd.setBeanClass(JsonRpcExporter.class);//如果你用的hessian或者httpinvoker请换成相应的exporter

换成bd.setBeanClass(HttpInvokerServiceExporter.class);

并且创建一个类似JsonRpc的注解例如httpinvoker,同时修改
addIncludeFilter(new AnnotationTypeFilter(JsonRpc.class));//只扫描带有JsonRpc的注解

我会重构一下这个类,让通过配置参数扫描响应的服务。

@RpcExporter(protocol="json")
不错,可以把我封转的那个改进改进
那个是用来导出json-rpc服务。如果你用的是httpinvoker,
请将

bd.setBeanClass(JsonRpcExporter.class);//如果你用的hessian或者httpinvoker请换成相应的exporter

换成bd.setBeanClass(HttpInvokerServiceExporter.class);

并且创建一个类似JsonRpc的注解例如httpinvoker,同时修改
addIncludeFilter(new AnnotationTypeFilter(JsonRpc.class));//只扫描带有JsonRpc的注解

我会重构一下这个类,让通过配置参数扫描响应的服务。
我已经换为bd.setBeanClass(HttpInvokerServiceExporter.class)了,然后在我在service加上@JsonRpc,如下


非常感谢
那个是用来导出json-rpc服务。如果你用的是httpinvoker,
请将

bd.setBeanClass(JsonRpcExporter.class);//如果你用的hessian或者httpinvoker请换成相应的exporter

换成bd.setBeanClass(HttpInvokerServiceExporter.class);

并且创建一个类似JsonRpc的注解例如httpinvoker,同时修改
addIncludeFilter(new AnnotationTypeFilter(JsonRpc.class));//只扫描带有JsonRpc的注解

我会重构一下这个类,让通过配置参数扫描响应的服务。
我已经换为bd.setBeanClass(HttpInvokerServiceExporter.class)了,然后在我在service加上@JsonRpc,如下


非常感谢
仁兄,是这样的。象remoting这样的服务,是通过beannameurlhandlermapping来分发的,所以该bean的name必须以"/"开头,就像咱们在xml配置的一样加上一个/,你试试userService = (IUserService) ctx.getBean("/userServiceRemote");或者加我qq405919612
那个是用来导出json-rpc服务。如果你用的是httpinvoker,
请将

bd.setBeanClass(JsonRpcExporter.class);//如果你用的hessian或者httpinvoker请换成相应的exporter

换成bd.setBeanClass(HttpInvokerServiceExporter.class);

并且创建一个类似JsonRpc的注解例如httpinvoker,同时修改
addIncludeFilter(new AnnotationTypeFilter(JsonRpc.class));//只扫描带有JsonRpc的注解

我会重构一下这个类,让通过配置参数扫描响应的服务。
我已经换为bd.setBeanClass(HttpInvokerServiceExporter.class)了,然后在我在service加上@JsonRpc,如下


非常感谢
仁兄,是这样的。象remoting这样的服务,是通过beannameurlhandlermapping来分发的,所以该bean的name必须以"/"开头,就像咱们在xml配置的一样加上一个/,你试试userService = (IUserService) ctx.getBean("/userServiceRemote");或者加我qq405919612
非常感谢楼主耐心的回答,今晚回去加你QQ。
我刚才检查了一下,是我没有加上配置
<bean id="scanner" value="com.oy.service"></property>
</bean>

service也做了修改
@Service("userService")
@JsonRpc("userService")
public class UserService implements IUserService

现在遇到另外一个错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/userService' defined in file [D:\myResource\ibatis\springMVC-ibatis\WebRoot\WEB-INF\classes\com\oy\service\UserService.class]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'beanClassName' of bean class [org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter]: Bean property 'beanClassName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

我是哪里设置错了吗?
多谢 。。。。
那个是用来导出json-rpc服务。如果你用的是httpinvoker,
请将

bd.setBeanClass(JsonRpcExporter.class);//如果你用的hessian或者httpinvoker请换成相应的exporter

换成bd.setBeanClass(HttpInvokerServiceExporter.class);

并且创建一个类似JsonRpc的注解例如httpinvoker,同时修改
addIncludeFilter(new AnnotationTypeFilter(JsonRpc.class));//只扫描带有JsonRpc的注解

我会重构一下这个类,让通过配置参数扫描响应的服务。
我已经换为bd.setBeanClass(HttpInvokerServiceExporter.class)了,然后在我在service加上@JsonRpc,如下


非常感谢
仁兄,是这样的。象remoting这样的服务,是通过beannameurlhandlermapping来分发的,所以该bean的name必须以"/"开头,就像咱们在xml配置的一样加上一个/,你试试userService = (IUserService) ctx.getBean("/userServiceRemote");或者加我qq405919612
非常感谢楼主耐心的回答,今晚回去加你QQ。
我刚才检查了一下,是我没有加上配置
<bean id="scanner" value="com.oy.service"></property>
</bean>

service也做了修改
@Service("userService")
@JsonRpc("userService")
public class UserService implements IUserService

现在遇到另外一个错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/userService' defined in file [D:\myResource\ibatis\springMVC-ibatis\WebRoot\WEB-INF\classes\com\oy\service\UserService.class]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'beanClassName' of bean class [org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter]: Bean property 'beanClassName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

我是哪里设置错了吗?
多谢 。。。。

非常抱歉,我自己写的JsonRpcExporter有classbeanName这个属性

bd.getPropertyValues().add("beanClassName", beanClassName);
你把这行去掉就可以了

实在是抱歉!整得比较仓促
那个是用来导出json-rpc服务。如果你用的是httpinvoker,
请将

bd.setBeanClass(JsonRpcExporter.class);//如果你用的hessian或者httpinvoker请换成相应的exporter

换成bd.setBeanClass(HttpInvokerServiceExporter.class);

并且创建一个类似JsonRpc的注解例如httpinvoker,同时修改
addIncludeFilter(new AnnotationTypeFilter(JsonRpc.class));//只扫描带有JsonRpc的注解

我会重构一下这个类,让通过配置参数扫描响应的服务。
我已经换为bd.setBeanClass(HttpInvokerServiceExporter.class)了,然后在我在service加上@JsonRpc,如下


非常感谢
仁兄,是这样的。象remoting这样的服务,是通过beannameurlhandlermapping来分发的,所以该bean的name必须以"/"开头,就像咱们在xml配置的一样加上一个/,你试试userService = (IUserService) ctx.getBean("/userServiceRemote");或者加我qq405919612
非常感谢楼主耐心的回答,今晚回去加你QQ。
我刚才检查了一下,是我没有加上配置
<bean id="scanner" value="com.oy.service"></property>
</bean>

service也做了修改
@Service("userService")
@JsonRpc("userService")
public class UserService implements IUserService

现在遇到另外一个错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/userService' defined in file [D:\myResource\ibatis\springMVC-ibatis\WebRoot\WEB-INF\classes\com\oy\service\UserService.class]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'beanClassName' of bean class [org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter]: Bean property 'beanClassName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

我是哪里设置错了吗?
多谢 。。。。

非常抱歉,我自己写的JsonRpcExporter有classbeanName这个属性

bd.getPropertyValues().add("beanClassName", beanClassName);
你把这行去掉就可以了

实在是抱歉!整得比较仓促
举步维艰啊,好不容易初始化不报错,现在碰到两个问题:
1.单元测试时,service里面的dao没法被spring自动注入,如下:
 

那个是用来导出json-rpc服务。如果你用的是httpinvoker,
请将

bd.setBeanClass(JsonRpcExporter.class);//如果你用的hessian或者httpinvoker请换成相应的exporter

换成bd.setBeanClass(HttpInvokerServiceExporter.class);

并且创建一个类似JsonRpc的注解例如httpinvoker,同时修改
addIncludeFilter(new AnnotationTypeFilter(JsonRpc.class));//只扫描带有JsonRpc的注解

我会重构一下这个类,让通过配置参数扫描响应的服务。
我已经换为bd.setBeanClass(HttpInvokerServiceExporter.class)了,然后在我在service加上@JsonRpc,如下


非常感谢
仁兄,是这样的。象remoting这样的服务,是通过beannameurlhandlermapping来分发的,所以该bean的name必须以"/"开头,就像咱们在xml配置的一样加上一个/,你试试userService = (IUserService) ctx.getBean("/userServiceRemote");或者加我qq405919612
非常感谢楼主耐心的回答,今晚回去加你QQ。
我刚才检查了一下,是我没有加上配置
<bean id="scanner" value="com.oy.service"></property>
</bean>

service也做了修改
@Service("userService")
@JsonRpc("userService")
public class UserService implements IUserService

现在遇到另外一个错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/userService' defined in file [D:\myResource\ibatis\springMVC-ibatis\WebRoot\WEB-INF\classes\com\oy\service\UserService.class]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'beanClassName' of bean class [org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter]: Bean property 'beanClassName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

我是哪里设置错了吗?
多谢 。。。。

非常抱歉,我自己写的JsonRpcExporter有classbeanName这个属性

bd.getPropertyValues().add("beanClassName", beanClassName);
你把这行去掉就可以了

实在是抱歉!整得比较仓促
举步维艰啊,好不容易初始化不报错,现在碰到两个问题:
1.单元测试时,service里面的dao没法被spring自动注入,如下:
 

感谢关注,我提供了一个hessian的完整版本,你参考一下
那个是用来导出json-rpc服务。如果你用的是httpinvoker,
请将

bd.setBeanClass(JsonRpcExporter.class);//如果你用的hessian或者httpinvoker请换成相应的exporter

换成bd.setBeanClass(HttpInvokerServiceExporter.class);

并且创建一个类似JsonRpc的注解例如httpinvoker,同时修改
addIncludeFilter(new AnnotationTypeFilter(JsonRpc.class));//只扫描带有JsonRpc的注解

我会重构一下这个类,让通过配置参数扫描响应的服务。
我已经换为bd.setBeanClass(HttpInvokerServiceExporter.class)了,然后在我在service加上@JsonRpc,如下


非常感谢
仁兄,是这样的。象remoting这样的服务,是通过beannameurlhandlermapping来分发的,所以该bean的name必须以"/"开头,就像咱们在xml配置的一样加上一个/,你试试userService = (IUserService) ctx.getBean("/userServiceRemote");或者加我qq405919612
非常感谢楼主耐心的回答,今晚回去加你QQ。
我刚才检查了一下,是我没有加上配置
<bean id="scanner" value="com.oy.service"></property>
</bean>

service也做了修改
@Service("userService")
@JsonRpc("userService")
public class UserService implements IUserService

现在遇到另外一个错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/userService' defined in file [D:\myResource\ibatis\springMVC-ibatis\WebRoot\WEB-INF\classes\com\oy\service\UserService.class]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'beanClassName' of bean class [org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter]: Bean property 'beanClassName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

我是哪里设置错了吗?
多谢 。。。。

非常抱歉,我自己写的JsonRpcExporter有classbeanName这个属性

bd.getPropertyValues().add("beanClassName", beanClassName);
你把这行去掉就可以了

实在是抱歉!整得比较仓促
举步维艰啊,好不容易初始化不报错,现在碰到两个问题:
1.单元测试时,service里面的dao没法被spring自动注入,如下:
 

感谢关注,我提供了一个hessian的完整版本,你参考一下
cool~ 终于跑起来拉,楼主好人

读书人网 >软件架构设计

热点推荐