Java EE ------ Spring ----- "AspectJ Annotation

Source: Internet
Author: User

Java EE ------ Spring ----- "AspectJ Annotation

The Section Technology in Spring is introduced earlier, and now the section is annotated.

First of all, it must be the same as before. An automatic proxy annotation class AnnotationAwareAspectJAutoProxyCreator is required.

 

Code in the configuration file:

 

 
  
   
   
  
  
 

 

 

In the configuration file, it is not difficult to find that the most basic object is a proxy object, automatic proxy class, cut (can be self-written class, this annotation is self-written, of course, the previous one is in aop ). We need to write our own partition class.

 

Face Cutting

 

Package cn. aop. aspectj2; 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. annotatio N. pointcut; @ Aspectpublic class MyAdvisor {private final String CUT = "execution (* cn. aop. aspectj2 .*. *(..)) ";/** run the command Before the core code */@ Before (CUT) public void advice () {System. out. println ("Block previously");}/** After the core code is executed, this */@ After (CUT) public void advice2 () will be called if the code Exits normally or is abnormal */@ After (CUT () {System. out. println ("block once");}/** it is best not to use the around notification (which can be replaced by before + after), because the parameter dependency --- but also has advantages, * is to obtain the information of the intercepted object and the Corresponding Method */@ Around (CUT) public void around (ProceedingJo InPoint p) throws Throwable {System. out. println ("this is before surround" + p. getKind () + "," + p. getTarget (); p. proceed (); System. out. println ("after");}/** is for a function * after the previous function is executed, this will be executed. * However, when an exception occurs in the method, this will not be executed. It will only be executed after normal exit */@ AfterReturning (CUT) public void afterR (JoinPoint jp) {System. out. println ("all later" + jp. getKind ();} @ AfterReturning (CUT) public void after2 () {System. out. println ("All after 222");}/** this is called after an exception occurs, provided that the preceding code is not captured Catch this exception. * after the exception is caught, it will not be called here. It is usually used in many logs. **/@ AfterThrowing (CUT) public void throwex () throws Exception {System. out. println ("an Exception occurred ");}}

 

 

Person class

 

Package cn. aop. aspectj2; public class Person {public void say () {// Integer. parseInt ("aa"); // test whether an exception occurs. out. println ("... this is say .. ");} public void run () {System. out. println ("this is the run method in person ");}}


Test class

 

Public class Demo2 {@ Testpublic void test () {// get the same configuration file from the container, the ApplicationContext app = new ClassPathXmlApplicationContext ("cn/aop/aspectj2/aspectj2.xml"); Person p = app. getBean (Person. class); p. run (); p. say ();}}

 

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.