diff --git a/whatsmars-spring/pom.xml b/whatsmars-spring/pom.xml
index 4b0cc4872cfb17dac9a696a856f3b82c36549dd3..8ab96265971d082f0fafff54404f13b29171e4ad 100644
--- a/whatsmars-spring/pom.xml
+++ b/whatsmars-spring/pom.xml
@@ -25,16 +25,7 @@
org.springframework
- spring-tx
-
-
-
- org.springframework
- spring-webmvc
-
-
- javax.servlet
- javax.servlet-api
+ spring-aop
@@ -46,18 +37,6 @@
aspectjtools
-
- log4j
- log4j
-
-
- org.slf4j
- slf4j-api
-
-
- org.slf4j
- jcl-over-slf4j
-
ch.qos.logback
logback-classic
@@ -66,6 +45,7 @@
org.springframework
spring-test
+ test
diff --git a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/Monitor.java b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/Monitor.java
index a48afdc0d64fe4bd09e14c7f0ed5dae186e29d60..45ab314ae62bfd74bb6ec16ecaf7edfd57cb6ba0 100644
--- a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/Monitor.java
+++ b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/Monitor.java
@@ -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();
}
diff --git a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/MonitorAspect.java b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/MonitorAspect.java
index bf4fef28add99d67fb79ae333a2c404bbb664136..8e57a6d43532a0db2446ff2e338911f4c8a198f7 100644
--- a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/MonitorAspect.java
+++ b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/MonitorAspect.java
@@ -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;
- }
}
diff --git a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/SystemLog.java b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/SystemLog.java
deleted file mode 100644
index faa15f89076378af7c4dc69cc69cc1f7105621cb..0000000000000000000000000000000000000000
--- a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/SystemLog.java
+++ /dev/null
@@ -1,13 +0,0 @@
-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();
-
-}
diff --git a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/SystemLogAspect.java b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/SystemLogAspect.java
deleted file mode 100644
index 2e32c417c42747f42e1a9274ddb61a4b5a30cfde..0000000000000000000000000000000000000000
--- a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/SystemLogAspect.java
+++ /dev/null
@@ -1,103 +0,0 @@
-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;
- }
-}
diff --git a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/demo/Application.java b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/demo/Application.java
new file mode 100644
index 0000000000000000000000000000000000000000..a4b4885fd52c197905e9965acf1455a51bca3c5d
--- /dev/null
+++ b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/demo/Application.java
@@ -0,0 +1,23 @@
+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();
+ }
+}
diff --git a/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/demo/DemoService.java b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/demo/DemoService.java
new file mode 100644
index 0000000000000000000000000000000000000000..9f649b51b094f708368505e4ac21761244b5d69e
--- /dev/null
+++ b/whatsmars-spring/src/main/java/org/hongxi/whatsmars/spring/aspect/demo/DemoService.java
@@ -0,0 +1,19 @@
+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);
+ }
+ }
+}
diff --git a/whatsmars-spring/src/main/resources/spring-context.xml b/whatsmars-spring/src/main/resources/spring-context.xml
index f8db47602ce69452bc8ac401ab8e6f81e3e389c7..c66a17b0b220b1456de20dff16eb7c6bbd76780d 100644
--- a/whatsmars-spring/src/main/resources/spring-context.xml
+++ b/whatsmars-spring/src/main/resources/spring-context.xml
@@ -15,8 +15,8 @@
-
-
+
+
diff --git a/whatsmars-spring/src/test/resources/logback.xml b/whatsmars-spring/src/test/resources/logback.xml
deleted file mode 100644
index 4fd5eafb01852bf6208b9420425586ed23423a95..0000000000000000000000000000000000000000
--- a/whatsmars-spring/src/test/resources/logback.xml
+++ /dev/null
@@ -1,19 +0,0 @@
-
-
-
-
-
-
-
- %d %5p %40.40c:%4L - %m%n
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file