日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通过自定义注解与aop统一存储操作记录

發(fā)布時(shí)間:2024/1/17 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过自定义注解与aop统一存储操作记录 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

模塊開發(fā)完成后,接到通知需要添加操作記錄功能,看著那一堆接口,如果一個(gè)方法一個(gè)方法的加,那真是太麻煩了。為了偷懶,就百度了一下,發(fā)現(xiàn)可以通過自定義注解和aop的形式來統(tǒng)一添加操作記錄,只需要在每個(gè)方法上面添加自定義的注解就可以了。我在這里用的是springboot2.0以及jpa,廢話不多說,直接上代碼~

自定義注解serverLog

import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/*** 請(qǐng)輸入一段話進(jìn)行描述** @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;/*** 請(qǐng)輸入一段話進(jìn)行描述** @author Holley* @create 2018-07-03 16:57**/ @Aspect @Order(2) @Component public class TestAop {@Autowiredprivate OperationDao operationDao;/*** @Author holley* @Description 第一種設(shè)置切點(diǎn)的方式* 定義切點(diǎn)(添加severLog的方法),log是參數(shù)(該切點(diǎn)的serverLog注解的實(shí)例變量)* @Date 2018/7/3 17:09* @Param* @return*/@Pointcut("@annotation(log)")public void severLog(serverLog log){ }/*** @Author holley* @Description JoinPoint joinPoint參數(shù)在所有類型通知中都可直接調(diào)用,表示和切面的連接點(diǎn),可以通過詞參數(shù)獲取切面方法的參數(shù)等* @Date 2018/7/3 17:55* @Param* @return*/@After("severLog(log)")public void rightback(JoinPoint joinPoint, serverLog log){// 可以在此處通過 獲取token得到當(dāng)前登陸人的信息,也可以通過緩存獲取,然后存入操作記錄Operation operation = new Operation();operation.setContent(log.comment());operation.setCreated(new Date());operationDao.save(operation);System.out.println("切面的所有參數(shù):" + Arrays.toString(joinPoint.getArgs()));}/*** @Author holley* @Description returning 定義該切面(添加了severLog注解的方法)的返回結(jié)果* @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("切面的所有參數(shù):" + Arrays.toString(joinPoint.getArgs()));}/*** @Author holley* @Description 第二種設(shè)置切點(diǎn)的方式* 第一個(gè)星號(hào):表示返回類型,*號(hào)表示所有類型* 第二個(gè)星號(hào):表示設(shè)置為切點(diǎn)的類名,*號(hào)表示所有的類* 第三個(gè)星號(hào):表示設(shè)置為切點(diǎn)的類的方法名,*號(hào)表示該類中所有的方法* 括弧里表示方法的參數(shù),兩個(gè)點(diǎn)表示任何參數(shù)* @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.*;/*** 請(qǐng)輸入一段話進(jìn)行描述** @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注解將變量直接用/加在地址欄后時(shí),不能傳字符串,如: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 證明不能傳字符串格式的參數(shù)* @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;/*** 請(qǐng)輸入一段話進(jìn)行描述** @author Holley* @create 2018-07-03 14:33**/ @Service("helloservice") public class HelloServiceImpl implements Helloservice{@Autowiredprivate UserDao userDao;@serverLog(comment = "根據(jù)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,第二個(gè)泛型參數(shù)為User類主鍵的類型** @author Holley* @create 2018-07-03 14:38**/public interface UserDao extends JpaRepository<User,Long> {/*** @Author holley* @Description 注意:使用jpa時(shí),User不是數(shù)據(jù)庫中的表名,而是實(shí)體類的類名* @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,第二個(gè)泛型參數(shù)為User類主鍵的類型** @author Holley* @create 2018-07-03 14:38**/public interface OperationDao extends JpaRepository<Operation,Long> {}

?

model

import javax.persistence.*;/*** 請(qǐng)輸入一段話進(jìn)行描述** @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;/*** 操作表的實(shí)體類** @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

經(jīng)過測試,完全可以跑通,如果有什么問題,歡迎各位大神來拍磚~

轉(zhuǎn)載于:https://www.cnblogs.com/zhlblogs/p/9260039.html

總結(jié)

以上是生活随笔為你收集整理的通过自定义注解与aop统一存储操作记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。