Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
caopu16
whatsmars
提交
42ea222b
W
whatsmars
项目概览
caopu16
/
whatsmars
与 Fork 源项目一致
Fork自
武汉红喜 / whatsmars
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
whatsmars
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
42ea222b
编写于
12月 16, 2018
作者:
武汉红喜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MonitorAspect demo
上级
5b880b59
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
59 addition
and
175 deletion
+59
-175
whatsmars-spring/pom.xml
whatsmars-spring/pom.xml
+2
-22
whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/Monitor.java
...main/java/org/hongxi/whatsmars/spring/aspect/Monitor.java
+4
-6
whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/MonitorAspect.java
...ava/org/hongxi/whatsmars/spring/aspect/MonitorAspect.java
+9
-10
whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/SystemLog.java
...in/java/org/hongxi/whatsmars/spring/aspect/SystemLog.java
+0
-13
whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/SystemLogAspect.java
...a/org/hongxi/whatsmars/spring/aspect/SystemLogAspect.java
+0
-103
whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/demo/Application.java
.../org/hongxi/whatsmars/spring/aspect/demo/Application.java
+23
-0
whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/demo/DemoService.java
.../org/hongxi/whatsmars/spring/aspect/demo/DemoService.java
+19
-0
whatsmars-spring/src/main/resources/spring-context.xml
whatsmars-spring/src/main/resources/spring-context.xml
+2
-2
whatsmars-spring/src/test/resources/logback.xml
whatsmars-spring/src/test/resources/logback.xml
+0
-19
未找到文件。
whatsmars-spring/pom.xml
浏览文件 @
42ea222b
...
...
@@ -25,16 +25,7 @@
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-tx
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-webmvc
</artifactId>
</dependency>
<dependency>
<groupId>
javax.servlet
</groupId>
<artifactId>
javax.servlet-api
</artifactId>
<artifactId>
spring-aop
</artifactId>
</dependency>
<dependency>
...
...
@@ -46,18 +37,6 @@
<artifactId>
aspectjtools
</artifactId>
</dependency>
<dependency>
<groupId>
log4j
</groupId>
<artifactId>
log4j
</artifactId>
</dependency>
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
slf4j-api
</artifactId>
</dependency>
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
jcl-over-slf4j
</artifactId>
</dependency>
<dependency>
<groupId>
ch.qos.logback
</groupId>
<artifactId>
logback-classic
</artifactId>
...
...
@@ -66,6 +45,7 @@
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-test
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
...
...
whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/Monitor.java
浏览文件 @
42ea222b
...
...
@@ -11,12 +11,10 @@ import java.lang.annotation.Target;
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Target
({
ElementType
.
METHOD
,
ElementType
.
CONSTRUCTOR
})
public
@interface
Monitor
{
String
DEFAULT_TAG_NAME
=
"@@USE_METHOD_NAME"
;
String
tag
()
default
"@@USE_METHOD_NAME"
;
String
message
()
default
""
;
boolean
heart
()
default
false
;
/**
* 标识每一个方法,全局唯一
*/
String
tag
();
}
whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/MonitorAspect.java
浏览文件 @
42ea222b
...
...
@@ -3,42 +3,41 @@ package org.hongxi.whatsmars.spring.aspect;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Component
;
/**
* Created by shenhongxi on 2016/8/10.
*/
@Component
@Aspect
public
class
MonitorAspect
{
private
String
tagPrefix
;
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
MonitorAspect
.
class
)
;
@Around
(
value
=
"execution(* *(..)) && @annotation(monitor)"
,
argNames
=
"pjp,monitor"
)
public
Object
doUmpLogging
(
ProceedingJoinPoint
pjp
,
Monitor
monitor
)
throws
Throwable
{
// String tag = monitor.tag();
// boolean heart = monitor.heart();
String
tag
=
monitor
.
tag
();
long
start
=
System
.
currentTimeMillis
();
// record invocation (times)
logger
.
info
(
"{} start time: {}"
,
tag
,
start
);
Object
obj
=
null
;
try
{
obj
=
pjp
.
proceed
();
}
catch
(
Exception
e
)
{
// record error
logger
.
error
(
"{} invoke error"
,
tag
,
e
);
throw
e
;
}
finally
{
long
end
=
System
.
currentTimeMillis
();
// record time -> end - start
logger
.
info
(
"{} cost time: {}"
,
tag
,
end
-
start
);
}
return
obj
;
}
public
String
getTagPrefix
()
{
return
tagPrefix
;
}
public
void
setTagPrefix
(
String
tagPrefix
)
{
this
.
tagPrefix
=
tagPrefix
;
}
}
whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/SystemLog.java
已删除
100644 → 0
浏览文件 @
5b880b59
package
org.hongxi.whatsmars.spring.aspect
;
import
java.lang.annotation.*
;
@Target
({
ElementType
.
PARAMETER
,
ElementType
.
METHOD
})
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Documented
public
@interface
SystemLog
{
String
description
()
default
""
;
int
logType
();
}
whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/SystemLogAspect.java
已删除
100644 → 0
浏览文件 @
5b880b59
package
org.hongxi.whatsmars.spring.aspect
;
import
org.apache.log4j.Logger
;
import
org.aspectj.lang.JoinPoint
;
import
org.aspectj.lang.annotation.After
;
import
org.aspectj.lang.annotation.AfterThrowing
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
javax.servlet.http.HttpServletRequest
;
import
java.lang.reflect.Method
;
@Aspect
@Component
public
class
SystemLogAspect
{
private
static
Logger
logger
=
Logger
.
getLogger
(
SystemLog
.
class
);
// Controller层切点
@Pointcut
(
"@annotation(org.hongxi.whatsmars.spring.aspect.SystemLog)"
)
public
void
controllerAspect
()
{
}
// 操作记录
@After
(
"controllerAspect()"
)
public
void
doAfter
(
JoinPoint
joinPoint
)
{
HttpServletRequest
request
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getRequest
();
// 读取session中的用户
// User user = SessionUtils.getUserFromSession(request);
// if(null == user){
// logger.info("未登录日志不记录");
// return;
// }
try
{
Object
[]
args
=
getLogMethodDescription
(
joinPoint
);
// *========数据库日志=========*//
// logService.saveLog(user.getId(), user.getAccount(), Integer.valueOf(args[1].toString()),
// IpUtils.getIpAddr(request), args[0].toString(),
// user.getManufacturerId(), reqDescription(request));
}
catch
(
Exception
e
)
{
logger
.
error
(
e
);
e
.
printStackTrace
();
}
}
// 异常记录
@AfterThrowing
(
pointcut
=
"controllerAspect()"
,
throwing
=
"exception"
)
public
void
doException
(
JoinPoint
joinPoint
,
Exception
exception
)
{
HttpServletRequest
request
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getRequest
();
// 读取session中的用户
// User user = SessionUtils.getUserFromSession(request);
// if(null == user){
// logger.info("未登录日志不记录");
// return;
// }
try
{
Object
[]
args
=
getLogMethodDescription
(
joinPoint
);
// *========数据库日志=========*//
// logService.saveLog(user.getId(), user.getAccount(), LogType.EXCEPTION_LOG,
// IpUtils.getIpAddr(request), args[0].toString() + "-->" + exception.getMessage(),
// user.getManufacturerId(), reqDescription(request));
}
catch
(
Exception
e
)
{
// 记录本地异常日志
logger
.
error
(
e
);
e
.
printStackTrace
();
}
}
/**
* 获取注解中对方法的描述信息 用于Controller层注解 切点
*/
@SuppressWarnings
(
"rawtypes"
)
public
static
Object
[]
getLogMethodDescription
(
JoinPoint
joinPoint
)
throws
Exception
{
String
targetName
=
joinPoint
.
getTarget
().
getClass
().
getName
();
String
methodName
=
joinPoint
.
getSignature
().
getName
();
Object
[]
arguments
=
joinPoint
.
getArgs
();
Class
<?>
targetClass
=
Class
.
forName
(
targetName
);
Method
[]
methods
=
targetClass
.
getMethods
();
Object
[]
result
=
new
Object
[
2
];
for
(
Method
method
:
methods
)
{
if
(
method
.
getName
().
equals
(
methodName
))
{
Class
[]
clazzs
=
method
.
getParameterTypes
();
if
(
clazzs
.
length
==
arguments
.
length
)
{
result
[
0
]
=
method
.
getAnnotation
(
SystemLog
.
class
).
description
();
result
[
1
]
=
method
.
getAnnotation
(
SystemLog
.
class
).
logType
();
break
;
}
}
}
return
result
;
}
}
whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/demo/Application.java
0 → 100644
浏览文件 @
42ea222b
package
org.hongxi.whatsmars.spring.aspect.demo
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.ComponentScan
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.EnableAspectJAutoProxy
;
/**
* Created by shenhongxi on 2018/12/16.
*/
@ComponentScan
(
basePackages
=
"org.hongxi.whatsmars.spring.aspect"
)
@EnableAspectJAutoProxy
@Configuration
public
class
Application
{
public
static
void
main
(
String
[]
args
)
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
ctx
.
register
(
Application
.
class
);
ctx
.
refresh
();
DemoService
demoService
=
ctx
.
getBean
(
DemoService
.
class
);
demoService
.
t
();
}
}
whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/demo/DemoService.java
0 → 100644
浏览文件 @
42ea222b
package
org.hongxi.whatsmars.spring.aspect.demo
;
import
org.hongxi.whatsmars.spring.aspect.Monitor
;
import
org.springframework.stereotype.Service
;
/**
* Created by shenhongxi on 2018/12/15.
*/
@Service
public
class
DemoService
{
@Monitor
(
tag
=
"spring.aspect.demo.DemoService.t"
)
public
void
t
()
{
for
(
int
i
=
0
;
i
<
10000
;
i
++)
{
if
(
i
<
9999
)
System
.
out
.
print
(
i
+
","
);
else
System
.
out
.
println
(
i
);
}
}
}
whatsmars-spring/src/main/resources/spring-context.xml
浏览文件 @
42ea222b
...
...
@@ -15,8 +15,8 @@
<property
name=
"cnName"
value=
"火星"
/>
</bean>
<!--
MQ发消息
线程池 -->
<bean
id=
"task
Mq
Executor"
class=
"org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
>
<!-- 线程池 -->
<bean
id=
"taskExecutor"
class=
"org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
>
<!-- 核心线程数 -->
<property
name=
"corePoolSize"
value=
"10"
/>
<!-- 最大线程数 -->
...
...
whatsmars-spring/src/test/resources/logback.xml
已删除
100644 → 0
浏览文件 @
5b880b59
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL> </contextListener> -->
<appender
name=
"console"
class=
"ch.qos.logback.core.ConsoleAppender"
>
<encoder>
<pattern>
%d %5p %40.40c:%4L - %m%n
</pattern>
</encoder>
</appender>
<logger
name=
"org.springframework.data.mongodb"
level=
"debug"
/>
<root
level=
"info"
>
<appender-ref
ref=
"console"
/>
</root>
</configuration>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录