Simple use of AOP in Springframework

Source: Internet
Author: User
AOP, as an important part of spring's lightweight container, has received increasing attention, and spring's transaction is managed using AOP, and today is a simple example of how the basic usage of AOP in spring is. First determine the target of the proxy, the default in spring in the JDK dynamic proxy, it can only implement the interface agent, if you want to proxy the class, you need to use Cglib proxy. Obviously, it would be wiser to choose "Programming to Interface", which is the interface that will be proxied: public interface Foointerface {public void Printfoo (); public void Dummyfoo ();} and a simple Implementation: public class Fooimpl implements Foointerface {public void Printfoo () {System.out.println ("in Fooimpl.printfoo");} p ublic void Dummyfoo () {System.out.println ("in Fooimpl.dummyfoo");} Next, create a advice that supports the Around,before,after returning and throws four advice in spring, with a simple before advice example: public class Printbeforeadvice implements Methodbeforeadvice {public void before (method arg0, object[] arg1, Object arg2) throws Throw Able {System.out.println ("in Printbeforeadvice");}} With their own business interface and advice, the rest is how to assemble them, first using proxyfactory to implement programmatically, as follows: public class Aoptestmain {public static void Main (string[] args) {Fooimpl Fooimpl = new Fooimpl (); Printbeforeadvice Myadvice = new PRINTBEFOreadvice (); Proxyfactory factory = new Proxyfactory (FOOIMPL); Factory.addbeforeadvice (Myadvice); Foointerface MyInterface = (foointerface) factory.getproxy (); Myinterface.printfoo (); Myinterface.dummyfoo (); Now that the program is executed, the miraculous result appears: in Printbeforeadvice in Fooimpl.printfoo in Printbeforeadvice in Fooimpl.dummyfoo although this can be realized to spring AO P usage, but this is by no means a recommended method, now that you are using spring, assembling the required beans in ApplicationContext is the best strategy, and implementing the above functionality requires simply writing a simple applicationcontext, as follows: The AOP application context
Foointerface



Myadvice
Of course, the code in main needs to be modified as well: public static void Main (string[] args) {Classpathxmlapplicationcontext context = new Classpathxmla Pplicationcontext ("Applicationcontext.xml"); foointerface foo = (foointerface) context.getbean ("foo"); Foo.printfoo (); Foo.dummyfoo (); Now, the results will be exactly the same as the results above, so is it more elegant? When you need to change the implementation, you only need to modify the configuration file, the code in the program does not need any changes. However, it will be found that all the method calls in the object of the proxy will run the before in advice, which obviously does not meet the needs of most cases, at this point, just borrow the advisor, Of course, in the advisor to use pattern to set up which methods need advice, change ApplicationContext as follows: the Springeva application context


. *print.*

Foointerface



Myadvisor
The main program does not require any modifications, and the results of the operation have changed: in Printbeforeadvice in Fooimpl.printfoo in Fooimpl.dummyfoo so, you should have understood the use of AOP in spring, Of course the most important application of AOP in spring is transaction Manager, for example, take a look at this applicationcontext:
/web-inf/jdbc.properties

${jdbc.driverclassname}

${jdbc.url}

${jdbc.username}

${jdbc.password}



Smartmenu.hbm.xml



${hibernate.dialect}













Propagation_required,readonly

Propagation_required,readonly


Well, to fully understand spring's AOP, it's best to look at the source, open source is good!

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.