一、spring-aop.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
  8. http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd ">
  11. <!-- 启动@AspectJ支持 -->
  12. <aop:aspectj-autoproxy proxy-target-class="true"/>
  13. <context:component-scan base-package="hongmoshui.com.cnblogs.www.base.aop" />
  14. </beans>

二、spring-mybatis.xml文件和spring-mvc.xml文件中,分别导入spring-aop.xml的配置

spring-mybatis.xml【service和dao层的注解有效】:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  8. http://www.springframework.org/schema/mvc
  9. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  10.  
  11. <!--读取jdbc资源文件 -->
  12. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  13. <!-- 允许JVM参数覆盖 -->
  14. <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
  15. <!-- 忽略没有找到的资源文件 -->
  16. <property name="ignoreResourceNotFound" value="true" />
  17. <!-- 配置资源文件 -->
  18. <property name="locations">
  19. <list>
  20. <value>classpath:properties/jdbc.properties</value>
  21. </list>
  22. </property>
  23. </bean>
  24.  
  25. <!-- 配置数据源 -->
  26. <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
  27. <!-- 数据库驱动 -->
  28. <property name="driverClass" value="${master.jdbc.driver}" />
  29. <!-- 相应驱动的jdbcUrl -->
  30. <property name="jdbcUrl" value="${master.jdbc.url}" />
  31. <!-- 数据库的用户名 -->
  32. <property name="username" value="${master.jdbc.username}" />
  33. <!-- 数据库的密码 -->
  34. <property name="password" value="${master.jdbc.password}" />
  35. <!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
  36. <property name="idleConnectionTestPeriod" value="60" />
  37. <!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
  38. <property name="idleMaxAge" value="30" />
  39. <!-- 每个分区最大的连接数 -->
  40. <!-- 判断依据:请求并发数 -->
  41. <property name="maxConnectionsPerPartition" value="100" />
  42. <!-- 每个分区最小的连接数 -->
  43. <property name="minConnectionsPerPartition" value="5" />
  44. </bean>
  45. <!-- 扫描包 -->
  46. <context:component-scan base-package="hongmoshui.com.cnblogs.www.*.service.impl,hongmoshui.com.cnblogs.www.*.dao.impl" />
  47. <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
  48. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  49. <property name="dataSource" ref="dataSource" />
  50. <!-- 自动扫描mapping.xml文件 -->
  51. <property name="mapperLocations" value="classpath:mappers*/*Mapper.xml"></property>
  52. </bean>
  53.  
  54. <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
  55. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  56. <property name="basePackage" value="hongmoshui.com.cnblogs.www.base.dao.impl,hongmoshui.com.cnblogs.www.work.dao" />
  57. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  58. </bean>
  59.  
  60. <!-- 定义事务管理器 -->
  61. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  62. <property name="dataSource" ref="dataSource" />
  63. </bean>
  64. <!-- 导入aop配置 -->
  65. <import resource="classpath*:/spring/spring-aop.xml" />
  66. </beans>

spring-mvc.xml【controller层的注解有效】:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  13. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd ">
  14.  
  15. <!-- 配置注解驱动 -->
  16. <mvc:annotation-driven/>
  17.  
  18. <!-- 扫描controller包 -->
  19. <context:component-scan base-package="hongmoshui.com.cnblogs.www.*.controller"/>
  20.  
  21. <!-- 配置视图解析器 -->
  22. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  23. <property name="prefix" value="/WEB-INF/jsp/"/>
  24. <property name="suffix" value=".jsp"/>
  25. </bean>
  26. <!-- 对静态资源文件的访问 ,不支持访问WEB-INF目录 -->
  27. <!-- <mvc:default-servlet-handler/> -->
  28. <!-- 对静态资源文件的访问 ,可以访问任何目录,包括访问WEB-INF目录 -->
  29. <mvc:resources mapping="/resources/**" location="/resources/" />
  30. <!-- 导入aop配置 -->
  31. <import resource="classpath*:/spring/spring-aop.xml" />
  32. </beans>

三、自定义注解类【连接点】

  1. package hongmoshui.com.cnblogs.www.work.annotation;
  2.  
  3. import java.lang.annotation.Documented;
  4. import java.lang.annotation.ElementType;
  5. import java.lang.annotation.Inherited;
  6. import java.lang.annotation.Retention;
  7. import java.lang.annotation.RetentionPolicy;
  8. import java.lang.annotation.Target;
  9.  
  10. @Target(ElementType.METHOD)
  11. @Retention(RetentionPolicy.RUNTIME)
  12. @Documented
  13. @Inherited
  14. public @interface Log
  15. {
  16. /**
  17. * 方法名
  18. * @author 洪墨水
  19. */
  20. public String name() default "";
  21.  
  22. /**
  23. * 描述
  24. * @author 洪墨水
  25. */
  26. public String description() default "no description";
  27. }

参数定义:

@Target 注解

功能:指明了修饰的这个注解的使用范围,即被描述的注解可以用在哪里。

ElementType的取值包含以下几种:

  • TYPE:类,接口或者枚举

  • FIELD:域,包含枚举常量

  • METHOD:方法

  • PARAMETER:参数

  • CONSTRUCTOR:构造方法

  • LOCAL_VARIABLE:局部变量

  • ANNOTATION_TYPE:注解类型

  • PACKAGE:包

@Retention 注解

功能:指明修饰的注解的生存周期,即会保留到哪个阶段。

RetentionPolicy的取值包含以下三种:

  • SOURCE:源码级别保留,编译后即丢弃。

  • CLASS:编译级别保留,编译后的class文件中存在,在jvm运行时丢弃,这是默认值。

  • RUNTIME: 运行级别保留,编译后的class文件中存在,在jvm运行时保留,可以被反射调用。

@Documented 注解

功能:指明修饰的注解,可以被例如javadoc此类的工具文档化,只负责标记,没有成员取值。

@Inherited注解

功能:允许子类继承父类中的注解。

四、自定义切面类

  1. package hongmoshui.com.cnblogs.www.base.aop;
  2.  
  3. import java.io.IOException;
  4. import java.lang.reflect.Method;
  5. import java.util.Calendar;
  6.  
  7. import org.apache.log4j.Logger;
  8. import org.aspectj.lang.ProceedingJoinPoint;
  9. import org.aspectj.lang.annotation.After;
  10. import org.aspectj.lang.annotation.Around;
  11. import org.aspectj.lang.annotation.Aspect;
  12. import org.aspectj.lang.annotation.Pointcut;
  13. import org.springframework.stereotype.Component;
  14.  
  15. import com.alibaba.fastjson.JSON;
  16.  
  17. import hongmoshui.com.cnblogs.www.base.utils.TimeUtil;
  18. import hongmoshui.com.cnblogs.www.work.annotation.Log;
  19.  
  20. /**
  21. * 日志注解的切面类
  22. * @author 洪墨水
  23. */
  24. @Aspect
  25. @Component
  26. public class LogAspect
  27. {
  28. /**
  29. * 日志记录log
  30. */
  31. public transient Logger log = Logger.getLogger(getClass());
  32.  
  33. @Around(value = "@annotation(l)", argNames = "l")
  34. public Object aroundBase(ProceedingJoinPoint point, Log l)
  35. {
  36. RecordMessage recordMessage = new RecordMessage();
  37. Long startTime = System.currentTimeMillis();
  38. Object object = null;
  39.  
  40. try
  41. {
  42. /* 记录下当前时间 作为请求起始时间 */
  43. recordMessage.setRequestTime(TimeUtil.getLongDateString(Calendar.getInstance().getTime()));
  44.  
  45. /* 获取请求的信息 */
  46. getRequestParams(point, recordMessage);
  47.  
  48. /* 执行请求的方法 */
  49. object = point.proceed();
  50.  
  51. /* 记录下当前时间 作为响应时间 */
  52. recordMessage.setResponseTime(TimeUtil.getLongDateString(Calendar.getInstance().getTime()));
  53.  
  54. /* 记录响应参数 */
  55. recordMessage.setResponseParames(object == null ? "" : JSON.toJSONString(object));
  56. }
  57. catch (Throwable e)
  58. {
  59. log.warn("proceed GW Interface throwable, ", e);
  60. }
  61. finally
  62. {
  63. Long endTime = System.currentTimeMillis();
  64. recordMessage.setCostTime(endTime - startTime);
  65. /* 记录接口日志 */
  66. log.info(recordMessage.toString());
  67. }
  68. return object;
  69. }
  70.  
  71. /**
  72. * 从切点中解析出该切点对应的方法
  73. * @param point point
  74. * @throws ClassNotFoundException
  75. * @throws IOException
  76. * @author 洪墨水
  77. */
  78. private void getRequestParams(ProceedingJoinPoint point, RecordMessage recordMessage) throws ClassNotFoundException, IOException
  79. {
  80. /* 类名 */
  81. String targetObject = point.getTarget().getClass().getName();
  82. /* 方法名 */
  83. String methodName = point.getSignature().getName();
  84.  
  85. recordMessage.setTargetObject(targetObject);
  86. recordMessage.setMethod(methodName);
  87.  
  88. Object[] args = point.getArgs();
  89.  
  90. Class<?> targetClass = Class.forName(targetObject);
  91.  
  92. Method[] methods = targetClass.getMethods();
  93.  
  94. StringBuilder requestBuilder = new StringBuilder(0);
  95.  
  96. /**
  97. * 遍历方法 获取能与方法名相同且请求参数个数也相同的方法
  98. */
  99. for (Method method : methods)
  100. {
  101. if (!method.getName().equals(methodName))
  102. {
  103. continue;
  104. }
  105.  
  106. Class<?>[] classes = method.getParameterTypes();
  107.  
  108. if (classes.length != args.length)
  109. {
  110. continue;
  111. }
  112.  
  113. for (int index = 0; index < classes.length; index++)
  114. {
  115. requestBuilder.append(args[index] == null ? "" : JSON.toJSONString(args[index]));
  116. }
  117.  
  118. recordMessage.setRequestParames(requestBuilder.toString());
  119. }
  120.  
  121. return;
  122. }
  123.  
  124. @Pointcut(value = "execution(* hongmoshui.com.cnblogs.www.work.service.impl.*.*(..))")
  125. public void getValuePointCut()
  126. {
  127. }
  128.  
  129. @After(value = "getValuePointCut()")
  130. public void after()
  131. {
  132. System.out.println("方法执行结束...");
  133. }
  134.  
  135. }
  136.  
  137. /**
  138. * 日志记录对象
  139. * @author 洪墨水
  140. */
  141. class RecordMessage
  142. {
  143. /**
  144. * 请求的方法
  145. */
  146. private String method;
  147.  
  148. /**
  149. * 请求方法所在的对象
  150. */
  151. private String targetObject;
  152.  
  153. /**
  154. * 请求参数
  155. */
  156. private String requestParames;
  157.  
  158. /**
  159. * 请求时间
  160. */
  161. private String requestTime;
  162.  
  163. /**
  164. * 响应时间
  165. */
  166. private String responseTime;
  167.  
  168. /**
  169. * 响应参数
  170. */
  171. private String responseParames;
  172.  
  173. /**
  174. * 请求的来源IP
  175. */
  176. private String requestIp;
  177.  
  178. /**
  179. * 方法执行的耗时,毫秒
  180. */
  181. private long costTime;
  182.  
  183. /**
  184. * 请求的方法
  185. */
  186. public String getMethod()
  187. {
  188. return method;
  189. }
  190.  
  191. /**
  192. * 请求的方法
  193. */
  194. public void setMethod(String method)
  195. {
  196. this.method = method;
  197. }
  198.  
  199. /**
  200. * 请求方法所在的对象
  201. */
  202. public String getTargetObject()
  203. {
  204. return targetObject;
  205. }
  206.  
  207. /**
  208. * 请求方法所在的对象
  209. */
  210. public void setTargetObject(String targetObject)
  211. {
  212. this.targetObject = targetObject;
  213. }
  214.  
  215. /**
  216. * 请求参数
  217. */
  218. public String getRequestParames()
  219. {
  220. return requestParames;
  221. }
  222.  
  223. /**
  224. * 请求参数
  225. */
  226. public void setRequestParames(String requestParames)
  227. {
  228. this.requestParames = requestParames;
  229. }
  230.  
  231. /**
  232. * 请求时间
  233. */
  234. public String getRequestTime()
  235. {
  236. return requestTime;
  237. }
  238.  
  239. /**
  240. * 请求时间
  241. */
  242. public void setRequestTime(String requestTime)
  243. {
  244. this.requestTime = requestTime;
  245. }
  246.  
  247. /**
  248. * 响应时间
  249. */
  250. public String getResponseTime()
  251. {
  252. return responseTime;
  253. }
  254.  
  255. /**
  256. * 响应时间
  257. */
  258. public void setResponseTime(String responseTime)
  259. {
  260. this.responseTime = responseTime;
  261. }
  262.  
  263. /**
  264. * 响应参数
  265. */
  266. public String getResponseParames()
  267. {
  268. return responseParames;
  269. }
  270.  
  271. /**
  272. * 响应参数
  273. */
  274. public void setResponseParames(String responseParames)
  275. {
  276. this.responseParames = responseParames;
  277. }
  278.  
  279. /**
  280. * 请求的来源IP
  281. */
  282. public String getRequestIp()
  283. {
  284. return requestIp;
  285. }
  286.  
  287. /**
  288. * 请求的来源IP
  289. */
  290. public void setRequestIp(String requestIp)
  291. {
  292. this.requestIp = requestIp;
  293. }
  294.  
  295. /**
  296. * 方法执行的耗时,毫秒
  297. */
  298. public long getCostTime()
  299. {
  300. return costTime;
  301. }
  302.  
  303. /**
  304. * 方法执行的耗时,毫秒
  305. */
  306. public void setCostTime(long costTime)
  307. {
  308. this.costTime = costTime;
  309. }
  310.  
  311. /**
  312. * toString
  313. * @return String String
  314. * @author 洪墨水
  315. */
  316. @Override
  317. public String toString()
  318. {
  319. StringBuilder sBuilder = new StringBuilder(0);
  320.  
  321. sBuilder.append("method=").append(method);
  322. sBuilder.append(", targetObject=").append(targetObject);
  323. sBuilder.append(", requestParames=").append(requestParames);
  324. sBuilder.append(", requestTime=").append(requestTime);
  325. sBuilder.append(", responseTime=").append(responseTime);
  326. sBuilder.append(", responseParames=").append(responseParames);
  327. sBuilder.append(", requestIp=").append(requestIp);
  328. sBuilder.append(", costTime=").append(costTime);
  329.  
  330. return sBuilder.toString();
  331. }
  332.  
  333. }

时间工具类:

  1. package hongmoshui.com.cnblogs.www.base.utils;
  2.  
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import java.util.TimeZone;
  8.  
  9. import org.apache.commons.lang3.StringUtils;
  10.  
  11. /**
  12. * 时间工具类
  13. * @author 洪墨水
  14. */
  15. public class TimeUtil
  16. {
  17. private static int i = 0;
  18.  
  19. /**
  20. * 日期转换成 yyyy-MM-dd HH:mm:ss+时区形式 如:2019-04-24 19:18:03+0800
  21. * @param date 日期
  22. * @return 按格式返回日期
  23. * @author 洪墨水
  24. */
  25. public static String getDateString(Date date)
  26. {
  27. if (date == null)
  28. {
  29. return null;
  30. }
  31.  
  32. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
  33.  
  34. return dateFormat.format(date);
  35. }
  36.  
  37. /**
  38. * 日期转换成 yyyy-MM-dd HH:mm:ss 形式 如:2019-04-24 19:18:03
  39. * @param date 日期
  40. * @return 按格式返回日期
  41. * @author 洪墨水
  42. */
  43. public static String dateToString()
  44. {
  45. Calendar now = Calendar.getInstance();
  46. now.set(Calendar.SECOND, now.get(Calendar.SECOND) + i);
  47.  
  48. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  49. i++;
  50. if (i > 1)
  51. {
  52. i = 0;
  53. }
  54. return dateFormat.format(now.getTime());
  55. }
  56.  
  57. /**
  58. * 日期转换成 yyyy-MM-dd HH:mm:ss+时区形式 如:2019-04-24 19:18:03+0800
  59. * @param date 日期
  60. * @return 按格式返回日期
  61. * @author 洪墨水
  62. */
  63. public static String getDateString(String date)
  64. {
  65. if (date == null)
  66. {
  67. return null;
  68. }
  69. return TimeUtil.getDateString(toDate(date, "yyyy-MM-dd HH:mm:ss"));
  70. }
  71.  
  72. /**
  73. *
  74. * String转 Date
  75. * @param date 日期
  76. * @param format 格式
  77. * @return 转换后日期
  78. * @author 洪墨水
  79. */
  80. public static Date toDate(String date, String format)
  81. {
  82. if (StringUtils.isEmpty(format))
  83. {
  84. format = "yyyy-MM-dd HH:mm:ss";
  85. }
  86. SimpleDateFormat df = new SimpleDateFormat(format);
  87. try
  88. {
  89. return df.parse(date);
  90. }
  91. catch (ParseException e)
  92. {
  93. return new Date();
  94. }
  95. }
  96.  
  97. /**
  98. *
  99. * String转 Date
  100. * @param date 日期
  101. * @param format 格式
  102. * @return 转换后日期
  103. * @author 洪墨水
  104. */
  105. public static Date toDate(String date)
  106. {
  107. return toDate(date, null);
  108. }
  109.  
  110. /**
  111. * 将带时区的时间字符串转换成系统所在时区的时间 如:2019-04-24 19:18:03+0700 转换到东八区的时间为:2019-04-24
  112. * 20:18:03
  113. * @param dateformat 日期格式
  114. * @return 系统所在时区的时间
  115. * @throws ParseException [参数说明]
  116. * @author 洪墨水
  117. */
  118. public static Date convertToLocalDate(String dateformat) throws ParseException
  119. {
  120. SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
  121.  
  122. Date date = dFormat.parse(dateformat);
  123.  
  124. Calendar calendar = Calendar.getInstance();
  125. calendar.setTime(date);
  126. calendar.setTimeZone(TimeZone.getDefault());
  127.  
  128. return calendar.getTime();
  129. }
  130.  
  131. /**
  132. * <获取当前时间N小时后的时间 >
  133. * @param n 小时
  134. * @return 日期
  135. * @author 洪墨水
  136. */
  137. public static Date getNextDate(int n)
  138. {
  139. Calendar now = Calendar.getInstance();
  140. now.set(Calendar.HOUR_OF_DAY, now.get(Calendar.HOUR_OF_DAY) + n);
  141. return now.getTime();
  142. }
  143.  
  144. /**
  145. * <获取当前时间N天后的凌晨 >
  146. * @param n 天
  147. * @return 日期
  148. * @author 洪墨水
  149. */
  150. public static Date getMorningNextDate(int n)
  151. {
  152. Calendar now = Calendar.getInstance();
  153. now.set(Calendar.DATE, now.get(Calendar.DATE) + n);// 设置时间向前进n天
  154. now.set(Calendar.HOUR_OF_DAY, 0);
  155. now.set(Calendar.MINUTE, 0);
  156. now.set(Calendar.SECOND, 0);
  157. return now.getTime();
  158. }
  159.  
  160. /**
  161. * <获取当前时间N小时后的整点时间 >
  162. * @param n 小时
  163. * @return 日期
  164. * @author 洪墨水
  165. */
  166. public static Date getNextHour(int n)
  167. {
  168. Calendar now = Calendar.getInstance();
  169. now.set(Calendar.DATE, now.get(Calendar.DATE));
  170. now.set(Calendar.HOUR_OF_DAY, now.get(Calendar.HOUR_OF_DAY) + n);// 设置时间向前进n小时
  171. return now.getTime();
  172. }
  173.  
  174. /**
  175. * <获取当前时间N天后的凌晨【精确到毫秒】 >
  176. * @param n 前进天数
  177. * @return Date [日期]
  178. * @author 洪墨水
  179. */
  180. public static Date getMorningNextDateMillisecond(int n)
  181. {
  182. Calendar now = Calendar.getInstance();
  183. now.set(Calendar.DATE, now.get(Calendar.DATE) + n);// 设置时间向前进n天
  184. now.set(Calendar.HOUR_OF_DAY, 0);
  185. now.set(Calendar.MINUTE, 0);
  186. now.set(Calendar.SECOND, 0);
  187. now.set(Calendar.MILLISECOND, 0);
  188. return now.getTime();
  189. }
  190.  
  191. /**
  192. * 比较
  193. * @param date 日期
  194. * @return 是否是今天
  195. * @author 洪墨水
  196. */
  197. public static boolean checkLastDate(Date date)
  198. {
  199. Date d = new Date();
  200. Calendar current = Calendar.getInstance();
  201. current.setTime(date);
  202. Calendar start = Calendar.getInstance();
  203. Calendar end = Calendar.getInstance();
  204. start.set(Calendar.YEAR, current.get(Calendar.YEAR));
  205. start.set(Calendar.MONTH, current.get(Calendar.MONTH));
  206. start.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
  207. start.set(Calendar.HOUR_OF_DAY, 0);
  208. start.set(Calendar.MINUTE, 0);
  209. start.set(Calendar.SECOND, 0);
  210. end.set(Calendar.YEAR, current.get(Calendar.YEAR));
  211. end.set(Calendar.MONTH, current.get(Calendar.MONTH));
  212. end.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
  213. end.set(Calendar.HOUR_OF_DAY, 23);
  214. end.set(Calendar.MINUTE, 59);
  215. end.set(Calendar.SECOND, 59);
  216. if (d.after(start.getTime()) && d.before(end.getTime()))
  217. {
  218. return true;
  219. }
  220. return false;
  221. }
  222.  
  223. /**
  224. * 获取日期;格式:yyyy-MM-dd
  225. * @param date 日期
  226. * @return String yyyy-MM-dd格式的日期字符串
  227. * @author 洪墨水
  228. */
  229. public static String getDate(Date date)
  230. {
  231. SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd");
  232. dFormat.setTimeZone(TimeZone.getDefault());
  233. return dFormat.format(date);
  234. }
  235.  
  236. /**
  237. * <获取年月>
  238. * @param date 时间
  239. * @return String [获取年月]
  240. * @author 洪墨水
  241. */
  242. public static String getDateYearAndMonth(Date date)
  243. {
  244. SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM");
  245. dFormat.setTimeZone(TimeZone.getDefault());
  246. return dFormat.format(date);
  247. }
  248.  
  249. /**
  250. * 获取有效时间
  251. * @return 有效时间
  252. * @author 洪墨水
  253. */
  254. public static int getExpireTime()
  255. {
  256. Calendar cal = Calendar.getInstance();
  257. cal.set(Calendar.HOUR_OF_DAY, 23);
  258. cal.set(Calendar.MINUTE, 59);
  259. cal.set(Calendar.SECOND, 59);
  260. return (int) ((cal.getTime().getTime() - new Date().getTime()) / 1000);
  261. }
  262.  
  263. /**
  264. * <时间增加>
  265. * @param p 时间日期
  266. * @param number 要增加数
  267. * @param filed 域
  268. * @return Date [增加后的时间]
  269. * @author 洪墨水
  270. */
  271. public static Date addDate(Date p, int number, int filed)
  272. {
  273. Calendar cal = Calendar.getInstance();
  274. cal.setTime(p);
  275. cal.add(filed, number);
  276. return cal.getTime();
  277. }
  278.  
  279. /**
  280. * <时间戳转日期时间>
  281. * @param s 时间戳
  282. * @return String [日期时间]
  283. * @author 洪墨水
  284. */
  285. public static String stampToDate(String s, String format)
  286. {
  287. if (StringUtils.isEmpty(format))
  288. {
  289. format = "yyyy-MM-dd HH:mm:ss";
  290. }
  291. String res;
  292. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
  293. long lt = new Long(s);
  294. Date date = new Date(lt);
  295. res = simpleDateFormat.format(date);
  296. return res;
  297. }
  298.  
  299. /**
  300. * 日期转换成 yyyy-MM-dd HH:mm:ss zzz+时区形式 如:2019-04-24 19:18:03 132+0800
  301. * @param date 日期
  302. * @return LongDate
  303. * @author 洪墨水
  304. */
  305. public static String getLongDateString(Date date)
  306. {
  307. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SZ");
  308.  
  309. return dateFormat.format(date);
  310. }
  311. }

aop切面类中的@Pointcut的用法:

格式:

  1. execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)

括号中各个pattern分别表示:

  • 修饰符匹配(modifier-pattern?)
  • 返回值匹配(ret-type-pattern)可以为*表示任何返回值,全路径的类名等
  • 类路径匹配(declaring-type-pattern?)
  • 方法名匹配(name-pattern)可以指定方法名 或者 *代表所有, set* 代表以set开头的所有方法
  • 参数匹配((param-pattern))可以指定具体的参数类型,多个参数间用“,”隔开,各个参数也可以用“*”来表示匹配任意类型的参数,如(String)表示匹配一个String参数的方法;(*,String) 表示匹配有两个参数的方法,第一个参数可以是任意类型,而第二个参数是String类型;可以用(..)表示零个或多个任意参数
  • 异常类型匹配(throws-pattern?)
  • 其中后面跟着“?”的是可选项

注:详细信息请看----切面AOP的切点@Pointcut用法

四、测试类

  1. package hongmoshui.com.cnblogs.www.work.service.impl;
  2.  
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.URLEncoder;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9.  
  10. import javax.servlet.http.HttpServletResponse;
  11.  
  12. import org.apache.log4j.Logger;
  13. import org.apache.poi.hssf.usermodel.HSSFCell;
  14. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  15. import org.apache.poi.hssf.usermodel.HSSFFont;
  16. import org.apache.poi.hssf.usermodel.HSSFRow;
  17. import org.apache.poi.hssf.usermodel.HSSFSheet;
  18. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  19. import org.apache.poi.ss.util.CellRangeAddress;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22.  
  23. import hongmoshui.com.cnblogs.www.base.dao.RedisClientDao;
  24. import hongmoshui.com.cnblogs.www.base.model.Result;
  25. import hongmoshui.com.cnblogs.www.work.annotation.Log;
  26. import hongmoshui.com.cnblogs.www.work.dao.TestDao;
  27. import hongmoshui.com.cnblogs.www.work.dao.TestQueryDao;
  28. import hongmoshui.com.cnblogs.www.work.model.UserInfo;
  29. import hongmoshui.com.cnblogs.www.work.service.TestService;
  30.  
  31. @Service("testService")
  32. public class TestServiceImpl implements TestService
  33. {
  34. @Autowired
  35. private transient RedisClientDao redisClientDao;/**
  36. * 日志记录log
  37. */
  38. public transient Logger log = Logger.getLogger(getClass());
  39.  
  40. @Log(name = "getValue")
  41. @Override
  42. public Result getValue(String key)
  43. {
  44. Result result = new Result();
  45. Object obj = redisClientDao.get(key);
  46. if (obj != null)
  47. {
  48. result.setSuccess(true);
  49. result.setMessage("取出成功!");
  50. result.put("value", obj);
  51. log.info("call redis get success! redis key:" + key + ",value:" + obj);
  52. }
  53. else
  54. {
  55. result.setSuccess(false);
  56. result.setMessage("取出失败!");
  57. log.error("call redis get failed! redis key:" + key + ",value:" + obj);
  58. }
  59. return result;
  60. }
  61. }

测试结果:

2019-04-22 15:36:51 [hongmoshui.com.cnblogs.www.base.aop.LogAspect]-[INFO] method=getValue, targetObject=hongmoshui.com.cnblogs.www.work.service.impl.TestServiceImpl, requestParames="hongmoshui_01", requestTime=2019-04-22 15:36:51 134+0800, responseTime=2019-04-22 15:36:51 250+0800, responseParames={"message":"取出成功!","value":"我是01","success":true}, requestIp=null, costTime=119
方法执行结束...

AOP切面详解的更多相关文章

  1. spring框架 AOP核心详解

    AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,是个比较经典的例子. 一 AOP的基本概念 (1)Asp ...

  2. Spring系列25:Spring AOP 切点详解

    本文内容 Spring 10种切点表达式详解 切点的组合使用 公共切点的定义 声明切点@Poincut @Poincut 的使用格式如下: @Poincut("PCD") // 切 ...

  3. Spring——面向切面编程(AOP)详解

    声明:本博客仅仅是一个初学者的学习记录.心得总结,其中肯定有许多错误,不具有参考价值,欢迎大佬指正,谢谢!想和我交流.一起学习.一起进步的朋友可以加我微信Liu__66666666 这是简单学习一遍之 ...

  4. Spring框架学习-Spring的AOP概念详解

    一.SpringAOP的概述. AOP(Aspect Oriented Programming),面向切面编程,通过预编译方式和运行期间动态代理实现程序的功能的统一维护的技术.AOP是OOP(面向对象 ...

  5. Spring AOP全面详解(超级详细)

    如果说 IOC 是 Spring 的核心,那么面向切面编程AOP就是 Spring 另外一个最为重要的核心@mikechen AOP的定义 AOP (Aspect Orient Programming ...

  6. SPRING AOC、AOP 概念详解

    AOC 依赖注入:就是通过容器来控制业务对象之间的依赖关系.也就是把需要的业务对象都放入容器中,需要注入时,通过反射技术来动态获取指定的对象,装配到当前使用对象.代替了原始的 new 来实现对象的实例 ...

  7. Spring框架学习05——AOP相关术语详解

    1.Spring AOP 的基本概述 AOP(Aspect Oriented Programing)面向切面编程,AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视.事务管理.安全检查 ...

  8. [转载] 多图详解Spring框架的设计理念与设计模式

    转载自http://developer.51cto.com/art/201006/205212_all.htm Spring作为现在最优秀的框架之一,已被广泛的使用,51CTO也曾经针对Spring框 ...

  9. Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现

    前文,我们分析了Spring IOC的初始化过程和Bean的生命周期等,而Spring AOP也是基于IOC的Bean加载来实现的.本文主要介绍Spring AOP原理解析的切面实现过程(将切面类的所 ...

随机推荐

  1. [BZOJ3236]:[Ahoi2013]作业(莫队+分块)

    题目传送门 题目描述 此时已是凌晨两点,刚刚做了$Codeforces$的小$A$掏出了英语试卷.英语作业其实不算多,一个小时刚好可以做完.然后是一个小时可与做完的数学作业,接下来是分别都是一个小时可 ...

  2. 纯css实现手机通讯录

    我们经常在手机上看到通讯录列表,这类布局一般有两个显著的效果 首字母吸顶 快速定位 下面我们来实现一下 页面结构 这里页面结构很简单,就是两个列表 <div class="con&qu ...

  3. MacOS上zsh环境设置默认jdk

    进入home目录 cd ~ 修改.zprofile文件 vi .zprofile 按i进入vim插入模式,添加以下代码 export JAVA_HOME="/Library/Java/Jav ...

  4. SQLSTATE[HY000] [2002] No such file or directory

    正常的解决办法.. 只需将laravel配置文件中的host 127.0.0.1改成localhost就可以: 'mysql' => array(            'driver'    ...

  5. vi不能使用jk 映射?

    vi不能使用jk 映射? 因为vi 不支持inormap 这种键映射! 要安装vim-enhanced后才能使用vim命令, 也才能够使用 键映射!

  6. 【C++进阶:STL常见性质3】

    STL3个代表性函数:for_each(), random_shuffle(), sort() vector<int> stuff; random_shuffle(stuff.begin( ...

  7. MySQL的常用JSON函数

    1. JSON_SEARCH(col ->> '$[*].key', type, val) col: JSON格式的字段名 key:要搜索的col字段的key type:可以为'one'或 ...

  8. 测开之路一百二十三:快速搭建python虚拟环境

    前提:已装好python3.4+且环境可正常运行 一:手动搭建: 准备好一个工作目录 管理员运行cmd,进入到准备的目录里面 执行命令:python -m venv 虚拟环境名 激活虚拟环境(在ven ...

  9. PHP 实现并发-进程控制 PCNTL

    参考 基于PCNTL的PHP并发编程 PCNTL 是 PHP 中的一组进程控制函数,可以用来 fork(创建)进程,传输控制信号等. 在PHP中,进程控制支持默认关闭.编译时通过 --enable-p ...

  10. c# thread4——lock,死锁,以及monitor关键字

    多线程的存在是提高系统效率,挖掘cpu性能的一种手段,那么控制它,能够协同多个线程不发生bug是关键. 首先我们来看一段不安全的多线程代码. public abstract class Calcula ...