java的反射原理與Spring的自動注入(轉載)

來源:互聯網
上載者:User

標籤:

Java反射原理與Spring的自動注入反射的定義

java的反射機制就是在運行狀態中,

  1. 對於任意一個類都能夠知道這個類的所有屬性和方法;
  2. 對於任意一個對象,都能夠調用它的任意一個方法和屬性。

這種動態擷取的資訊以及動態調用對象的方法的功能稱為java語言的反射機制。

Sping的自動注入原理

一個Bean的類:

public class User{    private String username;    private String mobile;    public String getUsername(){        return username;    }    public void setUsername(String username){        this.username = username;    }    public String getMobile(){        return mobile;    }    public void setMobile(String mobile){        this.mobile = mobile;    }}

驅動類:

import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;public class ReflectionTest {    @SuppressWarnings({ "unchecked", "rawtypes" })    public static void main(String[] args) throws Exception {        //id 為自己指定的值        String id = "user";        Map<String, Object> context = new HashMap<String, Object>();          //這裡是通過User.class.getName()擷取類名,通用的作法是通過設定檔擷取類名        //三種方法,可以獲得類名        //1. User.class.getName()        //2. new User().getName()        //3. Class.forName("User").getName()        Class c = Class.forName(User.class.getName());        Object object = c.newInstance();         // 類比spring容器          context.put(id, object);        Method method = c.getDeclaredMethod("setUsername", String.class);        // setter注入         method.invoke(object, "xiaoqiang");        System.out.println(((User) object).getUsername());    }}
對反射定義的理解代碼

在運行狀態中,

對任意一個類,都能夠知道這個類的所有方法和屬性

程式碼範例如下:

import java.lang.reflect.Field; import java.lang.reflect.Method;public class ReflectionTest {    @SuppressWarnings("rawtypes")    public static void main(String[] args) throws Exception {        Class c = Class.forName(User.class.getName());        System.out.println("------------------------------------");        //擷取所有User類下面定義的所有的方法        Method method[] = c.getDeclaredMethods();        for(int i=0;i<method.length;i++){            System.out.println(method[i].getName());        }        System.out.println("------------------------------------");        //擷取所有User類下面定義的所有的屬性        Field f[]=c.getDeclaredFields();        for(int i=0;i<f.length;i++){            System.out.println(f[i].getName());        }        System.out.println("------------------------------------");        //擷取所有User類下面定義的所有的方法以及父類的方法        Method method1[] = c.getMethods();        for(int i=0;i<method1.length;i++){            System.out.println(method1[i].getName());        }        System.out.println("------------------------------------");        //擷取所有User類下面定義的所有的屬性以及父類的屬性        Field f1[]=c.getDeclaredFields();        for(int i=0;i<f1.length;i++){            System.out.println(f1[i].getName());        }    }}
對於任意一個對象,都可以調用它的任一屬性和方法

代碼執行個體如下:

import java.lang.reflect.Field;public class ReflectTest{    public static void main(String[] args) throws Exception {    Class c = Class.forName(User.class.getName());    Object obj = c.newInstance();    Field field = c.getDeclaredField("username");    field.setAccessible(true);    field.set(object,"hehe");    System.out.println((User)object.getUsername());    }}

此時,username被設定為hehe。

參考資料:

java 反射,spring 自動注入原理

java的反射原理與Spring的自動注入(轉載)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.