1. Object-oriented, interface-oriented, aspect-oriented programming
Object-oriented Programming (OOP), is to abstract the real thing into a class that contains the property and method, it is a kind of evolution to process-oriented programming, can realize the reuse of code, it realizes the abstraction of code level .
Interface-oriented programming, which is organized into an interface in a functionally similar way, it implements the abstraction of functional level .
Area-slicing programming, which enables the separation of business functions and concerns, and it achieves business-level abstraction.
Difference between AOP and OOP
http://www.differencebetween.com/difference-between-aop-and-vs-oop/
2. The role of AOP
AOP's aspect-oriented programming enables the separation of "business code" from "Focus code."
Save a user public void Add (user user) {session session = NULL; Transaction trans = null; try {session = Hibernatesessionfactoryutils.getsession (); "Focus Code" trans = Session.begintransaction (); "Attention Point Code" session.save (user); Core business code Trans.commit (); ... "Attention point Code"} catch (Exception e) {e.printstacktrace (); if (trans! = null) {trans.rollback (); //.. "Focus Code"}} finally{Hibernatesessionfactoryutils.closesession (session); ////.. "Focus Code"}}
Analysis Summary:
The focus code refers to the code that executes repeatedly.
What are the benefits of separating the business code from the focus code?
- - focus on the code to write once;
--The developer only needs to focus on the core business;
-- The operation period, the implementation of core business code, the dynamic implantation of the attention point code; "Agent"
3. AOP Example
3.1. Example One
Applicationcontext.xml
<?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:context= "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-context.xsd " Default-autowire= "Bytype" ><!-- turn on annotation scanning --><context:component-scan base-package= " Com.rk.hibernate.d_aop_basic "></contExt:component-scan></beans>
Aop.java
Package Com.rk.hibernate.d_aop_basic;import org.springframework.stereotype.component;/** * Iterative code Formation of an AOP class * */@ Component//Join IOC container public class aop{//repeat code public void begin () {SYSTEM.OUT.PRINTLN ("Transaction Open");} Duplicate code public void End () {System.out.println ("transaction End");}}
Iuserdao.java
Package com.rk.hibernate.d_aop_basic;//interface Public interface iuserdao{void Save ();}
Userdao.java
Package Com.rk.hibernate.d_aop_basic;import Javax.annotation.resource;import org.springframework.stereotype.component;/** * Target Object */@Component//Join IOC container public class Userdao implements iuserdao{@ Resource private AOP AOP; Note: The setter method is not set here, but it can also be successfully @overridepublic void Save () {Aop.begin (); SYSTEM.OUT.PRINTLN ("-----core business: Save!!! ------"); Aop.end ();}}
App.java
Package Com.rk.hibernate.d_aop_basic;import Org.junit.test;import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.classpathxmlapplicationcontext;public class App{private ApplicationContext ac = new Classpathxmlapplicationcontext ("/com/rk/hibernate/d_aop_basic/applicationcontext.xml") ; @Testpublic void Test () {Iuserdao DAO = (Iuserdao) ac.getbean ("Userdao");d Ao.save ();}}
3.2. Example Two
Applicationcontext.xml
<?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:context= "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-context.xsd " Default-autowire= "Bytype" ><!-- turn on annotation scanning --><context:component-scan base-package= " Com.rk.hibernate.e_aop_basic "></context:component-scan><!-- Call factory method, return Userdao proxy object --><bean id= "Userdao_proxy" Class= "Com.rk.hibernate.e_aop_basic. Proxyfactory " factory-method=" newproxyinstance "><constructor-arg index=" 0 " type=" Java.lang.Object " ref=" Userdao "></constructor-arg><constructor-arg index=" 1 " type=" Com.rk.hibernate.e_aop_basic. AOP " ref=" AOP ></constructor-arg></bean></beans>
Aop.java
Package Com.rk.hibernate.e_aop_basic;import org.springframework.stereotype.component;/** * Iterative code Formation of an AOP class * */@ Component//Join IOC container public class aop{//repeat code public void begin () {SYSTEM.OUT.PRINTLN ("Transaction Open");} Duplicate code public void End () {System.out.println ("transaction End");}}
Iuserdao.java
Package com.rk.hibernate.e_aop_basic;//interface Public interface iuserdao{void Save ();}
Userdao.java
Package Com.rk.hibernate.e_aop_basic;import org.springframework.stereotype.component;/** * Target Object */@Component// Join IOC container public class Userdao implements iuserdao{@Overridepublic void Save () {System.out.println ("-----core business: Save!!! ------");}}
Proxyfactory.java
Package Com.rk.hibernate.e_aop_basic;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.Method; Import java.lang.reflect.proxy;/** * Agent Factory * */public class Proxyfactory{public static Object newproxyinstance (final objec T target,final AOP AOP) {//method of generating proxy objects return Proxy.newproxyinstance (Target.getclass (). getClassLoader (), Target.getclass (). Getinterfaces (), new Invocationhandler () {@Overridepublic object Invoke (Object proxy, method, Object[] args) throws Throwable{aop.begin ();//Execute Duplicate code object result = Method.invoke (target, args);//method to execute target object Aop.end () ;//execute duplicate code return result;}});}}
App.java
Package Com.rk.hibernate.e_aop_basic;import Org.junit.test;import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.classpathxmlapplicationcontext;public class App{private ApplicationContext ac = new Classpathxmlapplicationcontext ("/com/rk/hibernate/e_aop_basic/applicationcontext.xml") ; @Testpublic void Test () {Iuserdao DAO = (Iuserdao) ac.getbean ("Userdao_proxy");d Ao.save ();}}
(5) Manual implementation of AOP programming