hessian spring疑问(已解决)
今天随便用maven创建了个工程,写了个hessian的服务,然后写了个客户端调用这个hessian服务,发现总是报如下错误:
?根据以前开发hessian接口的经验这种情况应该都是启动服务报错了,但是我用log4j打印日志,发现没有任何错误,后来想了下,这个错误是说文件找不到,那应该就是bean服务找不到,我现在是把配置文件放在类路径下,工程结构如下:
?<bean id="helloWorldAPI1" ref="helloWorldAPI1" />
<property name="serviceInterface" value="com.hessian.api.JavassitHelloWorldAPI" />
</bean>
这段提供服务的配置是定义在hello-servlet.xml文件中,web.xml文件定义如下:
?
<?xml version="1.0" encoding="GB2312"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
?
<context-param>?
<param-name>log4jConfigLocation</param-name>?
<param-value>classpath:log4j.xml</param-value>?
</context-param>?
?
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
?
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
?
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
?
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
? ?classpath*:/spring/*.xml</param-value>
</context-param>
?
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>/jsp/login.jsp</welcome-file>
</welcome-file-list>
?
</web-app>
按照道理,spring回去解析/spring/*.xml下的hello-servlet.xml文件,就可以提供hessian服务,可是却报了这个找不到的错误,于是我将hessian的bean定义放在test-servlet.xml文件中,同时注释掉web.xml中的如下配置:<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
?? ?classpath*:/spring/*.xml</param-value>
</context-param>
?
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>然后重新编译部署,居然调用成功了。。。那这样的话很明显应用不能解析hello-servlet.xml中的hessian bean,能解析定义在test-servlet.xml中的hessian bean.那其他在hello-servlet.xml总的bean能解析吗?于是我定义了一个c ontroller,启动服务访问对应的页面是ok的,由此我觉得可能hessian的bean只能定义在test-servlet.xm中。。有点奇怪。。(续)今天再次碰到了这个只能将hessian bean放在test-servlet.xml中,且需要注释掉contextConfigLocation这段配置的问题,经测试后发现,并不是hessian bean只能放在test-servlet.xml中,而是应为HessianServiceExporter与spring的SimpleUrlHandlerMapping冲突,当请求hessian接口时优先由spring的DispatcherServlet发给SimpleUrlHandlerMapping分配哪个bean处理该请求,而当时在SimpleUrlHandlerMapping中没有与hessian地址后缀相同的配置,所以造成报找不到文件的异常。解决方法为:将hessian的配置放在SimpleUrlHandlerMapping中,详细代码如下:web.xml如下:<?xml version="1.0" encoding="GB2312"?><web-app id="WebApp_ID" version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet><servlet-name>test</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet>
<servlet-mapping><servlet-name>test</servlet-name><url-pattern>/service/*</url-pattern></servlet-mapping>
<context-param><param-name>contextConfigLocation</param-name><param-value> ? ?classpath*:/spring/*.xml</param-value></context-param>
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><welcome-file-list><welcome-file>/jsp/login.jsp</welcome-file></welcome-file-list>
</web-app>
%classpath%/sping/test.xml:<?xml version="1.0" encoding="utf-8"?>
<beans default-autowire="byName"xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-2.0.xsd?http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.0.xsd?http://www.springframework.org/schema/util?http://www.springframework.org/schema/util/spring-util-2.5.xsd ">
<bean id="viewResolver"encoding="utf-8"?>
<beans default-autowire="byName"xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-2.0.xsd?http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.0.xsd?http://www.springframework.org/schema/util?http://www.springframework.org/schema/util/spring-util-2.5.xsd ">
<bean id="slaveHeartBeat" />
<bean name="/HeatBeat"ref="slaveHeartBeat" /><property name="serviceInterface" value="com.TaskScheduling.Master.SlaveHeartBeat" /></bean>
</beans>
测试类HessianTest如下:package com.hessian.test;
import java.net.MalformedURLException;
import org.apache.log4j.Logger;
import com.TaskScheduling.Master.SlaveHeartBeat;import com.caucho.hessian.client.HessianProxyFactory;
public class HessianTest {
private static Logger logger = Logger.getLogger(HessianTest.class);
public static void main(String args[]) {HessianProxyFactory factory = new HessianProxyFactory();
try {SlaveHeartBeat heatBeat=(SlaveHeartBeat)factory.create(SlaveHeartBeat.class,?"http://localhost:8080/demo.web/service/HeatBeat");String result=heatBeat.check();System.out.println(result);} catch (MalformedURLException e) {System.out.println(e);e.printStackTrace();}}
}运行后得到正确结果。
说明:也可将test.xml与test2.xml合并成一个文件,但是不可一部分放在test-servlet.xml中,一部分放在test.xml或者test2.xml中,否则会报找不到bean错误。----------------------------------------------spring加载xml说明:1.如果配置了如下:<servlet><servlet-name>test</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet>
<servlet-mapping><servlet-name>test</servlet-name><url-pattern>/service/*</url-pattern></servlet-mapping>则必须有WEB-INF/test-servlet.xml文件。
2.如果配置如下:<servlet><servlet-name>test</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet>
<servlet-mapping><servlet-name>test</servlet-name><url-pattern>/service/*</url-pattern></servlet-mapping>
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>则必须有WEB-INF/applicationContext.xml文件和WEB-INF/test-servlet.xml文件。
3.如果配置如下:<servlet><servlet-name>test</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet>
<servlet-mapping><servlet-name>test</servlet-name><url-pattern>/service/*</url-pattern></servlet-mapping>
<context-param><param-name>contextConfigLocation</param-name><param-value> ? ?classpath*:/spring/*.xml</param-value></context-param>
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>则必须有WEB-INF/test-servlet.xml文件,而WEB-INF/applicationContext.xml文件不需要