The AOP preparation for spring's re-learning

Source: Internet
Author: User
Tags aop throwable

First, the idea of AOP:

Transverse repetition, longitudinal extraction

1, garbled

2. Transaction Management

3,action

Spring can generate proxy objects for objects managed in the container

1, spring can help us to generate proxy objects

2, spring implementation of the principle of AOP

(1) Dynamic agent (priority)

The proxy object must implement an interface to produce a proxy object, and if no interface will be able to use dynamic proxy technology

(2) Cglib Agent

Third-party agent technology, Cglib, can be generated for any class agent, the principle of the agent is the target object to inherit the agent, if

If the target object is final decorated, the class cannot be Cglib proxy.

3, on the code

(1) interface

 Package Cn.itcast.service;  Public Interface userservice {    void  Save ();     void Delete ();     void update ();     void find ();}

(2) Implementation class

 PackageCn.itcast.service; Public classUserserviceimplImplementsUserService {@Override Public voidSave () {System.out.println ("Save the user!"); //int i = 1/0;} @Override Public voidDelete () {System.out.println ("Delete User!"); } @Override Public voidUpdate () {SYSTEM.OUT.PRINTLN ("Update user!"); } @Override Public voidfind () {System.out.println ("Find a user!"); }}

(3) Dynamic Agent code

//Tourist code = Dynamic Agent Public classUserserviceproxyfactoryImplementsInvocationhandler { PublicUserserviceproxyfactory (userservice us) {Super();  This. US =us; }    Privateuserservice us;  PublicUserService Getuserserviceproxy () {//Generating Dynamic AgentsUserService usproxy = (userservice) proxy.newproxyinstance (userserviceproxyfactory.class. getClassLoader (), Userserviceimpl.class. Getinterfaces (), This); //return        returnUsproxy; } @Override PublicObject Invoke (Object Arg0, Method method, object[] arg2)throwsthrowable {System.out.println ("Open the transaction!"); Object Invoke= Method.invoke (US, arg2);//Original MethodSYSTEM.OUT.PRINTLN ("COMMIT TRANSACTION!")); returninvoke; }}

Test:

@Test//Dynamic Agent     Public voidfun1 () {userservice US=NewUserserviceimpl (); Userserviceproxyfactory Factory=Newuserserviceproxyfactory (US); UserService Usproxy=Factory.getuserserviceproxy ();                Usproxy.save (); //The proxy object implements the same interface as the Proxied object//the proxy object has no inheritance relationship to the Proxied object//System.out.println (usproxy instanceof Userserviceimpl);//false}

Results:

Open the transaction! Save the user !      Commit the transaction !   

(4) Cglib Agent

//Tourist Code =>cglib agent Public classUserServiceProxyFactory2ImplementsMethodinterceptor { PublicUserService Getuserserviceproxy () {enhancer en=NewEnhancer ();//help us generate proxy objectsEn.setsuperclass (Userserviceimpl.class);//set who to delegate toEn.setcallback ( This);//What the agent wants to douserservice US= (UserService) en.create ();//Create a proxy object                returnus; } @Override PublicObject Intercept (Object Prxoyobj, method, object[] arg, Methodproxy methodproxy)throwsThrowable {//Open TransactionSYSTEM.OUT.PRINTLN ("Open transaction!")); //call the original methodObject returnvalue =methodproxy.invokesuper (Prxoyobj, ARG); //Commit a transactionSYSTEM.OUT.PRINTLN ("COMMIT TRANSACTION!")); returnreturnvalue; }}

Test:

   @Test    publicvoid  fun2 () {                new  UserServiceProxyFactory2 ();                 = factory.getuserserviceproxy ();                Usproxy.save ();                 // To determine if a proxy object belongs to a        proxied object type // the proxy object inherits the proxy object =>true         // System.out.println (usproxy instanceof Userserviceimpl); // true    }

Results:

Open the transaction! Save the user !      commit a transaction

Third, the term in AOP:

Joinpoint (connection point): All methods that can be enhanced in the target object

Pointcut (pointcut): target object, already enhanced method

Advice (Notification/enhancement): Enhanced code, etc.

Target object: The object being proxied

Weaving (weaving): Applying notifications to a process with pointcuts

Proxy: A proxy object is formed after the notification is woven into the target object

Aspect (tangent): Pointcut + notification

The AOP preparation for spring's re-learning

Related Article

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.