初学spring 关于IOC与AOP 使用的设计模式

Rod Johnson提出
interface21

改名为spring


IOC(Inverse of Control) 控制反转


以前程序中需要使用对象时,需要自己new一个,造成程序与对象的强耦合。
IOC 对象的生成交给spring容器完成


DI(Dependency Injection),依赖注入


IOC与DI一个意思,等价

 

AOP(Aspect-oriented Programming)面向切面编程

----------------------------------------------------------------------------------------------------------------------------------------------------------

Spring   IOC使用了工厂模式

初学spring 关于IOC与AOP 使用的设计模式

初学spring 关于IOC与AOP 使用的设计模式

初学spring 关于IOC与AOP 使用的设计模式

 

抽象产品:

Product.java

[java] view plain copy

  1. <span style="font-size:18px;">package com.test.simplefactory;  
  2.   
  3. public abstract class Product  
  4. {  
  5.       
  6. }  
  7. </span>  


具体产品:

ConcreteProductA.java

[java] view plain copy

  1. <span style="font-size:18px;">package com.test.simplefactory;  
  2.   
  3. public class ConcreteProductA extends Product  
  4. {  
  5.   
  6. }  
  7. </span>  

 

ConcreteProductB.java

[java] view plain copy

  1. <span style="font-size:18px;">package com.test.simplefactory;  
  2.   
  3. public class ConcreteProductB extends Product  
  4. {  
  5.   
  6. }  
  7. </span>  


工厂类

Creator.java

[java] view plain copy

  1. <span style="font-size:18px;">package com.test.simplefactory;  
  2.   
  3. public class Creator  
  4. {  
  5.     public static Product createProduct(String str)  
  6.     {  
  7.         if("A".equals(str))  
  8.         {  
  9.             return new ConcreteProductA();  
  10.         }  
  11.         else if("B".equals(str))  
  12.         {  
  13.             return new ConcreteProductB();  
  14.         }  
  15.         else  
  16.         {  
  17.             return null;  
  18.         }  
  19.     }  
  20. }  
  21. </span>  


具体应用

Client.java

[java] view plain copy

  1. <span style="font-size:18px;">package com.test.simplefactory;  
  2.   
  3. public class Client  
  4. {  
  5.     public static void main(String[] args)  
  6.     {  
  7.         Product productA = Creator.createProduct("A");  
  8.           
  9.         System.out.println(productA.getClass().getName());  
  10.           
  11.         Product productB = Creator.createProduct("B");  
  12.           
  13.         System.out.println(productB.getClass().getName());  
  14.     }  
  15. }  
  16. </span>  


 

 

----------------------------------------------------------------------

Spring   AOP使用了代理模式

初学spring 关于IOC与AOP 使用的设计模式

初学spring 关于IOC与AOP 使用的设计模式

 静态代理

 抽象角色

Subject.java

[java] view plain copy

  1. <span style="font-size:18px;">package com.test.proxy;  
  2.   
  3. // 抽象角色  
  4.   
  5. public abstract class Subject  
  6. {  
  7.     abstract public void request();  
  8. }  
  9. </span>  


 

 

代理角色    中介

ProxySubject.java

[java] view plain copy

  1. <span style="font-size:18px;">package com.test.proxy;  
  2.   
  3. //代理角色  
  4.   
  5. public class ProxySubject extends Subject  
  6. {  
  7.     private RealSubject realSubject; // 以真实角色作为代理角色的属性  
  8.   
  9.     public ProxySubject()  
  10.     {  
  11.     }  
  12.   
  13.     public void request() // 该方法封装了真实对象的request方法  
  14.     {  
  15.         preRequest();  
  16.   
  17.         if (realSubject == null)  
  18.         {  
  19.             realSubject = new RealSubject();  
  20.         }  
  21.   
  22.         realSubject.request(); // 此处执行真实对象的request方法  
  23.   
  24.         postRequest();  
  25.     }  
  26.   
  27.     private void preRequest()  
  28.     {  
  29.         // something you want to do before requesting  
  30.     }  
  31.   
  32.     private void postRequest()  
  33.     {  
  34.         // something you want to do after requesting  
  35.     }  
  36.   
  37. }  
  38. </span>  


 

真实角色   房东

RealSubject.java

[java] view plain copy

  1. <span style="font-size:18px;">package com.test.proxy;  
  2.   
  3. //真实角色:实现了Subject的request()方法  
  4.   
  5. public class RealSubject extends Subject  
  6. {  
  7.     public RealSubject()  
  8.     {  
  9.     }  
  10.   
  11.     public void request()  
  12.     {  
  13.         System.out.println("From real subject.");  
  14.     }  
  15.   
  16. }  
  17. </span>  


 

客户端调用    租客

Client.java

[java] view plain copy

  1. <span style="font-size:18px;">package com.test.proxy;  
  2.   
  3. //客户端调用  
  4.   
  5. public class Client  
  6. {  
  7.     public static void main(String[] args)  
  8.     {  
  9.         Subject sub = new ProxySubject();  
  10.   
  11.         sub.request();  
  12.     }  
  13. }  
  14. </span>  
  15. 初学spring 关于IOC与AOP 使用的设计模式


 

 -----------------------------------------------------------------------------------------------------

 动态代理:真实对象很多,不知道代理那个真实对象,动态代理代理的是接口

初学spring 关于IOC与AOP 使用的设计模式

初学spring 关于IOC与AOP 使用的设计模式

初学spring 关于IOC与AOP 使用的设计模式

 

抽象角色

Subject.java

[java] view plain copy

  1. <span style="font-size:18px;">package com.test.dynamicproxy;  
  2.   
  3. //抽象角色(之前是抽象类,此处应改为接口):   
  4.   
  5. public interface Subject  
  6. {  
  7.     public void request();  
  8. }  
  9. </span>  


 

 

代理角色    中介

DynamicSubject.java

[java] view plain copy

  1. <span style="font-size:18px;">package com.test.dynamicproxy;  
  2.   
  3. import java.lang.reflect.InvocationHandler;  
  4. import java.lang.reflect.Method;  
  5.   
  6. //代理处理器   
  7.   
  8. /** 
  9.  * 该代理类的内部属性为Object类,实际使用时通过该类的构造函数DynamicSubject(Object obj)对其赋值; 
  10.  * 此外,在该类还实现了invoke方法,该方法中的 method.invoke(sub,args); 
  11.  * 其实就是调用被代理对象的将要被执行的方法,方法参数sub是实际的被代理对象, args为执行被代理对象相应操作所需的参数。 
  12.  * 通过动态代理类,我们可以在调用之前或之后执行一些相关操作 
  13.  */  
  14.   
  15. public class DynamicSubject implements InvocationHandler  
  16. {  
  17.     private Object sub;  
  18.   
  19.     public DynamicSubject()  
  20.     {  
  21.     }  
  22.   
  23.     public DynamicSubject(Object obj)  
  24.     {  
  25.         sub = obj;  
  26.     }  
  27.   
  28.     public Object invoke(Object proxy, Method method, Object[] args)  
  29.             throws Throwable  
  30.     {  
  31.         System.out.println("before calling " + method);  
  32.   
  33.         method.invoke(sub, args);  
  34.   
  35.         System.out.println("after calling " + method);  
  36.   
  37.         return null;  
  38.     }  
  39.   
  40. }  
  41. </span>  


 

 

 

真实角色   房东

RealSubject.java

 

 

[java] view plain copy

  1. <span style="font-size:18px;">package com.test.dynamicproxy;  
  2.   
  3. //具体角色  
  4.   
  5. public class RealSubject implements Subject  
  6. {  
  7.   
  8.     public RealSubject()  
  9.     {  
  10.     }  
  11.   
  12.     public void request()  
  13.     {  
  14.         System.out.println("From real subject.");  
  15.     }  
  16.   
  17. }  
  18. </span>  

 

 

客户端调用    租客

Client.java

[java] view plain copy

  1. <span style="font-size:18px;">package com.test.dynamicproxy;  
  2.   
  3. import java.lang.reflect.InvocationHandler;  
  4. import java.lang.reflect.Proxy;  
  5.   
  6. //客户端  
  7.   
  8. public class Client  
  9. {  
  10.     public static void main(String[] args) throws Throwable  
  11.     {  
  12.         RealSubject rs = new RealSubject(); // 在这里指定被代理类  
  13.           
  14.         InvocationHandler ds = new DynamicSubject(rs);  
  15.           
  16.         Class<?> cls = rs.getClass();  
  17.   
  18.         // 以下是一次性生成代理  
  19.   
  20.         Subject subject = (Subject) Proxy.newProxyInstance(  
  21.                 cls.getClassLoader(), cls.getInterfaces(), ds);  
  22.   
  23.         subject.request();  
  24.     }  
  25. }  
  26. </span>  

初学spring 关于IOC与AOP 使用的设计模式

初学spring 关于IOC与AOP 使用的设计模式

初学spring 关于IOC与AOP 使用的设计模式

 

初学spring 关于IOC与AOP 使用的设计模式

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

AOP核心本质:                                                                                             

http://blog.csdn.net/fjseryi/article/details/13997951 

来源: https://blog.csdn.net/fjseryi/article/details/13540555