欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Java面向切面編程AOP怎么實現(xiàn)-創(chuàng)新互聯(lián)

這篇文章主要介紹“Java面向切面編程AOP怎么實現(xiàn)”,在日常操作中,相信很多人在Java面向切面編程AOP怎么實現(xiàn)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java面向切面編程AOP怎么實現(xiàn)”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

在樂山等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站制作、成都網(wǎng)站設(shè)計 網(wǎng)站設(shè)計制作按需策劃,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,成都全網(wǎng)營銷推廣,外貿(mào)營銷網(wǎng)站建設(shè),樂山網(wǎng)站建設(shè)費用合理。

一:背景
Spring的AOP的存在目的是為了解耦。AOP可以讓一組類共享相同的行為。在OOP中只能通過繼承類和實現(xiàn)接口,來是代碼的耦合度增強,且類繼承只能為單繼承,阻礙更多行為添加到一組類上,AOP彌補了OPP的不足。

二:概述  Spring支持AspectJ的注解方式切面編程

1.使用@Aspect 聲明一個切面。

2.使用@After,@Before,@Around 定義建言(advice),可直接將攔截規(guī)則(切點)作為參數(shù)。

3.其中@After,@Before,@Around參數(shù)的攔截規(guī)則為切點(PointCut) ,為了使切點復(fù)用,可使用@PointCut 專門定義攔截規(guī)則,然后在@After,@Before,@Around的參數(shù)中調(diào)用。

4.其中符合條件的每一個被攔截處為連接點(JoinPoint)

三:代碼實例

1.pom.xml

點擊(此處)折疊或打開

  1. <dependency>

  2.             <groupId>org.springframework</groupId>

  3.             <artifactId>spring-core</artifactId>

  4.         </dependency>

  5.         <dependency>

  6.             <groupId>org.springframework</groupId>

  7.             <artifactId>spring-beans</artifactId>

  8.         </dependency>

  9.         <dependency>

  10.             <groupId>org.springframework</groupId>

  11.             <artifactId>spring-context</artifactId>

  12.         </dependency>

  13.         <dependency>

  14.             <groupId>org.springframework</groupId>

  15.             <artifactId>spring-aop</artifactId>

  16.         </dependency>

  17.         <dependency>

  18.             <groupId>org.aspectj</groupId>

  19.             <artifactId>aspectjrt</artifactId>

  20.         </dependency>

  21.         <dependency>

  22.             <groupId>org.aspectj</groupId>

  23.             <artifactId>aspectjweaver</artifactId>

  24.         </dependency>

2.攔截規(guī)則的注解

點擊(此處)折疊或打開

  1. @Target(ElementType.METHOD)

  2. @Retention(RetentionPolicy.RUNTIME)

  3. @Documented

  4. public @interface Action {

  5.     String name();

  6. }

2.注解的被攔截類

點擊(此處)折疊或打開

  1. @Service

  2. public class DemoAnnotationService {

  3.     @Action(name = "注解式攔截的add操作")

  4.     public void add() {

  5.        System.out.println("======DemoAnnotationService方法add()=========");

  6.     }

  7. }

3.方法規(guī)則被攔截類

點擊(此處)折疊或打開

  1. @Service

  2. public class DemoMethodService {

  3.     public String add() throws Exception{

  4.         System.out.println("======DemoMethodService方法add()=========");

  5.         int i=100/0;

  6.         return "SUCCESS";

  7.     }

  8. }


4.編寫切面

點擊(此處)折疊或打開

  1. @Aspect

  2. @Component

  3. public class LogAspect {

  4.     @Pointcut("@annotation(com.gemdale.gmap.spring.boot.demo.Action)")

  5.     public void annotationPointCut() {

  6.     }

  7.     @After("annotationPointCut()")

  8.     public void after(JoinPoint joinPoint) {

  9.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();

  10.         Method method = signature.getMethod();

  11.         Action action = method.getAnnotation(Action.class);

  12.         System.out.println("注解式攔截 " + action.name());

  13.     }

  14.     @Before("execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))")

  15.     public void methodBefore(JoinPoint joinPoint) {

  16.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();

  17.         Method method = signature.getMethod();

  18.         System.out.println("before方法規(guī)則式攔截 " + method.getName());

  19.     }

  20.     @After("execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))")

  21.     public void methodAfter(JoinPoint joinPoint) {

  22.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();

  23.         Method method = signature.getMethod();

  24.         System.out.println("after方法規(guī)則式攔截 " + method.getName());

  25.     }

  26.     @AfterReturning(pointcut = "execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))", returning = "result")

  27.     public void methodAfterResult(JoinPoint joinPoint, Object result) {

  28.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();

  29.         Method method = signature.getMethod();

  30.         System.out.println("after result方法規(guī)則式攔截 " + method.getName() + "result=" + result);

  31.     }

  32.     @AfterThrowing(pointcut = "execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))", throwing = "e")

  33.     public void methodAfterException(JoinPoint joinPoint, Exception e) {

  34.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();

  35.         Method method = signature.getMethod();

  36.         System.out.println("after exception方法規(guī)則式攔截 " + method.getName() + " e=" + e.getMessage());

  37.     }

  38. }

5.配置類

點擊(此處)折疊或打開

  1. @Configuration

  2. @ComponentScan("com.gemdale")

  3. @EnableAspectJAutoProxy

  4. public class AppliactionConfig {

  5. }


6.執(zhí)行類

點擊(此處)折疊或打開

  1. public class Start {

  2.     public static void main(String[] args) throws Exception{

  3.         AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(

  4.                 AppliactionConfig.class);

  5.         // UseFunctionService

  6.         // useFunctionService=configApplicationContext.getBean(UseFunctionService.class);

  7.         // System.out.println(useFunctionService.sayHello("Gengchong"));

  8.         DemoAnnotationService demoAnnotationService = configApplicationContext.getBean(DemoAnnotationService.class);

  9.         DemoMethodService demoMethodService = configApplicationContext.getBean(DemoMethodService.class);

  10.         demoAnnotationService.add();

  11.         demoMethodService.add();

  12.         configApplicationContext.close();

  13.     }

  14. }

到此,關(guān)于“Java面向切面編程AOP怎么實現(xiàn)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

當(dāng)前題目:Java面向切面編程AOP怎么實現(xiàn)-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://aaarwkj.com/article24/jcije.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管標(biāo)簽優(yōu)化、App開發(fā)、企業(yè)網(wǎng)站制作營銷型網(wǎng)站建設(shè)、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)
日本 影院 一区 二区| 中文字幕精品一区二区介绍| 日韩欧美亚洲另类激情一区| 伊人久久大香线蕉av网站| 久久精品国产亚洲av麻豆她| 精品国产熟女成人av| 最新中文字幕人妻少妇| 情五月激情亚洲丁香佳色| 欧美日韩亚洲国产专区精品| 国产精品欧美一区久久| 国内午夜福利精品视频| 亚洲婷婷久久一区二区| 精品国产一区二区成人| 国产老熟女高潮精品视频网站免费| 99热视频在线观看免费| 免费亚洲一区二区三区| 韩国理伦三级做爰观看| 91国产香蕉在线观看| 国产性做爰片免费视频| 国产成人精品福利一区二区| 爱我久久视频网免费视频| 日本少妇一区二区99| 亚洲欧美日韩不卡视频| 欧美亚洲综合另类色妞| 97资源在线公开视频| 亚洲国产传媒在线观看| 99热在线播放精品观看| 国产欧美日韩亚洲综合在线| 黄片视频免费在线播放大全| 性生活视性生活大片日本| av在线男人社区日韩| 可以看的黄色亚洲网站| 日本一区二区三区不卡在线| 日韩精品一区二区在线天天狠天 | 一区二区日韩激情在线观看视频| 男人的天堂av最新版本| 中文字幕乱码十国产乱码| 亚洲日本不卡在线一区二区| 国产午夜视频在线观看一区| 中文字幕av在线日韩| 国产传媒在线观看精品|