8.Spring "AOP" annotation method

Source: Internet
Author: User

1. Introduction of the JAR package

Sprig Framework foundation Package +JUNTIL Integration Pack + Log Pack +AOP package

spring的传统AOP的开发的包

  spring-aop-4.2.4.RELEASE.jar

  com.springsource.org.aopalliance-1.0.0.jar

aspectJ的开发包

  com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

  spring-aspects-4.2.4.RELEASE.jar

2. Introduction of configuration Files (AOP constraints, annotation constraints)

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd    Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd/HTTP Www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">        </Beans>

3. Create an implementation class

 Public Interface UserService {        publicvoid  Save ();          Public void update ();          Public void Delete ();}
@Service (value= "UserService") Public classUserserviceimplImplementsUserService {@Override Public voidSave () {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("Business layer: Save Customer ..."); } @Override Public voidUpdate () {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("Business layer: Update Customer ..."); } @Override Public voidDelete () {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("Business layer: Delete Customer ..."); }}

4. Hosting the implementation class in spring annotation mode

5. Creating a Slice Class

 public  class   Myaspectann { public  void   log () {System.out.println (" Pre-notification ... "     public  void   arround (Proceedingjoinpoint joinpoint) {System.out.println ( Surround notification 1 ...)   SYSTEM.OUT.PRINTLN ( surround notification 2 ...)     public  void   after () {System.out.println (" post-Notification: "     }  

6. Hosting a slice class in spring

7. Defining the Pointcut for the Slice class method

8. Turn on automatic proxy in the configuration file and scan annotations

    <!---    <base-package = "Com.spring.demo1"  />        <!----    <AOP: Aspectj-autoproxy/>

9. Writing test Code

@RunWith (Springjunit4classrunner.class) @ContextConfiguration ("Classpath:applicationContext.xml") Public classDemo1 {@Resource (name= "UserService")    PrivateUserService UserService; @Test Public voidm01 () {System.out.println ("=======================");        Userservice.save (); System.out.println ("=======================");        Userservice.update (); System.out.println ("=======================");        Userservice.delete (); System.out.println ("======================="); }}

Effect:

8.Spring "AOP" annotation method

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.