Spring AOP Simple Example

Source: Internet
Author: User
Tags throwable

Simply record a sample of spring AOP

Based on two configuration methods:

Based on XML configuration

Annotation-based configuration

This example is to simulate a change to the database to add things

Actually did not add, just a simple output of the record

First look at the entire example of the directory map

  

All the code is not posted, the number is a bit more, but very simple, see part of it can understand

The first method of configuration

XML-based configuration

First register the Service,dao with the spring container

  

It's convenient to configure the scan package.

Next look at the service

  

1  PackageCom.yangxin.core.service.impl;2 3 Importorg.springframework.beans.factory.annotation.Autowired;4 ImportOrg.springframework.stereotype.Service;5 6 ImportCom.yangxin.core.dao.UserDao;7 ImportCom.yangxin.core.pojo.User;8 ImportCom.yangxin.core.service.UserService;9 Ten @Service One  Public classUserserviceimplImplementsUserService { A  - @Autowired -     PrivateUserdao Userdao; the      - @Override -      Public voidaddUser (user user) { - userdao.insertuser (user); +System.out.println ("Add Success"); -     } +  A @Override at      Public voiddeleteuser (String name) { - Userdao.deteleuser (name); -System.out.println ("Delete succeeded"); -     } -  -}

The thing to do is simple, insert a piece of data, delete a piece of data

Next, take a look at the tangent code

1  Packagecom.yangxin.core.transaction;2 3 ImportOrg.aspectj.lang.ProceedingJoinPoint;4 5 ImportCom.yangxin.core.pojo.User;6 7  Public classTransactiondemo {8     9     //front-facing notificationsTen      Public voidStartTransaction () { OneSYSTEM.OUT.PRINTLN ("BEGIN Transaction")); A     } -      -     //Post Notification the      Public voidcommittransaction () { -SYSTEM.OUT.PRINTLN ("Commit Transaction"); -     } -      +     //Surround Notifications -      Public voidAround (Proceedingjoinpoint joinpoint)throwsthrowable{ +SYSTEM.OUT.PRINTLN ("BEGIN Transaction")); A          at joinpoint.proceed (); -          -SYSTEM.OUT.PRINTLN ("Commit Transaction"); -     } -      -}

and see how this section is configured in Applicationcontext.xml.

 

1 <Aop:config>2         <Aop:pointcutexpression= "Execution (* com.yangxin.core.service.*.*.* (..))"ID= "P1" />3 4         <Aop:aspectref= "Transactiondemo">5         6         <Aop:beforeMethod= "StartTransaction"Pointcut-ref= "P1" />7         8         <aop:after-returningMethod= "CommitTransaction"Pointcut-ref= "P1"/>9         Ten         </Aop:aspect> One     </Aop:config>

There is no presentation surround notification here

Okay, run the test code.

The test code is as follows

1     @Test2      Public voidtest1 () {3ApplicationContext ApplicationContext =NewClasspathxmlapplicationcontext ("Spring/applicationcontext.xml");4         5UserService userservice = Applicationcontext.getbean (userservice.class);6         7User User =NewUser ();8         9User.setage (19);TenUser.setname ("Yangxin"); One                  A userservice.adduser (user); -Userservice.deteleuser ("Yangxin"); - the          -}

The console output is as follows

BEGIN TRANSACTION

Add success

Commit TRANSACTION

BEGIN TRANSACTION

Delete Succeeded

Commit TRANSACTION

Now, let's test the surround notification.

Modify the section of the configuration plane in Applicationcontext.xml

The modified code

1 <Aop:config>2         <Aop:pointcutexpression= "Execution (* com.yangxin.core.service.*.*.* (..))"ID= "P1" />3 4         <Aop:aspectref= "Transactiondemo">5         6         <Aop:aroundMethod= "Around"Pointcut-ref= "P1"/>7         8         </Aop:aspect>9     </Aop:config>

Run the test code

The output is as follows

BEGIN TRANSACTION
Add success
Commit TRANSACTION
BEGIN TRANSACTION
Delete Succeeded
Commit TRANSACTION

Okay, now, how do you put the annotation in the way?

Paste the code of the annotation-based slice

1  Packagecom.yangxin.core.transaction;2 3 ImportOrg.aspectj.lang.ProceedingJoinPoint;4 Importorg.aspectj.lang.annotation.AfterReturning;5 ImportOrg.aspectj.lang.annotation.Around;6 ImportOrg.aspectj.lang.annotation.Aspect;7 ImportOrg.aspectj.lang.annotation.Before;8 ImportOrg.aspectj.lang.annotation.Pointcut;9 Ten @Aspect One  Public classTransactionDemo2 { A      -@Pointcut (value= "Execution (* com.yangxin.core.service.*.*.* (..))") -      Public voidPoint () { the          -     } -      -@Before (value= "point ()") +      Public voidbefore () { -SYSTEM.OUT.PRINTLN ("Transaction Begin"); +     } A      at@AfterReturning (value = "point ()") -      Public voidAfter () { -SYSTEM.OUT.PRINTLN ("Transaction Commit"); -     } -      -@Around ("Point ()") in      Public voidAround (Proceedingjoinpoint joinpoint)throwsthrowable{ -SYSTEM.OUT.PRINTLN ("Transaction Begin"); to joinpoint.proceed (); +SYSTEM.OUT.PRINTLN ("Transaction Commit"); -          the     } *}

Configuring in Applicationcontext.xml

1 <  = " transactionDemo2"= "Com.yangxin.core.transaction.TransactionDemo2"    />
1 <  />

The test procedure is consistent with the above, and it is not posted here.

Complete

Spring AOP Simple example

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.