Spring Learning-Manual implementation of AOP programming

Source: Internet
Author: User

AOP-oriented tangent programming

AOP allows for complete separation of "business code" and "Focus Code"

  @Override public    list<message> findallmessage () {        session session = NULL;  Attention Point Code        Transaction tx = null;        try{            session = Hibernateutils.getsession ();            tx = Session.begintransaction ();  Attention Point code            Query q = Session.createquery ("From Message Order by write_time Desc"); Business code            return Q.list ();  Business code        }catch (Exception e) {            throw new RuntimeException (e);  Attention Point Code        }finally {            tx.commit ();            Session.close ();  Attention Point code        }    }

  

Analysis:

The focus code refers to the code that executes repeatedly.

Business code and attention Point code separation, benefits?

--Focus code write once.

-developers need to focus on their core business

--During operation, the dynamic implantation of the focus code when executing the business code; (Proxy implementation)

How to Detach

Cross-Program/object/proxy mode separation

To implement an AOP programming example manually:

Package Aop;import org.springframework.stereotype.component;/** * Created is cxspace on 16-8-10.    */@Component//Join IOC container public class Aop {//execute code public void begin () {System.out.println ("Start transaction");    } public void Commite () {System.out.println ("Commit Transaction"); }}package aop;/** * Created by Cxspace on 16-8-10. */public interface Iuserdao {public void Save ();} Package Aop;import org.springframework.stereotype.component;import javax.annotation.resource;/** * Created by Cxspace On 16-8-10.    */@Componentpublic class Userdao implements iuserdao{@Resource private AOP AOP;        @Override public void Save () {Aop.begin (); SYSTEM.OUT.PRINTLN ("Save data!        ");    Aop.commite (); }}<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" Xmlns:contex T= "Http://www.springframework.org/schema/context "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.O Rg/schema/context/spring-context.xsd > <!--open annotation Scan-<context:component-scan base-package= "AOP" > </context:component-scan></beans>package Aop;import Org.junit.test;import Org.springframework.context.applicationcontext;import org.springframework.context.support.classpathxmlapplicationcontext;/** * Created by Cxspace on 16-8-10.    */public class App {applicationcontext AC = new Classpathxmlapplicationcontext ("Aop/bean.xml");        @Test public void Test () {Iuserdao Userdao = (Iuserdao) ac.getbean ("Userdao");    Userdao.save (); }}

Package aopproxy;/** * Created by Cxspace on 16-8-10. */public interface Iuserdao {public void Save ();} Package Aopproxy;import org.springframework.stereotype.component;/** * Created is cxspace on 16-8-10. */@Componentpublic class Userdao implements Iuserdao {@Override public void Save () {System.out.println ("Save number According to    "); }}package aopproxy;import org.springframework.stereotype.component;/** * Created by Cxspace on 16-8-10.    */@Component//Join IOC container public class Aop {//execute code public void begin () {System.out.println ("Start transaction");    } public void Commite () {System.out.println ("Commit Transaction"); }}package Aopproxy;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;import java.lang.reflect.proxy;/** * Created by Cxspace on 16-8-10.    */public class Proxyfactory {//target object private static object target;    private static AOP AOP;       Generate proxy object method public static object Getproxyinstance (Object Target_,aop aop_) {target = target_; AOP = Aop_; Return Proxy.newproxyinstance (Target.getclass (). getClassLoader (), Target.getclass (). Getinte Rfaces (), new Invocationhandler () {@Override public Object invoke (Ob                        Ject Proxy, method method, object[] args) throws Throwable {Aop.begin ();                        Object returnvalue = Method.invoke (Target,args);                        Aop.commite ();                    Return returnvalue;    }                }        ); }}<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" Xmlns:contex t= "Http://www.springframework.org/schema/context" 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-contex T.xsd > <!--open annotation Scan-<context:component-scan base-package= "Aopproxy" ></context:component-scan&    Gt <!--call the factory method, return the object after the Userdao agent--<bean id= "Userdao_proxy" class= "Aopproxy.proxyfactory" factory-method= " Getproxyinstance "> <constructor-arg index=" 0 "ref=" Userdao "></constructor-arg> <constructo R-arg index= "1" ref= "AOP" ></constructor-arg> </bean></beans>package aopproxy;import Org.junit.test;import Org.springframework.context.applicationcontext;import org.springframework.context.support.classpathxmlapplicationcontext;/** * Created by Cxspace on 16-8-10.    */public class App {applicationcontext AC = new Classpathxmlapplicationcontext ("Aopproxy/bean.xml");        @Test public void Test () {Iuserdao Userdao = (Iuserdao) ac.getbean ("Userdao_proxy"); SYSTEM.OUT.PRINTLN (USerdao.getclass ());    Userdao.save (); }}

  

Spring Learning-Manual implementation of AOP programming

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.