SPRINGAOP: A simple aspect case

Source: Internet
Author: User

Slice class

Package com.xbz.learn.spring.aspect;
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.springframework.stereotype.Component; /** * Slice class * @author xubaozhong * */@Aspect public class Firstaspect {@Before ("Execution (* com.xbz.learn.spring . Aspect.
    firstaspecttarget.* (..)) ")
    public void Dobefore () {System.out.println ("before ...");
    } @After ("Execution (* com.xbz.learn.spring.aspect.firstaspecttarget.* (..))")
    public void Doafter () {System.out.println ("after ...");
    } @AfterReturning ("Execution (* com.xbz.learn.spring.aspect.firstaspecttarget.* (..))")
    public void afterreturning () {System.out.println ("afterreturning ..."); } @AfterThrowing ("Execution (* com.xbz.learn.spring.aspect.FirstAspectTarget.* (..)) ")
    public void RollbackTransaction () {System.out.println ("afterthrowing ..."); }
}

Target class

Package com.xbz.learn.spring.aspect;

Import org.springframework.stereotype.Component;

/**
 * Tangent target class, target class blocked
 * @author xubaozhong * *
 /
@Component Public
class Firstaspecttarget { Public

    void Doservice () {
        System.out.println ("doservice----processing business logic");
        throw new RuntimeException (New Exception ("Business Error"));
    }
}

Javaconfig class

Package com.xbz.learn.spring.aspect;

Import Org.springframework.context.annotation.Bean;
Import Org.springframework.context.annotation.ComponentScan;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
//@ComponentScan
@EnableAspectJAutoProxy Public
class Firstaspecjavaconfig {

    @Bean Public
    Firstaspect Getfirstaspect () {
        return new firstaspect ();
    } 

    @Bean public
    firstaspecttarget getfirstaspecttarget () {
        return new firstaspecttarget ();
    } 
}

Test class

Package com.xbz.learn.spring.aspect;

Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.test.context.ContextConfiguration;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (Classes={firstaspecjavaconfig.class})
public class Firstaspecttest {
    @Autowired
    private firstaspecttarget target;
    @Test public
    void Testfirstaspect () {
        target.doservice ();
    }
}

However, in the above section class did not see Pointcut figure, and many execution is very heavy, using the following alternative
Another way of writing a slice

Package com.xbz.learn.spring.aspect;
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; /** * Slice class * @author xubaozhong * */@Aspect public class Firstaspect {@Pointcut ("Execution (* COM.XBZ.LEARN.SPRI
    ng.aspect.firstaspecttarget.* (..)) ") public void Pointcut () {} @Before ("Pointcut ()") public void Dobefore () {System.out.println ("before.

    .");
    }
    @After ("Pointcut ()") public void Doafter () {System.out.println ("after ...");
    } @AfterReturning ("Pointcut ()") public void afterreturning () {System.out.println ("afterreturning ...");
   } @AfterThrowing ("Pointcut ()") public void RollbackTransaction () {     System.out.println ("afterthrowing ...");
 }
}

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.