Toggle navigation
Toggle navigation
This project
Loading...
Sign in
ufo
/
yohoufo-fore
·
Commits
Go to a project
GitLab
Go to group
Project
Activity
Files
Commits
Pipelines
0
Builds
0
Graphs
Milestones
Issues
0
Merge Requests
0
Members
Labels
Wiki
Forks
Network
Create a new issue
Download as
Email Patches
Plain Diff
Browse Files
Authored by
Lixiaodi
7 years ago
Commit
2d7588524c5c4919f4880029cbbb25c3c9ab5d3f
1 parent
49898faa
增加代理serviceCall
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
1 deletions
common/src/main/java/com/yohoufo/common/caller/UfoServiceCaller.java
web/src/main/resources/META-INF/spring/spring-web-context.xml
common/src/main/java/com/yohoufo/common/caller/UfoServiceCaller.java
0 → 100644
View file @
2d75885
package
com
.
yohoufo
.
common
.
caller
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.Method
;
import
java.util.HashMap
;
import
java.util.Map
;
import
org.apache.commons.collections.MapUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.context.event.ContextRefreshedEvent
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.util.ReflectionUtils
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
com.yoho.core.rest.exception.ServiceNotFoundException
;
public
class
UfoServiceCaller
implements
ApplicationListener
<
ContextRefreshedEvent
>
{
private
final
static
Logger
logger
=
LoggerFactory
.
getLogger
(
UfoServiceCaller
.
class
);
private
static
class
ServiceMethod
{
Object
bean
;
Method
method
;
ServiceMethod
(
Object
bean
,
Method
method
)
{
this
.
bean
=
bean
;
this
.
method
=
method
;
}
Object
call
(
Object
[]
args
)
throws
IllegalAccessException
,
IllegalArgumentException
,
InvocationTargetException
{
return
method
.
invoke
(
bean
,
args
);
}
}
private
Map
<
String
,
ServiceMethod
>
serviceMap
=
new
HashMap
<>();
public
Object
call
(
String
serviceMethod
,
Object
[]
param
)
{
logger
.
info
(
"call ufo service : {}"
,
serviceMethod
);
ServiceMethod
sm
=
serviceMap
.
get
(
serviceMethod
);
if
(
sm
==
null
)
{
throw
new
ServiceNotFoundException
(
"Not found service "
+
serviceMethod
,
serviceMethod
);
}
try
{
return
sm
.
call
(
param
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"service error : "
+
serviceMethod
,
e
);
return
null
;
}
}
@Override
public
void
onApplicationEvent
(
ContextRefreshedEvent
event
)
{
//查找bean
logger
.
info
(
"start to register ufo service info"
);
final
Map
<
String
/* bean name*/
,
Object
/*bean instance*/
>
serviceBeanMap
=
new
HashMap
<>();
ApplicationContext
applicationContext
=
event
.
getApplicationContext
();
serviceBeanMap
.
putAll
(
applicationContext
.
getBeansWithAnnotation
(
Controller
.
class
));
serviceBeanMap
.
putAll
(
applicationContext
.
getBeansWithAnnotation
(
RestController
.
class
));
//注册
if
(
MapUtils
.
isNotEmpty
(
serviceBeanMap
))
{
register
(
applicationContext
,
serviceBeanMap
);
}
}
private
void
register
(
ApplicationContext
applicationContext
,
Map
<
String
,
Object
>
serviceBeanMap
)
{
for
(
Object
bean
:
serviceBeanMap
.
values
())
{
// 获取每一个方法上的request mapping的value,可能为空
Method
[]
methods
=
ReflectionUtils
.
getAllDeclaredMethods
(
bean
.
getClass
());
for
(
Method
method
:
methods
)
{
RequestMapping
method_requestMappingAnno
=
method
.
getAnnotation
(
RequestMapping
.
class
);
String
[]
params
=
method_requestMappingAnno
.
params
();
String
tag
=
"method="
;
if
(
params
!=
null
&&
params
.
length
==
1
&&
params
[
1
].
trim
().
startsWith
(
tag
))
{
String
methodStr
=
params
[
1
].
trim
().
substring
(
tag
.
length
());
if
(
StringUtils
.
isNotBlank
(
methodStr
))
{
serviceMap
.
put
(
methodStr
,
new
ServiceMethod
(
bean
,
method
));
}
}
}
logger
.
info
(
"Register all ufo service for controller:{} success"
,
bean
);
}
}
}
...
...
web/src/main/resources/META-INF/spring/spring-web-context.xml
View file @
2d75885
...
...
@@ -86,7 +86,8 @@
<property
name=
"scanSwitch"
value=
"${yoho.api.docs.switch:off}"
/>
</bean>
<!-- api文档配置 end -->
<bean
id=
"ufoServiceCaller"
class=
"com.yohoufo.common.caller.UfoServiceCaller"
>
<!--Spring mvc 拦截器-->
<mvc:interceptors>
...
...
Please
register
or
login
to post a comment