通过自定义注解与aop统一存储操作记录
生活随笔
收集整理的這篇文章主要介紹了
通过自定义注解与aop统一存储操作记录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
模塊開發完成后,接到通知需要添加操作記錄功能,看著那一堆接口,如果一個方法一個方法的加,那真是太麻煩了。為了偷懶,就百度了一下,發現可以通過自定義注解和aop的形式來統一添加操作記錄,只需要在每個方法上面添加自定義的注解就可以了。我在這里用的是springboot2.0以及jpa,廢話不多說,直接上代碼~
自定義注解serverLog
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/*** 請輸入一段話進行描述** @author Holley* @create 2018-07-03 14:23**/ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface serverLog {public String comment() default ""; }aop
import com.springcloud.serviceclient01.dao.OperationDao; import com.springcloud.serviceclient01.model.Operation; import com.springcloud.serviceclient01.model.User; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component;import java.util.Arrays; import java.util.Date;/*** 請輸入一段話進行描述** @author Holley* @create 2018-07-03 16:57**/ @Aspect @Order(2) @Component public class TestAop {@Autowiredprivate OperationDao operationDao;/*** @Author holley* @Description 第一種設置切點的方式* 定義切點(添加severLog的方法),log是參數(該切點的serverLog注解的實例變量)* @Date 2018/7/3 17:09* @Param* @return*/@Pointcut("@annotation(log)")public void severLog(serverLog log){ }/*** @Author holley* @Description JoinPoint joinPoint參數在所有類型通知中都可直接調用,表示和切面的連接點,可以通過詞參數獲取切面方法的參數等* @Date 2018/7/3 17:55* @Param* @return*/@After("severLog(log)")public void rightback(JoinPoint joinPoint, serverLog log){// 可以在此處通過 獲取token得到當前登陸人的信息,也可以通過緩存獲取,然后存入操作記錄Operation operation = new Operation();operation.setContent(log.comment());operation.setCreated(new Date());operationDao.save(operation);System.out.println("切面的所有參數:" + Arrays.toString(joinPoint.getArgs()));}/*** @Author holley* @Description returning 定義該切面(添加了severLog注解的方法)的返回結果* @Date 2018/7/3 17:41* @Param* @return*/@AfterReturning(returning = "u", pointcut = "severLog(log)")public void endback(JoinPoint joinPoint,User u,serverLog log){System.out.println(u.toString()+"-----------------------"+log.comment());System.out.println("切面的所有參數:" + Arrays.toString(joinPoint.getArgs()));}/*** @Author holley* @Description 第二種設置切點的方式* 第一個星號:表示返回類型,*號表示所有類型* 第二個星號:表示設置為切點的類名,*號表示所有的類* 第三個星號:表示設置為切點的類的方法名,*號表示該類中所有的方法* 括弧里表示方法的參數,兩個點表示任何參數* @Date 2018/7/3 17:48* @Param* @return*/@Pointcut("execution(* com.springcloud.serviceclient01.service.impl..*.*(..))")public void severTest(){ }}controller層
import com.springcloud.serviceclient01.model.User; import com.springcloud.serviceclient01.service.Helloservice; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;/*** 請輸入一段話進行描述** @author Holley* @create 2018-07-03 15:12**/ @RestController @RequestMapping("/hello") public class HelloController {@Autowiredprivate Helloservice helloservice;@GetMapping("/{sid}")public String Hello(@PathVariable("sid") Long sid){User u = helloservice.getUserByid(sid);return u.toString();}@GetMapping("/test/{sid}")public String test1(@PathVariable("sid") Long sid){String name = helloservice.findByUid(sid);return name;}/*** @Author holley* @Description 注意:使用spring注解將變量直接用/加在地址欄后時,不能傳字符串,如:localhost:18762/hello/name/2/川普* 正確如下* localhost:18762/hello/name/2?name=川普* @Date 2018/7/3 16:19* @Param* @return*/@GetMapping("/name/{sid}")public String test2(@PathVariable("sid") Long sid,@RequestParam("name")String name){String p = helloservice.findByUser(sid,name);return p;}/*** @Author holley* @Description 證明不能傳字符串格式的參數* @Date 2018/7/3 16:23* @Param* @return*//*@GetMapping("/a/{name}")public String test3(@PathVariable("name")String name){System.out.print(name + "測試成功--------------");return name;}*/ }serviceimpl層
import com.springcloud.serviceclient01.aop.serverLog; import com.springcloud.serviceclient01.dao.UserDao; import com.springcloud.serviceclient01.model.User; import com.springcloud.serviceclient01.service.Helloservice; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;/*** 請輸入一段話進行描述** @author Holley* @create 2018-07-03 14:33**/ @Service("helloservice") public class HelloServiceImpl implements Helloservice{@Autowiredprivate UserDao userDao;@serverLog(comment = "根據id查詢用戶")@Overridepublic User getUserByid(Long id) {return userDao.getOne(id);}@Overridepublic String findByUid(Long id) {return userDao.findByaa(id);}@Overridepublic String findByUser(Long id, String name) {return userDao.findUser(id,name);} }dao層
import com.springcloud.serviceclient01.model.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.transaction.annotation.Transactional;/*** 繼承JpaRepository,第二個泛型參數為User類主鍵的類型** @author Holley* @create 2018-07-03 14:38**/public interface UserDao extends JpaRepository<User,Long> {/*** @Author holley* @Description 注意:使用jpa時,User不是數據庫中的表名,而是實體類的類名* @Date 2018/7/3 16:12* @Param* @return*/@Transactional(timeout = 10)@Query("select name from User where uid = ?1")String findByaa(Long uid);@Transactional(timeout = 10)@Query("select password from User where uid = ?1 and name = ?2")String findUser(Long uid,String name); } import com.springcloud.serviceclient01.model.Operation; import org.springframework.data.jpa.repository.JpaRepository;/*** 繼承JpaRepository,第二個泛型參數為User類主鍵的類型** @author Holley* @create 2018-07-03 14:38**/public interface OperationDao extends JpaRepository<Operation,Long> {}?
model
import javax.persistence.*;/*** 請輸入一段話進行描述** @author Holley* @create 2018-07-03 14:34**/ @Entity public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long uid;private String name;private String password;@Column(name ="is_opt_required")private Integer isOptRequired;public Long getUid() {return uid;}public void setUid(Long uid) {this.uid = uid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Integer getIsOptRequired() {return isOptRequired;}public void setIsOptRequired(Integer isOptRequired) {this.isOptRequired = isOptRequired;}@Overridepublic String toString() {return "User{" +"uid=" + uid +", name='" + name + '\'' +", password='" + password + '\'' +", isOptRequired=" + isOptRequired +'}';} } import javax.persistence.*; import java.util.Date;/*** 操作表的實體類** @author Holley* @create 2018-06-05 14:12**/ @Entity public class Operation {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long opid;private Long sid;private Long oid;private Long pid;private Long uid;private String account;private String content;private Date created;@Transientprivate String orderCode;@Transientprivate String pname;@Transientprivate String sname;public Long getOpid() {return opid;}public void setOpid(Long opid) {this.opid = opid;}public Long getOid() {return oid;}public void setOid(Long oid) {this.oid = oid;}public Long getPid() {return pid;}public void setPid(Long pid) {this.pid = pid;}public Long getUid() {return uid;}public void setUid(Long uid) {this.uid = uid;}public String getAccount() {return account;}public void setAccount(String account) {this.account = account;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public Date getCreated() {return created;}public void setCreated(Date created) {this.created = created;}public Long getSid() {return sid;}public void setSid(Long sid) {this.sid = sid;}public String getOrderCode() {return orderCode;}public void setOrderCode(String orderCode) {this.orderCode = orderCode;}public String getPname() {return pname;}public void setPname(String pname) {this.pname = pname;}public String getSname() {return sname;}public void setSname(String sname) {this.sname = sname;}@Overridepublic String toString() {return "Operation{" +"opid=" + opid +", sid=" + sid +", oid=" + oid +", pid=" + pid +", uid=" + uid +", account='" + account + '\'' +", content='" + content + '\'' +", created=" + created +", orderCode='" + orderCode + '\'' +", pname='" + pname + '\'' +", sname='" + sname + '\'' +'}';} }參考文檔:
https://www.cnblogs.com/acm-bingzi/p/javaAnnotation.html
https://www.zifangsky.cn/788.html
https://blog.csdn.net/qq_34021712/article/details/78680915
經過測試,完全可以跑通,如果有什么問題,歡迎各位大神來拍磚~
轉載于:https://www.cnblogs.com/zhlblogs/p/9260039.html
總結
以上是生活随笔為你收集整理的通过自定义注解与aop统一存储操作记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一起用ipython
- 下一篇: 【learning】快速沃尔什变换FWT