SpringAOP基于注解的Around通知

Git ww9992019com 发表于 1 周前

<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="10051">
                <input type="hidden" name="catid" id="catid" value="0">

                <p><code><a href="https://github.com/Around" title="@Around " class="at-link">@Around </a></code>是一种建议类型,可确保方法执行前后的通知可以运行。 以下是<code><a href="https://github.com/Around" title="@Around " class="at-link">@Around </a></code>通知的语法:</p>

<p><strong>语法</strong></p>

<pre><code class="lang-java">@Pointcut ("execution(* com.yiibai.Student.getAge(..))")

private void selectGetName(){}

@Around ("selectGetAge()")

public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint){

System.out.println("[aroundAdvice] Around advice");

Object[] args=jp.getArgs();

if(args.length&gt;0){

  System.out.print("[aroundAdvice] Arguments passed: " );

  for (int i = 0; i &lt; args.length; i++) {

     System.out.print("[aroundAdvice] arg "+(i+1)+": "+args[i]);

  }

}

Object result=jp.proceed(args);

System.out.println("[aroundAdvice] Returning " + result);

return result.toString();

}

</code></pre>

<p>在上面的语法示例中 -</p>

<ul>

<li><code><a href="https://github.com/Pointcut" title="@Pointcut " class="at-link">@Pointcut </a></code> - 将函数标记为切入点</li><li><code>execution( expression )</code> - 涵盖应用通知的方法的表达式。</li><li><code><a href="https://github.com/Around" title="@Around " class="at-link">@Around </a></code> - 将函数标记为在切入点覆盖的方法之前执行的通知。</li></ul>

<p>要了解上面提到的<code><a href="https://github.com/Around" title="@Around " class="at-link">@Around </a></code>通知相关的概念,我们写一个Spring AOP基于注解的应用例子,它将使用基于注解配置实现通知。打开并使用<strong>Eclipse IDE</strong>,并按照以下步骤创建一个Spring应用程序:</p>

<ol>

<li>更新在<a href="http://www.yiibai.com/spring_aop/springaop_annotation_application.html" title="Spring AOP应用实例">Spring AOP应用实例</a>中创建过的<code>Student</code>项目。</li><li>更新<code>bean</code>配置并运行应用程序,如下所述。</li></ol>

<p>整个项目的目录结构如下所示 -</p>

<p><img src="http://www.yiibai.com/uploads/images/201704/1304/560150423_48373.png" alt=""></p>

<p>下面是<code>Logging.java</code>文件的内容。 这实际上是一个<code>Aspect</code>模块的样本,它定义了在各个点被调用的方法。</p>

<pre><code class="lang-java">package com.yiibai;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Pointcut;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.ProceedingJoinPoint;

@Aspect

public class Logging {

/** Following is the definition for a pointcut to select

*  all the methods available. So advice will be called

*  for all the methods.

*/

@Pointcut ("execution(* com.yiibai.Student.getAge(..))")

private void selectGetAge(){}

/**

* This is the method which I would like to execute

* around a selected method execution.

*/

@Around ("selectGetAge()")

public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{

  System.out.println("Around advice");

  Object[] args=proceedingJoinPoint.getArgs();

  if(args.length&gt;0){

     System.out.print("Arguments passed: " );

     for (int i = 0; i &lt; args.length; i++) {

        System.out.print("arg "+(i+1)+": "+args[i]);

     }

  }



  Object result=proceedingJoinPoint.proceed(args);

  System.out.println("Returning " + result);

}

}

</code></pre>

<p>以下是<em>Student.java</em>文件的内容:</p>

<pre><code class="lang-java">package com.yiibai;

public class Student {

private Integer age;

private String name;

public void setAge(Integer age) {

  this.age = age;

}

public Integer getAge() {

  System.out.println("Age : " + age );

  return age;

}

public void setName(String name) {

  this.name = name;

}

public String getName() {

  System.out.println("Name : " + name );

  return name;

}

public void printThrowException(){

   System.out.println("Exception raised");

   throw new IllegalArgumentException();

}

}

</code></pre>

<p>以下是<code>MainApp.java</code>文件的内容:</p>

<pre><code class="lang-java">package com.yiibai;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

public static void main(String[] args) {

  ApplicationContext context =

         new ClassPathXmlApplicationContext("Beans.xml");



  Student student = (Student) context.getBean("student");



  student.getAge();

}

}

</code></pre>

<p>以下是配置文件<em>Beans.xml</em>文件的内容:</p>

<pre><code class="lang-xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "&gt;

&lt;aop:aspectj-autoproxy/&gt;

&lt;!-- Definition for student bean --&gt;

&lt;bean id="student" class="com.yiibai.Student"&gt;

  &lt;property name="name"  value="minsu" /&gt;

  &lt;property name="age"  value="24"/&gt;

&lt;/bean&gt;

&lt;!-- Definition for logging aspect --&gt;

&lt;bean id="logging" class="com.yiibai.Logging"/&gt;

&lt;/beans&gt;

</code></pre>

<h2 id="h2-u8FD0u884Cu9879u76EE"><a name="%E8%BF%90%E8%A1%8C%E9%A1%B9%E7%9B%AE" class="reference-link"></a><span class="header-link octicon octicon-link"></span>运行项目</h2><p>完成创建源代码和配置文件后,运行应用程序。右键单击应用程序中的<code>MainApp.java</code>,并使用运行方式作为<strong>Java应用程序命令</strong>。 如果您的应用程序一切正常,这将打印以下消息:</p>

<pre><code class="lang-shell">[aroundAdvice]Around advice

Age : 24

[aroundAdvice] Returning 24

</code></pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="Spring AOP基于注解的AfterThrowing"><i class="layui-icon"></i>
                            Spring AOP基于注解的AfterThrowing</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:Spring AOP代理"><i class="layui-icon"></i>
                            Spring AOP代理</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复