Spring+springmvc+maven adding facets using @aspectj

Source: Internet
Author: User

I do it by myself on the information on the Internet, for reference only.

Be clear: Using the @aspect annotation method, the project is a MAVEN project.

Using the @aspect annotation method, the main areas that need to be modified are:

1, pom file, add:

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver  -->    <dependency>        < Groupid>org.aspectj</groupid>        <artifactid> aspectjweaver</artifactid>        <version>1.7.4</ version>    </dependency>    <!-- https:// mvnrepository.com/artifact/aspectj/aspectjrt -->    <dependency>         <groupId>org.aspectj</groupId>         <artifactId>aspectjrt</artifactId>         <version>1.7.4</version>    </dependency> 

Here's a question to be aware of. The version I used here was 1.5.4, but when I started it, I encountered an error,

0 can ' t find referenced pointcut, this problem makes me very puzzled, later after the online search information, said is the environmental problem.

In general:

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/8C/CC/wKioL1h4qInB24fWAAAaIrreKDk728.png-wh_500x0-wm_ 3-wmp_4-s_4043168549.png "title=" (@[dzpw ' gntf39x (00d}%i9.png "alt=" wkiol1h4qinb24fwaaaairrekdk728.png-wh_50 "/ >

My JDK version is 1.7, so I switched to 1.7.4ok.

2, SPRINGMVC configuration, I here is the default name, called Springmvc-servlet.xml

This configuration file needs to be added

<!--scan AOP, which corresponds to its own defined annotation class--<context:component-scan base-package= "Com.tarena.vote.aspect"/> <!--start a SPECTJ annotation support, Proxy-target-class equals true is mandatory with Cglib proxy--<aop:aspectj-autoproxy proxy-target-class= "true"/>

3, define the annotation class.

Description: I this is an experimental nature, so there is no specific business code. Inside you can add the business you need, such as logging, transaction control, and so on.

package com.tarena.vote.aspect;import org.aspectj.lang.joinpoint;import  org.aspectj.lang.proceedingjoinpoint;import org.aspectj.lang.annotation.after;import  org.aspectj.lang.annotation.afterreturning;import org.aspectj.lang.annotation.afterthrowing;import  org.aspectj.lang.annotation.around;import org.aspectj.lang.annotation.aspect;import  org.aspectj.lang.annotation.before;import org.aspectj.lang.annotation.pointcut;import  Org.springframework.stereotype.component;/** * <p> *    description:   Annotations Logic class (test, meaningless)  * </p> * *  @author  fcl *  @date   January 11, 2017  *  @version  v_1.0 */@Aspect @componentpublic class aspectadvice {     /**     * Pointcut     *  Define Pointcut,pointcut name called Aspectjmethod, must have no parameter, no return value      *  just an identity, do not make calls      *  pointcuts what I am defining here is for all classes under the controller package, all methods are added,      *   You can specify a specific class or specific method      */     @Pointcut ("Execution (*  Com.tarena.vote.web.controller.*.* (..)) ")     //@Pointcut ("@annotation (Com.tarena.vote.aspect.AspectAdvice)")      private void aspectjmethod () {};     @Before ("Aspectjmethod ()")      public void dobefore (Joinpoint joinpoint) {       &NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("----dobefore () Start----");         System.out.println ("Do some work before executing business logic");         system.out.println (" Get what you need through Jointpoint ")         system.out.println ("----dobefore () End----" );    }     @Around ("Aspectjmethod ()") &NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSp;object doaround (PROCEEDINGJOINPOINT&NBSP;PJP)  throws Throwable{     &NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("----doaround () Start----");         system.out.println ("Do some work similar to before here");         //Core Logic          object retval=pjp.proceed ();         system.out.println ("Work here can be done like after");         SYSTEM.OUT.PRINTLN ("----doaround () end----");        return retval;     }     @After (value= "Aspectjmethod ()")     public  void doafter (Joinpoint joinpoint) {         SYSTEM.OUT.PRINTLN ("----doafter () start----");         system.out.println (" After executing the core logic, the work "); &NBSP;&NBSP;&NBSP;&NBSP;&NBsp;   system.out.println ("Get what you need through Jointpoint");        &NBSP;SYSTEM.OUT.PRINTLN ("----doafter () end----");     }    @ Afterreturning (value= "Aspectjmethod ()", returning= "RetVal")     public void  Doreturn (Joinpoint joinpoint, string retval) {         System.out.println ("afterreturning () Start");         system.out.println (" return value=  "+retval);         system.out.println (" You can do some processing of the returned results here. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("----afterreturning () End---- ");    }     @AfterThrowing (value=" Aspectjmethod () ",  throwing=" E ")     public void dothrowing (joinpoint joinpoint,exception e) {         system. OUT.PRINTLN ("-----dothrowing () Start-----");         system.out.println ("   Error message: "+e.getmessage ());         system.out.println ("   This is intended to perform core business logic errors, catch exceptions, and do some logging operations, and so on ");         system.out.println ("   can be joinpoint to get what you Need ");         system.out.println ("-----End  of dothrowing ()------");    }}/** *  custom annotations.  *  This custom annotation I don't know what it's for, I feel like it might be used to describe it., *  feel that there is no place in the project to use, but see the online examples are added  *  if the great God saw, help me answer.  */package com.tarena.vote.aspect;import java.lang.annotation.Documented;import  java.lang.annotation.elementtype;import java.lang.annotation.retention;import  java.lang.annotation.retentionpolicy;import java.lang.annotation.target;/** * Custom Annotations   intercept controller  */@Target ({elementtype.parameter, elementtype.method}) @Retention (RetentionPolicy.runtime) @Documentedpublic    @interface  systemlog {    string  operationtype ()   default  "";    //operation type     String  Methods ()   default  "";   //new user     string description ()    default  "";   //}

At this point, the work that needs to be done has been completed. The results of running the project are as follows:

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/8C/D0/wKiom1h4qv6AHp4WAAB8J0eDzz0820.png-wh_500x0-wm_ 3-wmp_4-s_1895866543.png "title=" n}ee{$D 5ni]rnkyv1v~rz1k.png "alt=" wkiom1h4qv6ahp4waab8j0edzz0820.png-wh_50 "/ >

This article is from the "Unruly Wind" blog, please be sure to keep this source http://fengcl.blog.51cto.com/9961331/1891863

Spring+springmvc+maven adding facets using @aspectj

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.