spring IOC(DI)和AOP

軟件152譚智馗java

 

IOC(Inversion of Control,控制倒轉)Ioc意味着將你設計好的對象交給容器控制,而不是傳統的在你的對象內部直接控制。spring

DI—Dependency Injection,即「依賴注入」:組件之間依賴關係由容器在運行期決定,形象的說,即由容器動態的將某個依賴關係注入到組件之中。編程

依賴注入(DI)和控制反轉(IOC)是從不一樣的角度的描述的同一件事情,就是指經過引入IOC容器,利用依賴關係注入的方式,實現對象之間的解耦。緩存

AOP: Aspect-OrientedProgramming(面向切面編程)。AOP適合於那些具備橫切邏輯的應用:如性能監測,訪問控制,事務管理、緩存、對象池管理以及日誌記錄。AOP將這些分散在各個業務邏輯中的代碼經過橫向切割的方式抽取到一個獨立的模塊中。AOP 實現的關鍵就在於 AOP 框架自動建立的 AOP 代理,AOP 代理則可分爲靜態代理和動態代理兩大類,其中靜態代理是指使用 AOP 框架提供的命令進行編譯,從而在編譯階段就可生成 AOP 代理類,所以也稱爲編譯時加強;而動態代理則在運行時藉助於 JDK 動態代理、CGLIB 等在內存中「臨時」生成 AOP 動態代理類,所以也被稱爲運行時加強。app

 

IOC例子:框架

 

package cqvie.com;
interface USB //USB接口
{
  public void insert();
  public String read();
  public void write(String s);
  public void pop();
}
USB.java
package cqvie.com;
import org.springframework.stereotype.Component;
@Component
public class UDisk implements USB
{
  public void insert() {
      System.out.println("讀入U盤");
      }
  public String read(){
      System.out.println("讀取數據:"); 
      return "data";
  }
  public void write(String s){
      System.out.println("寫入數據");
  }
  public void pop(){
      System.out.println("彈出U盤");
  }
}
UDisk.java
package cqvie.com;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component("theComputer")
public class Computer
{
  @Autowired
  //@Qualifier("UDisk")
  private USB usbDevice;
  public Computer(){ }  //這裏不能構造USB設備
  public void saveData(String data) //實現智能設備的方法
  { //insert,write,pop
      usbDevice.insert();
      usbDevice.write(data);
      usbDevice.pop();
  }
  public void setUsbDevice(USB u){  usbDevice=u;  }   
}
Computer.java
package cqvie.com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
    public static void main(String[] args) {
          //Computer c=new Computer();
          //USB u=new UDisk();
          //c.setUsbDevice(u);
         ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
             Computer c=(Computer) ctx.getBean("theComputer");
             c.saveData("我的簡歷"); 
             
           Computer1 c1=new Computer1();
              HDD mp=new movHDD();
              c1.setMpDevice(mp);
              c1.saveData("移動硬盤");             
    }
}
test.java
<?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-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="u1" class="cqvie.com.UDisk"></bean>
    <bean id="theComputer" class="cqvie.com.Computer">
        <property name="usbDevice" ref="u1"></property>
    </bean>
</beans>
applicationContext.xml

運行結果以下:ide

 

AOP例子:性能

package org.proxy.interfaces;
//老闆接口
public interface ILaoBan {
    public void kaiHui();
}
package org.proxy.interfaces.impl;

import org.proxy.interfaces.ILaoBan;

//接口實現類
public class LaoBan implements ILaoBan{

    @Override
    public void kaiHui() {
       System.out.println("--->老闆要開會");
    }
}
LaoBan
package org.proxy.proxyClass;
import org.proxy.interfaces.ILaoBan;

public class MiShu implements ILaoBan{

    private ILaoBan laoban;
    public MiShu(ILaoBan laoban){
        this.laoban = laoban;
    }

    private void before() {    
        System.out.println("訂機票");
        System.out.println("訂酒店");
        System.out.println("送老闆");
    }
    
    private void after() {
        System.out.println("訂機票");
        System.out.println("退酒店");
        System.out.println("接老闆");
    }
    
    @Override
    public void kaiHui() {
         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 {
    public static void main(String args[]){
        ILaoBan proxy_laoban = new MiShu(new LaoBan());
        proxy_laoban.kaiHui();
    }
}

運行結果以下:ui

 

--------動態代理this

package org.proxy.interfaces;
//老闆接口
public interface IBoss {
    public void kaiHui();  //老闆要開會
}
IBoss.java
package org.proxy.interfaces.impl;

import org.proxy.interfaces.IBoss;

//接口實現類
public class Boss implements IBoss{

    public void kaiHui()
    {
      System.out.println("老闆要開會");
    }
}
Boss.java
package org.proxy.proxyClass;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//動態代理類
public class DynamicProxy implements InvocationHandler{
    private Object obj;
    public Object bind(Object obj){
        this.obj=obj;
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), 
                obj.getClass().getInterfaces(), this);
    }
    public Object invoke(Object proxy, Method method, Object[] objs)
            throws Throwable {
        Object result=null;
        try{
            before();
            result=method.invoke(obj, objs);
            after();
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }
    public void before(){
        System.out.println("訂機票,訂酒店,送老闆");
        }
    
    public void after(){
        System.out.println("訂機票,退酒店,接老闆");
    }
}
DynamicProxy.java
package test;
import org.proxy.interfaces.IBoss;
import org.proxy.interfaces.impl.Boss;
import org.proxy.proxyClass.DynamicProxy;


public class Test {
    public static void main(String args[]){
        DynamicProxy proxy=new DynamicProxy();
        IBoss boss=(IBoss) proxy.bind(new Boss());
        boss.kaiHui();
    }
}
Test.java

運行結果以下:

相關文章
相關標籤/搜索