Spring things 2 (9), spring

Source: Internet
Author: User

Spring things 2 (9), spring

AOP is very important in spring.

There are five notification types in the face-cutting class:

Aop: before pre-notification

Aop: Post-notification after-returning

Aop: after final notification

Aop: after-throwing exception notification

Aop: around notifications

1 <bean id = "personDAO" class = "com. lee. spring002.aop. xml. personDAOImpl "> </bean> 2 <bean id =" transaction "class =" com. lee. spring002.aop. xml. transaction "> </bean> 3 4 <aop: config> 5 <! -- Entry point expression. Role: Determine the target class --> 6 <! -- Spring automatically checks whether the class under this expression is a cut surface. If yes, it does not include --> 7 <aop: pointcut expression = "execution (* com. lee. spring002.aop. xml. personDAOImpl. *(..)) "id =" perform "/> 8 <! -- Ref points to the aspect --> 9 <aop: aspect ref = "transaction"> 10 <! -- Pre-notification --> 11 <aop: before method = "beginTransaction" pointcut-ref = "perform"/> 12 13 <! -- 14 the post-Notification 15 can get the returned value of the target method (the pre-method cannot be obtained) 16. If the target method throws an exception, the post-notification will not continue to execute 17 --> 18 <aop: after-returning method = "commit" pointcut-ref = "perform" returning = "val"/> 19 20 <! -- 21 the final notification 22 will execute this method no matter whether the target method throws an exception or not --> 24 <aop: after method = "finallyDisplay" pointcut-ref = "perform"/> 25 26 <! -- 27 exception Notification 28 --> 29 <aop: after-throwing method = "exception" pointcut-ref = "perform" throwing = "content"/> 30 31 <! -- 32 surround notification 33 can control whether the target method can be executed 34 pre-notification and post-notification can add code before and after the target method, but cannot control the method execution 35 --> 36 <aop: around method = "arround" pointcut-ref = "perform"/> 37 </aop: aspect> 38 </aop: config>

Let's briefly describe the section expression:

  • Execution of any public method:
    execution(public * *(..))
  • Execution of any method whose name starts with "set:
    execution(* set*(..))
  • AccountServiceExecution of any method defined by the interface:
    execution(* com.xyz.service.AccountService.*(..))
  • Execution of any method defined in the service package:
    execution(* com.xyz.service.*.*(..))
  • Execution of any method defined in the service package or its sub-package:
    execution(* com.xyz.service..*.*(..))

 

IPersonDAO. java

1 package com.lee.spring002.aop.xml;2 3 public interface IPersonDAO {4     public String savePerson();5 }

PersonDAOImpl. java

 1 package com.lee.spring002.aop.xml; 2  3 public class PersonDAOImpl implements IPersonDAO { 4  5     @Override 6     public String savePerson() { 7         System.out.println("PersonDAOImpl - savePerson()"); 8          9 //        int a = 1 / 0;10         11         return "save successfully";12     }13 14 }

Transaction. java

 

1 package com. lee. spring002.aop. xml; 2 3 import org. aspectj. lang. joinPoint; 4 import org. aspectj. lang. proceedingJoinPoint; 5 6/** 7 * This is a cut section 8*9 * @ author leechenxiang10 * @ date January 12, 2016 11*12 */13 public class Transaction {14 15 public void beginTransaction (JoinPoint jp) {16 System. out. println ("connection point name:" + jp. getSignature (). getName (); 17 System. out. println ("target class name:" + jp. getTarget (). getClass (); 18 System. out. println ("Begin transaction... "); 19} 20 21/** 22*23 * @ param jp24 * @ param val the return value of the target method. It must correspond to the returning 25 */26 public void commit (JoinPoint jp, object val) {27 System. out. println ("Transaction commit... "); 28 29 System. out. println ("returning:" + val. toString (); 30} 31 32 public void finallyDisplay () {33 System. out. println ("finally... "); 34} 35 36 public void exception (JoinPoint jp, Throwable content) {37 System. out. println ("exception:" + content); 38 System. out. println ("exception:" + content. getMessage (); 39} 40 41 public void arround (ProceedingJoinPoint pjp) throws Throwable {42 System. out. println ("arround... "); 43 pjp. proceed (); // call the target method. If this row is not written, the target method is not executed 44} 45}

Test:

1 package com. lee. spring002.aop. xml; 2 3 import org. junit. test; 4 import org. springframework. context. applicationContext; 5 import org. springframework. context. support. classPathXmlApplicationContext; 6 7 public class TransactionTest {8 9/** 10 * principle: 11*1. When the spring container is started, load two beans, instantiate the two beans 12*2. When the spring container parses the configuration file to <aop: config> 13*3. parse the entry point expression, match bean14 * 4 of the spring container content according to the entry point expression. If the matching is successful, create a proxy object for the bean 15*5. When the client uses context. when getBean obtains an object, if the object has a proxy object, it returns the proxy object 16 * if there is no proxy object, then it returns the object itself 17 */18 @ Test19 public void testPerson () {20 ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext. xml "); 21 IPersonDAO personDAO = (IPersonDAO) context. getBean ("personDAO"); 22 personDAO. savePerson (); 23} 24 25}

 

Github address: https://github.com/leechenxiang/maven-spring002-aop

 

 

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.