`
xiaoZ5919
  • 浏览: 400243 次
  • 性别: Icon_minigender_1
  • 来自: 安平人@北京
博客专栏
Group-logo
Netty学习笔记
浏览量:72746
社区版块
存档分类
最新评论

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并且注册。还是看看代码吧

 

  for (String basePackage : basePackages) {
                Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
                for (BeanDefinition candidate : candidates) {
                    ScopeMetadata scopeMetadata = this.scopeMetadataResolver
                            .resolveScopeMetadata(candidate);
                    candidate.setScope(scopeMetadata.getScopeName());
                    String originalBeanName = this.beanNameGenerator.generateBeanName(candidate,
                            this.registry);
                    // if (candidate instanceof AbstractBeanDefinition) {
                    // postProcessBeanDefinition((AbstractBeanDefinition)
                    // candidate, originalBeanName);
                    // }
                    
                    ScannedGenericBeanDefinition bd = (ScannedGenericBeanDefinition) candidate;
                    String beanClassName = bd.getBeanClassName();
                    bd.setBeanClassName(JsonRpcExporter.class.getName());
                    bd.setBeanClass(JsonRpcExporter.class);//如果你用的hessian或者httpinvoker请换成相应的exporter
                    bd.getPropertyValues().add("service",
                            applicationContext.getBean(originalBeanName));
                    bd.getPropertyValues().add("beanClassName", beanClassName);
                    String[] interfaces = bd.getMetadata().getInterfaceNames();
                    if (interfaces == null || interfaces.length == 0)
                        continue;
                  //  HessianServiceExporter.class.getClassLoader().getResource(name)
                    Class interf = null;

 

 

   @Override
        protected void registerDefaultFilters() {

            addIncludeFilter(new AnnotationTypeFilter(JsonRpc.class));//只扫描带有JsonRpc的注解
        }

 @Component("helloService")

@JsonRpc("helloService")
public class HelloServiceImpl implements IHelloService {

 <bean id="scanner" class="com.taofang.scanner.HessianServiceScanner">

 

   更新

    非常感谢yangpeihai 兄的关注。起因是这样,我看到yang兄在一个帖子上说道能不能用注解暴露服务,正好这阶段我这也在做这方面的事情包括json-rpc,一看到这个帖子我就急匆匆的发了这篇blog,代码这也是从jsonrpc中摘出来的。也存在一些bug,要不是yang兄说他那边调试有问题,我现在还不知道。我发的这个版本,我把exporter的beanName和它引用的service的beanname搞混了,恰巧的是我把两个名字设成一样的,这个bug才隐藏在这里。按yang兄的要求我整了一份hessian的完整。测试类在test包中的ResourceTest。

 

 bd.getPropertyValues().add("service",
                            applicationContext.getBean(originalBeanName));//这里应该是exporter的service的beanname,
  BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate,
                          "/" +   beanName );//这里才是exporter的beanname,也是请求的url中的name

 

	   <property name="basePackage" value="com.taofang.service"></property>
	</bean
2
1
分享到:
评论
14 楼 facebook1314 2015-09-06  
请问后台的HessianService能发布成多线程模式么(prototype)?
13 楼 wangyujie0431 2015-08-27  
zhouwm402 写道
我也是:service里面的dao没法被spring自动注入,请问如何解决的?



service 中应该饮用RuntimeReference 而不是直接用applicationContext.getBean,因为getBean时resource没有注入
12 楼 lcwen_13 2015-08-22  
Scanner貌似是重复造轮子的赶脚
11 楼 zhouwm402 2013-05-11  
我也是:service里面的dao没法被spring自动注入,请问如何解决的?
10 楼 yangpeihai 2012-11-20  
xiaoZ5919 写道
yangpeihai 写道
xiaoZ5919 写道
yangpeihai 写道
xiaoZ5919 写道
yangpeihai 写道
xiaoZ5919 写道
yangpeihai 写道
附件好像少了一个类com.taofang.jsonrpc.JsonRpcExporter;
楼主能不能上传一个完整的helloWord例子(jar包除外),让我跟楼主helloWord ^_^,我 是 菜 鸟,别喷哈.....
非常感谢

那个是用来导出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,如下

@Service
@JsonRpc("userServiceRemote")
public class UserService implements IUserService {
 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1863135765563158207L;
	@Autowired
	private UserDAO userDao;

	public List findAll() {
		return userDao.findAll();
	}
}



测试类如下:

public class UserServiceTest {
	private static IUserService userService = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		try {
			AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-common.xml");
			userService = (IUserService) ctx.getBean("userServiceRemote");
		} catch (Exception e) { 
			e.printStackTrace();
		}
	}
 
	@Test
	public void testFindAll() {
		List<User> users = userService.findAll();
		for (User user : users) {
			System.out.println(user);
		}
	}
 
}


但是获取不到userServiceRemote对象哦,但是我可以获取到userService对象,请楼主指导下,我是否还要配置什么,我已经在applicationContext-common.xml配置了spring的annotation扫描
    <context:annotation-config />
    <context:component-scan base-package="com.oy" />

非常感谢

仁兄,是这样的。象remoting这样的服务,是通过beannameurlhandlermapping来分发的,所以该bean的name必须以"/"开头,就像咱们在xml配置的一样加上一个/,你试试userService = (IUserService) ctx.getBean("/userServiceRemote");或者加我qq405919612 

非常感谢楼主耐心的回答,今晚回去加你QQ。
我刚才检查了一下,是我没有加上配置
<bean id="scanner" class="com.scanner.JsonRpcServiceScanner">
<property name="basePackage" 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自动注入,如下:
     @Autowired  
     private UserDAO userDao;  

2.部署到服务器后,没法访问暴露的service
服务端web.xml配置如下:
	<servlet>
		<servlet-name>remote</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置该Servlet随应用启动时候启动 -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- 配置DispatcherServlet映射的url -->
	<servlet-mapping>
		<servlet-name>remote</servlet-name>
		<url-pattern>/remoting/*</url-pattern>
	</servlet-mapping>


客户端:
	public static void main(String[] args) {
		ApplicationContext ctx = new FileSystemXmlApplicationContext("WebContent/WEB-INF/remote.xml");
		BaseRemotingServiceFactory service = (BaseRemotingServiceFactory) ctx.getBean("baseRemoteService");
		IUserService remoteService = (IUserService) service.getRemoteObject(IUserService.class,
				"http://127.0.0.1:7002/WebRoot", "userService"); 
		List result = remoteService.findAll();
		System.out.println(result);

	}


说明:在服务端使用annotation暴露远程服务前(以前是用xml配置暴露远程服务),我的客户端是可以访问的,所以排除客户端有问题。
楼主要是有时间,麻烦整一个完整的服务端例子,帮忙我们这些后来者一把,非常感谢

感谢关注,我提供了一个hessian的完整版本,你参考一下

cool~ 终于跑起来拉,楼主好人
9 楼 xiaoZ5919 2012-11-20  
yangpeihai 写道
xiaoZ5919 写道
yangpeihai 写道
xiaoZ5919 写道
yangpeihai 写道
xiaoZ5919 写道
yangpeihai 写道
附件好像少了一个类com.taofang.jsonrpc.JsonRpcExporter;
楼主能不能上传一个完整的helloWord例子(jar包除外),让我跟楼主helloWord ^_^,我 是 菜 鸟,别喷哈.....
非常感谢

那个是用来导出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,如下

@Service
@JsonRpc("userServiceRemote")
public class UserService implements IUserService {
 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1863135765563158207L;
	@Autowired
	private UserDAO userDao;

	public List findAll() {
		return userDao.findAll();
	}
}



测试类如下:

public class UserServiceTest {
	private static IUserService userService = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		try {
			AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-common.xml");
			userService = (IUserService) ctx.getBean("userServiceRemote");
		} catch (Exception e) { 
			e.printStackTrace();
		}
	}
 
	@Test
	public void testFindAll() {
		List<User> users = userService.findAll();
		for (User user : users) {
			System.out.println(user);
		}
	}
 
}


但是获取不到userServiceRemote对象哦,但是我可以获取到userService对象,请楼主指导下,我是否还要配置什么,我已经在applicationContext-common.xml配置了spring的annotation扫描
    <context:annotation-config />
    <context:component-scan base-package="com.oy" />

非常感谢

仁兄,是这样的。象remoting这样的服务,是通过beannameurlhandlermapping来分发的,所以该bean的name必须以"/"开头,就像咱们在xml配置的一样加上一个/,你试试userService = (IUserService) ctx.getBean("/userServiceRemote");或者加我qq405919612 

非常感谢楼主耐心的回答,今晚回去加你QQ。
我刚才检查了一下,是我没有加上配置
<bean id="scanner" class="com.scanner.JsonRpcServiceScanner">
<property name="basePackage" 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自动注入,如下:
     @Autowired  
     private UserDAO userDao;  

2.部署到服务器后,没法访问暴露的service
服务端web.xml配置如下:
	<servlet>
		<servlet-name>remote</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置该Servlet随应用启动时候启动 -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- 配置DispatcherServlet映射的url -->
	<servlet-mapping>
		<servlet-name>remote</servlet-name>
		<url-pattern>/remoting/*</url-pattern>
	</servlet-mapping>


客户端:
	public static void main(String[] args) {
		ApplicationContext ctx = new FileSystemXmlApplicationContext("WebContent/WEB-INF/remote.xml");
		BaseRemotingServiceFactory service = (BaseRemotingServiceFactory) ctx.getBean("baseRemoteService");
		IUserService remoteService = (IUserService) service.getRemoteObject(IUserService.class,
				"http://127.0.0.1:7002/WebRoot", "userService"); 
		List result = remoteService.findAll();
		System.out.println(result);

	}


说明:在服务端使用annotation暴露远程服务前(以前是用xml配置暴露远程服务),我的客户端是可以访问的,所以排除客户端有问题。
楼主要是有时间,麻烦整一个完整的服务端例子,帮忙我们这些后来者一把,非常感谢

感谢关注,我提供了一个hessian的完整版本,你参考一下
8 楼 yangpeihai 2012-11-20  
xiaoZ5919 写道
yangpeihai 写道
xiaoZ5919 写道
yangpeihai 写道
xiaoZ5919 写道
yangpeihai 写道
附件好像少了一个类com.taofang.jsonrpc.JsonRpcExporter;
楼主能不能上传一个完整的helloWord例子(jar包除外),让我跟楼主helloWord ^_^,我 是 菜 鸟,别喷哈.....
非常感谢

那个是用来导出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,如下

@Service
@JsonRpc("userServiceRemote")
public class UserService implements IUserService {
 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1863135765563158207L;
	@Autowired
	private UserDAO userDao;

	public List findAll() {
		return userDao.findAll();
	}
}



测试类如下:

public class UserServiceTest {
	private static IUserService userService = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		try {
			AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-common.xml");
			userService = (IUserService) ctx.getBean("userServiceRemote");
		} catch (Exception e) { 
			e.printStackTrace();
		}
	}
 
	@Test
	public void testFindAll() {
		List<User> users = userService.findAll();
		for (User user : users) {
			System.out.println(user);
		}
	}
 
}


但是获取不到userServiceRemote对象哦,但是我可以获取到userService对象,请楼主指导下,我是否还要配置什么,我已经在applicationContext-common.xml配置了spring的annotation扫描
    <context:annotation-config />
    <context:component-scan base-package="com.oy" />

非常感谢

仁兄,是这样的。象remoting这样的服务,是通过beannameurlhandlermapping来分发的,所以该bean的name必须以"/"开头,就像咱们在xml配置的一样加上一个/,你试试userService = (IUserService) ctx.getBean("/userServiceRemote");或者加我qq405919612 

非常感谢楼主耐心的回答,今晚回去加你QQ。
我刚才检查了一下,是我没有加上配置
<bean id="scanner" class="com.scanner.JsonRpcServiceScanner">
<property name="basePackage" 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自动注入,如下:
     @Autowired  
     private UserDAO userDao;  

2.部署到服务器后,没法访问暴露的service
服务端web.xml配置如下:
	<servlet>
		<servlet-name>remote</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置该Servlet随应用启动时候启动 -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- 配置DispatcherServlet映射的url -->
	<servlet-mapping>
		<servlet-name>remote</servlet-name>
		<url-pattern>/remoting/*</url-pattern>
	</servlet-mapping>


客户端:
	public static void main(String[] args) {
		ApplicationContext ctx = new FileSystemXmlApplicationContext("WebContent/WEB-INF/remote.xml");
		BaseRemotingServiceFactory service = (BaseRemotingServiceFactory) ctx.getBean("baseRemoteService");
		IUserService remoteService = (IUserService) service.getRemoteObject(IUserService.class,
				"http://127.0.0.1:7002/WebRoot", "userService"); 
		List result = remoteService.findAll();
		System.out.println(result);

	}


说明:在服务端使用annotation暴露远程服务前(以前是用xml配置暴露远程服务),我的客户端是可以访问的,所以排除客户端有问题。
楼主要是有时间,麻烦整一个完整的服务端例子,帮忙我们这些后来者一把,非常感谢
7 楼 xiaoZ5919 2012-11-20  
yangpeihai 写道
xiaoZ5919 写道
yangpeihai 写道
xiaoZ5919 写道
yangpeihai 写道
附件好像少了一个类com.taofang.jsonrpc.JsonRpcExporter;
楼主能不能上传一个完整的helloWord例子(jar包除外),让我跟楼主helloWord ^_^,我 是 菜 鸟,别喷哈.....
非常感谢

那个是用来导出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,如下

@Service
@JsonRpc("userServiceRemote")
public class UserService implements IUserService {
 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1863135765563158207L;
	@Autowired
	private UserDAO userDao;

	public List findAll() {
		return userDao.findAll();
	}
}



测试类如下:

public class UserServiceTest {
	private static IUserService userService = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		try {
			AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-common.xml");
			userService = (IUserService) ctx.getBean("userServiceRemote");
		} catch (Exception e) { 
			e.printStackTrace();
		}
	}
 
	@Test
	public void testFindAll() {
		List<User> users = userService.findAll();
		for (User user : users) {
			System.out.println(user);
		}
	}
 
}


但是获取不到userServiceRemote对象哦,但是我可以获取到userService对象,请楼主指导下,我是否还要配置什么,我已经在applicationContext-common.xml配置了spring的annotation扫描
    <context:annotation-config />
    <context:component-scan base-package="com.oy" />

非常感谢

仁兄,是这样的。象remoting这样的服务,是通过beannameurlhandlermapping来分发的,所以该bean的name必须以"/"开头,就像咱们在xml配置的一样加上一个/,你试试userService = (IUserService) ctx.getBean("/userServiceRemote");或者加我qq405919612 

非常感谢楼主耐心的回答,今晚回去加你QQ。
我刚才检查了一下,是我没有加上配置
<bean id="scanner" class="com.scanner.JsonRpcServiceScanner">
<property name="basePackage" 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); 
你把这行去掉就可以了

实在是抱歉!整得比较仓促
6 楼 yangpeihai 2012-11-20  
xiaoZ5919 写道
yangpeihai 写道
xiaoZ5919 写道
yangpeihai 写道
附件好像少了一个类com.taofang.jsonrpc.JsonRpcExporter;
楼主能不能上传一个完整的helloWord例子(jar包除外),让我跟楼主helloWord ^_^,我 是 菜 鸟,别喷哈.....
非常感谢

那个是用来导出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,如下

@Service
@JsonRpc("userServiceRemote")
public class UserService implements IUserService {
 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1863135765563158207L;
	@Autowired
	private UserDAO userDao;

	public List findAll() {
		return userDao.findAll();
	}
}



测试类如下:

public class UserServiceTest {
	private static IUserService userService = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		try {
			AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-common.xml");
			userService = (IUserService) ctx.getBean("userServiceRemote");
		} catch (Exception e) { 
			e.printStackTrace();
		}
	}
 
	@Test
	public void testFindAll() {
		List<User> users = userService.findAll();
		for (User user : users) {
			System.out.println(user);
		}
	}
 
}


但是获取不到userServiceRemote对象哦,但是我可以获取到userService对象,请楼主指导下,我是否还要配置什么,我已经在applicationContext-common.xml配置了spring的annotation扫描
    <context:annotation-config />
    <context:component-scan base-package="com.oy" />

非常感谢

仁兄,是这样的。象remoting这样的服务,是通过beannameurlhandlermapping来分发的,所以该bean的name必须以"/"开头,就像咱们在xml配置的一样加上一个/,你试试userService = (IUserService) ctx.getBean("/userServiceRemote");或者加我qq405919612 

非常感谢楼主耐心的回答,今晚回去加你QQ。
我刚才检查了一下,是我没有加上配置
<bean id="scanner" class="com.scanner.JsonRpcServiceScanner">
<property name="basePackage" 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?

我是哪里设置错了吗?
多谢 。。。。
5 楼 xiaoZ5919 2012-11-20  
yangpeihai 写道
xiaoZ5919 写道
yangpeihai 写道
附件好像少了一个类com.taofang.jsonrpc.JsonRpcExporter;
楼主能不能上传一个完整的helloWord例子(jar包除外),让我跟楼主helloWord ^_^,我 是 菜 鸟,别喷哈.....
非常感谢

那个是用来导出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,如下

@Service
@JsonRpc("userServiceRemote")
public class UserService implements IUserService {
 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1863135765563158207L;
	@Autowired
	private UserDAO userDao;

	public List findAll() {
		return userDao.findAll();
	}
}



测试类如下:

public class UserServiceTest {
	private static IUserService userService = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		try {
			AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-common.xml");
			userService = (IUserService) ctx.getBean("userServiceRemote");
		} catch (Exception e) { 
			e.printStackTrace();
		}
	}
 
	@Test
	public void testFindAll() {
		List<User> users = userService.findAll();
		for (User user : users) {
			System.out.println(user);
		}
	}
 
}


但是获取不到userServiceRemote对象哦,但是我可以获取到userService对象,请楼主指导下,我是否还要配置什么,我已经在applicationContext-common.xml配置了spring的annotation扫描
    <context:annotation-config />
    <context:component-scan base-package="com.oy" />

非常感谢

仁兄,是这样的。象remoting这样的服务,是通过beannameurlhandlermapping来分发的,所以该bean的name必须以"/"开头,就像咱们在xml配置的一样加上一个/,你试试userService = (IUserService) ctx.getBean("/userServiceRemote");或者加我qq405919612 
4 楼 yangpeihai 2012-11-20  
xiaoZ5919 写道
yangpeihai 写道
附件好像少了一个类com.taofang.jsonrpc.JsonRpcExporter;
楼主能不能上传一个完整的helloWord例子(jar包除外),让我跟楼主helloWord ^_^,我 是 菜 鸟,别喷哈.....
非常感谢

那个是用来导出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,如下

@Service
@JsonRpc("userServiceRemote")
public class UserService implements IUserService {
 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1863135765563158207L;
	@Autowired
	private UserDAO userDao;

	public List findAll() {
		return userDao.findAll();
	}
}



测试类如下:

public class UserServiceTest {
	private static IUserService userService = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		try {
			AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-common.xml");
			userService = (IUserService) ctx.getBean("userServiceRemote");
		} catch (Exception e) { 
			e.printStackTrace();
		}
	}
 
	@Test
	public void testFindAll() {
		List<User> users = userService.findAll();
		for (User user : users) {
			System.out.println(user);
		}
	}
 
}


但是获取不到userServiceRemote对象哦,但是我可以获取到userService对象,请楼主指导下,我是否还要配置什么,我已经在applicationContext-common.xml配置了spring的annotation扫描
    <context:annotation-config />
    <context:component-scan base-package="com.oy" />

非常感谢
3 楼 tou3921 2012-11-20  
xiaoZ5919 写道
yangpeihai 写道
附件好像少了一个类com.taofang.jsonrpc.JsonRpcExporter;
楼主能不能上传一个完整的helloWord例子(jar包除外),让我跟楼主helloWord ^_^,我 是 菜 鸟,别喷哈.....
非常感谢

那个是用来导出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")
不错,可以把我封转的那个改进改进
2 楼 xiaoZ5919 2012-11-20  
yangpeihai 写道
附件好像少了一个类com.taofang.jsonrpc.JsonRpcExporter;
楼主能不能上传一个完整的helloWord例子(jar包除外),让我跟楼主helloWord ^_^,我 是 菜 鸟,别喷哈.....
非常感谢

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

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

换成bd.setBeanClass(HttpInvokerServiceExporter.class);

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

我会重构一下这个类,让通过配置参数扫描响应的服务。
1 楼 yangpeihai 2012-11-20  
附件好像少了一个类com.taofang.jsonrpc.JsonRpcExporter;
楼主能不能上传一个完整的helloWord例子(jar包除外),让我跟楼主helloWord ^_^,我 是 菜 鸟,别喷哈.....
非常感谢

相关推荐

Global site tag (gtag.js) - Google Analytics