當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
大数据WEB阶段Spring框架 AOP面向切面编程(二)
生活随笔
收集整理的這篇文章主要介紹了
大数据WEB阶段Spring框架 AOP面向切面编程(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Spring AOP面向切面編程(二)
一、切入點的execution表達式
execution的表達形式: execution(修飾符? 返回值類型 所在包類? 方法名(參數列表) 異常?)
? execution(public * *(..))
工程中所有的public方法
?execution(* set*(..))
工程中所有方法名以set開頭的方法
?execution(* com.xyz.service.AccountService.*(..))
com.xyz.service.AccountService類下面的所有方法
?execution(* com.xyz.service..(..))
com.xyz.service包下所有類的所有方法
?execution(* com.xyz.service...(..))
com.xyz.service包下及其子包下所有類的所有方法
execution(* com.xyz..service..(..))
報名以com.xyz開頭的所有子包一直子到service下的所有類的所有方法
舉例:com.xyz.a.b.service com.xyz.a.service com.xyz.a.b.c.service 會滿足上面的條件
二、五大通知的具體實現
三、AOP注解
四、通過注解生成切入點表達式的引用
五、環繞嵌套問題
六、自定義注解
七、各種示例
異常
代碼結構如圖所示PersonServletPersonServicePersonDao 用異常通知捕獲servlet的所有的方法拋出的異常: 目標對象所在的類 cn.tedu.big1601.servlet.PersonServlet 拋出異常所在的方法 save() 拋出異常的名稱 XxxException 異常信息 message意義: 異常處理類和業務邏輯類完全松耦合。 時刻捕獲生產生產環境中所有的錯誤,實時監控該系統,異常收集。@Component @Aspect public class ExceptionAspect {@AfterThrowing(value = "execution(* com.tj..*(..))" ,throwing = "throwable" )public void after(JoinPoint jp , Throwable throwable){System.out.println("異常發生在:"+jp.getTarget().getClass());System.out.println(jp.getSignature().getName()+"()發生了異常!");System.out.println("發生異常類型:"+throwable.getClass());System.out.println("異常信息:"+throwable.getMessage());} }統計方法執行時間
計算servlet的各個類的各個方法的執行時間 1.類的名稱 2.方法的名稱 3.執行的時間 控制臺輸出意義:用來監控程序的性能問題 @Component @Aspect public class RuntimeAspect {@Around("execution(* com.tj..*(..))")public Object around(ProceedingJoinPoint pjp) throws Throwable{Long begin = System.currentTimeMillis();Object result = pjp.proceed();Long end = System.currentTimeMillis();System.out.println(pjp.getTarget().getClass()+"類"+pjp.getSignature().getName()+"方法執行了"+ (end - begin)+"毫秒!");return result;} }事務控制
當方法上有事務的注解,該方法就有事務。寫一個切面來完成事務控制類 public class TxManage {/*** 開啟事務* */public static void stattx(){System.out.println("開啟了事務");}/*** 提交事務* */public static void commitTx(){System.out.println("提交事務");}/*** 回滾事務* */public static void rollback(){System.out.println("事務回滾");} }自定義事務注解 @Target(value = { ElementType.METHOD }) @Retention(value = RetentionPolicy.RUNTIME) public @interface TxAnnotation {String value() default "";}給需要事務控制的地方添加注解 @Component public class PersonServiceImpl implements PersonService{@Autowiredprivate PersonDao dao;@TxAnnotation@Cacheable("add")@Overridepublic void savePerson(Person person) {dao.savePerson(person);}@TxAnnotation@Cacheable("get")@Overridepublic Person getPerson(int id) {Person person = dao.getPerson(id);return person;}@TxAnnotation@Cacheable("del")@Overridepublic void delPerson(int id) {dao.delPerson(id);}}事務控制切面 @Component @Aspect public class TxAspect {@Around(value = "execution(* com.tj..*(..)) && @annotation(ann)")public Object around(ProceedingJoinPoint pjp ,TxAnnotation ann ) throws Throwable{Object result = null;try{TxManage.stattx();result = pjp.proceed();TxManage.commitTx();}catch(Exception e){TxManage.rollback();}return result; } }權限控制
說明:每個方法上添加能夠執行該方法的注解@PrivilegeInfo 并且要指明 PrivilegeInfo(name=”add”) 那么將來這個方法只能執行還有add權限的方法自定義權限控制注解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface PrivilegeInfo {String value() ; }給需要控制權限的地方添加權限控制注解 @Component public class PersonServlet {@Autowiredprivate PersonService ps;/*** 保存用戶信息* */@PrivilegeInfo("add")public void savePerson(Person person){ // int i = 1/0;ps.savePerson(person);}/*** 獲取用戶信息* */@PrivilegeInfo("get")public Person getPerson(int id){Person person = ps.getPerson(id);return person ; }/*** 刪除用戶信息* */@PrivilegeInfo("del")public void delPerson(int id ){ps.delPerson(id);} }權限控制切面類@Component @Aspect public class PrivilegInfoAspect {//當前用戶的權限List<String> list =Arrays.asList("add" , "get");@Around("execution(* com.tj..*(..)) && @annotation(ann)")public Object around(ProceedingJoinPoint pjp , PrivilegeInfo ann) throws Throwable{String value = ann.value();Object result = null;if(list.contains(value)){System.out.println("尊敬的飛秋會員你好!");result = pjp.proceed();}else{System.out.println("你沒有這個權限 ,滾");}return result;}}數據緩存
緩存需求:1.savePerson的時候需要往數據庫里保存一份然后再往內存(Map)中保存一份 2.getPerson的時候先從Map中獲取 如果有則返回則不用執行目標方法,如果內存中沒有則執行目標方法從數據庫取 3.如果第一次調用getPerson內存中沒有的話 執行目標方法從數據庫取 取出來后同時把獲取到的Person對象保存到內存中,以便后續獲取時直接從內存中取自定義 緩存控制注解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Cacheable {String value(); }給需要緩存的地方添加注解 @Component public class PersonServiceImpl implements PersonService{@Autowiredprivate PersonDao dao;@TxAnnotation@Cacheable("add")@Overridepublic void savePerson(Person person) {dao.savePerson(person);}@TxAnnotation@Cacheable("get")@Overridepublic Person getPerson(int id) {Person person = dao.getPerson(id);return person;}@TxAnnotation@Cacheable("del")@Overridepublic void delPerson(int id) {dao.delPerson(id);}}緩存控制切面類 @Component @Aspect public class CacheableAspect {//緩存Map<Integer , Person> map = new HashMap<Integer,Person>();@Around("execution(* com.tj..*(..))&& @annotation(ann)")public Object befer(ProceedingJoinPoint jp , Cacheable ann) throws Throwable{Object result = null;String v = ann.value();if(v.equals("add")){Person person = (Person) jp.getArgs()[0];int id = person.getId();if(map.containsKey(id)){System.out.println("該用戶已存在");}else{System.out.println("存入 緩存");map.put(id, person);result = jp.proceed();}}else if(v.equals("get")){int id = (Integer) jp.getArgs()[0];if(map.containsKey(id)){System.out.println("從緩存中獲取");result = map.get(id);}else{result = jp.proceed();Person person = (Person) result;map.put(person.getId(), person);}}return result;} }總結
以上是生活随笔為你收集整理的大数据WEB阶段Spring框架 AOP面向切面编程(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机视觉之OpenCV教程 ---Ma
- 下一篇: 大数据WEB阶段Spring框架 AOP