IOC (inversion of control, controlled inversion)IOC means that the object you have designed is given to the container control, rather than the traditional direct control within your object.
DI-dependency injection, or "dependency injection": dependencies between components are determined by the container at run time, in the image, that is, the container dynamically injects a dependency into the component.
Dependency Injection (DI) and inversion of control (IOC) are the same thing that is described from different angles , that is, by introducing an IOC container, the decoupling of objects is realized by means of dependency injection.
AOP: aspect-orientedprogramming (plane-oriented programming). AOP is suitable for applications with crosscutting logic: performance monitoring, access control, transaction management, caching, object pool management, and logging. AOP extracts the code that is scattered across the business logic into a separate module by cutting it horizontally. The key to AOP implementations is the AOP proxy, which is automatically created by the AOP framework, which can be divided into static proxies and dynamic proxies, in which static proxies are compiled using the commands provided by the AOP framework to generate an AOP proxy class at compile time, which is also referred to as compilation , while dynamic agents generate AOP dynamic proxy classes in memory with the help of JDK dynamic proxies, CGLIB, and so on at runtime, and are also known as runtime enhancements.
IOC Example:
Package cqvie.com; Interface // USB Interface { publicvoid Insert (); Public String read (); Public void Write (String s); Public void pop ();}
Usb.java
Packagecqvie.com;Importorg.springframework.stereotype.Component; @Component Public classUdiskImplementsusb{ Public voidInsert () {System.out.println ("Read-in USB drive"); } PublicString Read () {System.out.println ("Read data:"); return"Data"; } Public voidWrite (String s) {System.out.println ("Write Data"); } Public voidpop () {System.out.println ("Eject USB flash drive"); }}
Udisk.java
Packagecqvie.com;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.beans.factory.annotation.Qualifier;Importorg.springframework.stereotype.Component; @Component ("Thecomputer") Public classcomputer{@Autowired//@Qualifier ("Udisk") PrivateUSB Usbdevice; PublicComputer () {}//USB devices cannot be constructed here Public voidSaveData (String data)//ways to implement smart devices{//Insert,write,popUsbdevice.insert (); Usbdevice.write (data); Usbdevice.pop (); } Public voidSetusbdevice (USB u) {usbdevice=u; } }
Computer.java
Packagecqvie.com;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classTest { Public Static voidMain (string[] args) {//Computer c=new computer (); //USB u=new udisk (); //c.setusbdevice (u);ApplicationContext CTX =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Computer C= (computer) Ctx.getbean ("Thecomputer"); C.savedata ("Biographical Notes"); Computer1 C1=NewComputer1 (); HDD MP=NewMOVHDD (); C1.setmpdevice (MP); C1.savedata ("Mobile HDD"); }}
Test.java
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context.xsd "> <BeanID= "U1"class= "Cqvie.com.UDisk"></Bean> <BeanID= "Thecomputer"class= "Cqvie.com.Computer"> < Propertyname= "Usbdevice"ref= "U1"></ Property> </Bean></Beans>
Applicationcontext.xml
The results of the operation are as follows:
Examples of AOP:
Package org.proxy.interfaces; // Boss Interface Public Interface Ilaoban { publicvoid Kaihui ();}
Package Org.proxy.interfaces.impl; Import Org.proxy.interfaces.ILaoBan; // interface Implementation Class Public class Implements ilaoban{ @Override publicvoid Kaihui () { System.out.println ( "---> Boss to have a meeting");} }
Laoban
PackageOrg.proxy.proxyClass;ImportOrg.proxy.interfaces.ILaoBan; Public classMishuImplementsilaoban{PrivateIlaoban Laoban; PublicMishu (Ilaoban laoban) { This. Laoban =Laoban; } Private voidbefore () {System.out.println ("Ticket Booking"); System.out.println ("Hotel Reservation"); System.out.println ("Send the Boss"); } Private voidAfter () {System.out.println ("Ticket Booking"); System.out.println ("Hotel"); System.out.println ("Pick up the Boss"); } @Override Public voidKaihui () {before (); Laoban.kaihui (); After (); } }
Mishu
Package test; Import Org.proxy.interfaces.ILaoBan; Import Org.proxy.interfaces.impl.LaoBan; Import Org.proxy.proxyClass.MiShu; Public class Test { publicstaticvoid main (String args[]) { New Mishu (new Laoban ()); Proxy_laoban.kaihui (); }}
The results of the operation are as follows:
--------Dynamic Agent
Package org.proxy.interfaces; // Boss Interface Public Interface Iboss { publicvoid Kaihui (); // The boss wants a meeting }
Iboss.java
Package Org.proxy.interfaces.impl; Import Org.proxy.interfaces.IBoss; // interface Implementation Class Public class Implements iboss{ publicvoid Kaihui () { System.out.println ("The boss wants a meeting "); }}
Boss.java
PackageOrg.proxy.proxyClass;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy;//Dynamic Proxy Classes Public classDynamicproxyImplementsinvocationhandler{PrivateObject obj; Publicobject bind (Object obj) { This. obj=obj; returnproxy.newproxyinstance (Obj.getclass (). getClassLoader (), Obj.getclass (). Getinterfaces (), This); } Publicobject Invoke (Object proxy, Method method, object[] objs)throwsthrowable {Object result=NULL; Try{before (); Result=method.invoke (obj, OBJS); After (); }Catch(Exception e) {e.printstacktrace (); } returnresult; } Public voidbefore () {System.out.println ("Great place to stay"); } Public voidAfter () {System.out.println ("Great place to stay in the airport"); }}
Dynamicproxy.java
Packagetest;ImportOrg.proxy.interfaces.IBoss;ImportOrg.proxy.interfaces.impl.Boss;ImportOrg.proxy.proxyClass.DynamicProxy; Public classTest { Public Static voidMain (String args[]) {Dynamicproxy proxy=NewDynamicproxy (); Iboss boss= (Iboss) proxy.bind (NewBoss ()); Boss.kaihui (); }}
Test.java
The results of the operation are as follows:
Spring IOC (DI) and AOP