Spring RMI暴露服务

2 篇文章 0 订阅

Service 端:(注:org.springframework.web.servlet.DispatcherServlet)配置文件中.xml

	<!-- RMI -->
	<bean id="accountServiceImpl" class="com.spring.service.impl.AccountServiceImpl" />

	<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
		<property name="serviceName" value="AccountService" />
		<property name="service" ref="accountServiceImpl" />
		<property name="serviceInterface" value="com.spring.service.AccountService" />
		<property name="registryPort" value="1199" />
	</bean>

接口类:

package com.spring.service;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;

import com.spring.model.Account;

public interface AccountService extends Remote {

	public void insert(Account account) throws RemoteException;

	public List<Account> getAccount(String name) throws RemoteException;

}

实现类:

package com.spring.service.impl;

import java.rmi.RemoteException;
import java.util.List;

import javax.annotation.Resource;

import com.spring.dao.AccountDao;
import com.spring.model.Account;
import com.spring.service.AccountService;

public class AccountServiceImpl implements AccountService {

	private AccountDao accountDao;

	@Override
	public void insert(Account account) throws RemoteException {

		this.accountDao.insert(account);
	}

	@Override
	public List<Account> getAccount(String name) throws RemoteException {
		return null;
	}

	@Resource(name = "accountDaoImpl")
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

}


其他机器或项目客户端:需要在spring环境中

	<bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
		<property name="serviceUrl" value="rmi://127.0.0.1:1199/AccountService" />
		<property name="serviceInterface" value="com.spring.service.AccountService" />
	</bean>

	<bean id="simpleRMI" class="com.ccl.util.SimpleObject">
		<property name="accountService" ref="accountService" />
	</bean>


应用类:

package com.ccl.util;

import java.rmi.RemoteException;

import com.spring.model.Account;
import com.spring.service.AccountService;

public class SimpleObject {

	private AccountService accountService;

	public AccountService getAccountService() {
		return accountService;
	}

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	public void insert() {

		try {
			Account a = new Account();
			a.setName("client");
			accountService.insert(a);
		} catch (RemoteException e) {
			e.printStackTrace();
		}

	}

}




调用:

		ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");

		SimpleObject so = (SimpleObject) ac.getBean("simpleRMI");
 
		so.insert();

从上可以看到:

要用到两个Service端的类,所以可以打成jar包加进项目就OK。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值