hessian应用

hessian应用(想要知道hessian有没有建成功,在service启动后直接访问service,如果报405表示成功)

hessian有两种使用方法

一:web  使用xml,    service 使用xml

web项目依赖

web.xml

<!-- springmvc的前端控制器 -->
    <servlet>
        <servlet-name>taotao-manager-web</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/*.xml</param-value>
        </init-param>
        <init-param>
            <param-name>dispatchOptionsRequest</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>taotao-manager-web</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

spring-hessian.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
	">
    
     <bean id="itemService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl" value="http://localhost:6006/itemService"/>
        <property name="serviceInterface" value="com.taotao.service.ItemService"/>
         <property name="overloadEnabled" value="true" />
    </bean>
</beans>

service项目依赖

与web差不多。

web.xml

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

<!-- 注意这里servlet是一定配置的,也可配置hessianServlet,不过那样需要service继续hessianServlet类,如果这里不配置的话,hessian是找不到目标service的 -->
    <servlet>
        <servlet-name>hessian-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/applicationt-hessian.xml</param-value>
        </init-param>
        <init-param>
            <param-name>dispatchOptionsRequest</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>hessian-servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

service中的hessian配置,application-hessian.xml

注意注解中说的坑,service如果不是用注解注入的话,那么在其中使用自动注入也是无法实现的。而里面说的ref无法映射应该是spring-context的依赖有问题。

<?xml version="1.0" encoding="UTF-8"?>
	<beans xmlns="http://www.springframework.org/schema/beans"
		   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		   xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!-- 注意这里遇到坑,一开始ref怎么都映射不到实体类,我就直接配置了一个,然后后面运行项目的时候导致mapper自动注入不了 -->
	<!--<bean name="itemServiceImpl" class="com.taotao.service.impl.ItemServiceImpl"/>-->
 	<bean name="/itemService"
		class="org.springframework.remoting.caucho.HessianServiceExporter">
		<property name="service" ref="itemServiceImpl" />
		<property name="serviceInterface"
			value="com.taotao.service.ItemService" />
	</bean>
</beans>

 

第二种方式:  java配置 + xml

xml的方式就是上面service,现在介绍web层中的java配置

HessianManager

/**
 * 远程服务统一管理
 * <p>1.确认访问域名是否存在</p>
 * <p> 不存在添加访问域名</p>
 * <p>2.确认服务接口是否存在</p>
 * <p> 不存在则添加服务接口及获取接口方法</p>
 * <P>3.上述完成后直接调用服务</p>
 * <code>HessianManager.getXXXService().xxx</code>方法
 */
public class HessianManager {

	public static final Logger LOG = Logger.getLogger(HessianManager.class);
	
	//============================访问域名=======================
	public static final String SERVICE_URL = PropertiesHelp.getProperty("hessian.serviceUrl");
	
	//============================访问域名END=======================
	//=============================服务接口======================
	private static LoginService loginService = null;

	
	//=============================服务接口END======================
	//==========================获取服务方法==========================
	

	//==========================获取服务方法END==========================
	/**
	 * 通用获取接口
	 * @param clazz				接口类Class对象
	 * @param serviceUrl		远程URL
	 * @param serviceName		服务接口名称
	 * @return
	 */
    public static synchronized LoginService getLoginService(){
		
		if(loginService ==null){
			loginService = createService(LoginService .class, SERVICE_URL,"loginService ");
		}
		return loginService;
	}
	@SuppressWarnings("unchecked")
	public static <T>T createService(Class<T> clazz,String serviceUrl,String serviceName){
		try {
			HessianProxyFactory factory = new HessianProxyFactory();
			T service = (T)factory.create(clazz, serviceUrl+"/"+serviceName);
			LOG.info("HessianManager_CREATE_"+serviceName);
			return service;
		} catch (Exception e) {
			LOG.error("HessianManager_"+serviceName+"_err,e=="+e);
			return null;
		}
	}
	
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值