01.
package
spring.invoker.service;
02.
03.
import
spring.invoker.domain.UserInfo;
04.
05.
/**
06.
* spring http invoker例子
07.
* @author steven
08.
*
09.
*/
10.
public
interface
UserService {
11.
/**
12.
* 根據(jù)用戶名,獲取用戶信息
13.
* @param userName 用戶名
14.
* @return 返回用戶信息
15.
*/
16.
public
UserInfo getUserInfobyName(String userName);
17.
18.
19.
20.
/**
21.
* 根據(jù)用戶名,獲取用戶郵箱
22.
* @param userName 用戶名
23.
* @return 返回用戶郵箱
24.
*/
25.
public
String getUserEmailbyName(String userName);
26.
}
01.
package
spring.invoker.service.impl;
02.
03.
import
spring.invoker.domain.UserInfo;
04.
import
spring.invoker.service.UserService;
05.
06.
/**
07.
* spring http invoker例子
08.
* @author steven
09.
*
10.
*/
11.
public
class
UserServiceImpl
implements
UserService{
12.
13.
14.
/**
15.
* 根據(jù)用戶名,獲取用戶信息
16.
* @param userName 用戶名
17.
* @return 返回用戶信息
18.
*/
19.
public
UserInfo getUserInfobyName(String userName){
20.
UserInfo userInfo =
new
UserInfo();
21.
userInfo.setUserName(userName);
22.
userInfo.setEmail(
"xxx@site.com"
);
23.
24.
return
userInfo;
25.
}
26.
27.
28.
29.
/**
30.
* 根據(jù)用戶名,獲取用戶郵箱
31.
* @param userName 用戶名
32.
* @return 返回用戶郵箱
33.
*/
34.
public
String getUserEmailbyName(String userName){
35.
return
userName +
"的郵箱地址為:xxx.site.com"
;
36.
}
37.
}
01.
package
spring.invoker.domain;
02.
03.
import
java.io.Serializable;
04.
05.
/**
06.
* 用戶基本信息
07.
* @author steven
08.
*
09.
*/
10.
public
class
UserInfo
implements
Serializable {
11.
private
static
final
long
serialVersionUID = 1L;
12.
private
String userName;
13.
private
String email;
14.
15.
public
String getUserName() {
16.
return
userName;
17.
}
18.
public
void
setUserName(String userName) {
19.
this
.userName = userName;
20.
}
21.
public
String getEmail() {
22.
return
email;
23.
}
24.
public
void
setEmail(String email) {
25.
this
.email = email;
26.
}
27.
28.
}
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
03.
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
04.
xmlns:p
=
"http://www.springframework.org/schema/p"
05.
xmlns:context
=
"http://www.springframework.org/schema/context"
06.
xsi:schemaLocation="
07.
http://www.springframework.org/schema/beans
08.
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
09.
http://www.springframework.org/schema/context
10.
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
11.
12.
<!-- 交給Spring管理,bean的名稱是:userService -->
13.
<
bean
id
=
"userService"
class
=
"spring.invoker.service.impl.UserServiceImpl"
/>
14.
15.
<!-- 這個(gè)配置,就是把userService接口,提供給遠(yuǎn)程調(diào)用 -->
16.
<
bean
id
=
"httpService"
17.
class
=
"org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"
>
18.
<
property
name
=
"service"
>
19.
<
ref
bean
=
"userService"
/>
20.
</
property
>
21.
<
property
name
=
"serviceInterface"
22.
value
=
"spring.invoker.service.UserService"
>
23.
</
property
>
24.
</
bean
>
25.
26.
<!-- 遠(yuǎn)程服務(wù)的URL -->
27.
<
bean
28.
class
=
"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
>
29.
<
property
name
=
"mappings"
>
30.
<
props
>
31.
<
prop
key
=
"/test"
>httpService</
prop
>
32.
</
props
>
33.
</
property
>
34.
</
bean
>
35.
36.
</
beans
>
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<
web-app
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
03.
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:web
=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
04.
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
05.
id
=
"WebApp_ID"
version
=
"2.5"
>
06.
<
display-name
>Spring3.0 Test Demo</
display-name
>
07.
<
context-param
>
08.
<
param-name
>contextConfigLocation</
param-name
>
09.
<
param-value
>
10.
/WEB-INF/dispatcher-service.xml
11.
/WEB-INF/dispatcher-servlet.xml
12.
</
param-value
>
13.
</
context-param
>
14.
<
listener
>
15.
<
listener-class
>
16.
org.springframework.web.context.ContextLoaderListener
17.
</
listener-class
>
18.
</
listener
>
19.
<
servlet
>
20.
<
servlet-name
>dispatcher</
servlet-name
>
21.
<
servlet-class
>
22.
org.springframework.web.servlet.DispatcherServlet
23.
</
servlet-class
>
24.
<
load-on-startup
>1</
load-on-startup
>
25.
</
servlet
>
26.
<
servlet-mapping
>
27.
<
servlet-name
>dispatcher</
servlet-name
>
28.
<
url-pattern
>/</
url-pattern
>
29.
</
servlet-mapping
>
30.
</
web-app
>
01.
<
bean
id
=
"httpService"
02.
class
=
"org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"
>
03.
<
property
name
=
"serviceUrl"
>
04.
<
value
>
05.
http://${serviceName}:${port}/${contextPath}/test
06.
</
value
>
07.
</
property
>
08.
<
property
name
=
"serviceInterface"
value
=
"spring.invoker.service.UserService"
>
09.
</
property
>
10.
</
bean
>
01.
package
spring.invoker;
02.
03.
import
javax.annotation.Resource;
04.
05.
import
org.springframework.stereotype.Controller;
06.
import
org.springframework.web.bind.annotation.RequestMapping;
07.
08.
import
spring.invoker.service.UserService;
09.
10.
@Controller
11.
public
class
InvokerAction {
12.
@Resource
private
UserService httpService;
13.
14.
@RequestMapping
(value=
"/httpTest"
)
15.
public
void
testInvoker(){
16.
System.out.println(httpService.getUserEmailbyName(
"xxx"
));
17.
}
18.
19.
}
聯(lián)系客服