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 );}}