Spring_6 _ use annotation to implement AOP

Source: Internet
Author: User

Spring_6 _ use annotation to implement AOP

1)PersonServiceInterface Class:

public interface PersonService {public void save(String naem);public void update(String name,Integer id);public String getPersonName(Integer id);}

2) PersonServiceBean implementation class:

@ Service // automatically assemble public class PersonServiceBean implements PersonService {@ Overridepublic void save (String naem) {// throw new RuntimeException ("exception"); System. out. println ("Call the save () method") ;}@ Overridepublic void update (String name, Integer id) {System. out. println ("Call update () method") ;}@ Overridepublic String getPersonName (Integer id) {System. out. println ("Call the getPersonName () method"); return "xoxo ";}}

3) MyIntercerptor interceptor class:

/*** Cut plane */@ Aspect // mark as cut plane @ Component // automatically assemble public class MyInterceptor by means of automatic scanning {// springDaoBean. personServiceBean is the class to be intercepted ,//. *(..) all methods in the interception class @ Pointcut ("execution (* springDaoBean. personServiceBean. *(..)) ") private void anyMethod () {// declare a start point} // set and obtain parameters of the pre-notification @ Before (" anyMethod () & args (name) ") public void doAccessCheck (String name) {System. out. println ("pre-notification:" + name);} // set and obtain parameters of the Post-Notification @ AfterReturning (pointcut = "anyMethod ()", returning = "result") public void doAfterReturning (String result) {System. out. println ("post notification:" + result) ;}@ After ("anyMethod ()") public void doAfter () {System. out. println ("last notification");} // set and obtain parameters for external notifications @ AfterThrowing (pointcut = "anyMethod ()", throwing = "e ") public void doAfterThrowing (RuntimeException e) {System. out. println ("exception notification:" + e) ;}// surround notification setting @ Around ("anyMethod ()") public Object doBasicProfiling (ProceedingJoinPoint pjp) throws Throwable {System. out. println ("access method"); Object result = pjp. proceed (); System. out. println ("Exit method"); return result ;}}

4) XML file Configuration:

 
 
  
  
  Aspectj-autoproxy/>
  
  
  
 

5) springAopTest test class:

Public class springAopTest {@ Testpublic void instanceSpring () {AbstractApplicationContext ctx = new ClassPathXmlApplicationContext ("springXml/beans. xml "); PersonService personServiceBean = (PersonService) ctx. getBean ("personServiceBean"); // pre-notification test and parameter printing personServiceBean. save ("oooo"); // post-Notification testing and parameter printing personServiceBean. getPersonName (0 );}}












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.