java反射機制是在運行狀態中,對於任意一個類,都能知道這個類的全部屬性和方法,java
對於任意一個對象可以調用它的任意方法和屬性,ide
這種動態獲取信息以及動態調用對象方法的功能稱爲java語言的反射機制。函數
反射生成對象的核心代碼,經過經過類加載器註冊一個類的全限定名,而後經過newInstace()方法建立了一個對象。測試
obj = (ReflectServiceImpl) Class.forName("basic_class_01.ReflectServiceImpl").newInstance();
反射方法的核心代碼this
Method method = target.getClass().getMethod("sayHello", String.class);
import java.lang.reflect.Method; public class ReflectServiceImpl{ public void sayHello(String name){ System.out.println("Hello" + name); } // 經過反射生成對象 public ReflectServiceImpl getInstance(){ ReflectServiceImpl obj = null; try { obj = (ReflectServiceImpl) Class.forName("basic_class_01.ReflectServiceImpl").newInstance(); } catch (Exception e) { e.printStackTrace(); } return obj; } // 獲取和反射方法 public Object reflectMethod(){ Object returnObj = null; ReflectServiceImpl target = new ReflectServiceImpl(); try{ // 第一個參數爲方法名稱,第二參數爲參數類型 Method method = ReflectServiceImpl.class.getMethod("sayHello", String.class); // 也能夠寫爲 // Method method = target.getClass().getMethod("sayHello", String.class); //第一個參數爲那個對象調用方法,第二個參數爲方法參數 returnObj = method.invoke(target, "zhangsan"); }catch (Exception e){ e.printStackTrace(); } return returnObj; } // 反射方法和反射對象 public Object reflect(){ ReflectServiceImpl obj = null; try { obj = (ReflectServiceImpl) Class.forName("basic_class_01.ReflectServiceImpl").newInstance(); Method method = obj.getClass().getMethod("sayHello", String.class); method.invoke(obj, "zhangsan"); } catch (Exception e) { e.printStackTrace(); } return obj; } }
反射帶參構造函數對象的核心代碼,經過經過類加載器註冊一個類的全限定名,而後經過getConstructor()方法,方法中參數能夠有多個,最後經過newInstace()方法建立了一個對象。spa
obj = (ReflectServiceImpl2) Class.forName("basic_class_01.ReflectServiceImpl2").getConstructor(String.class).newInstance("zhangsan");
public class ReflectServiceImpl2 { private String name; public ReflectServiceImpl2(String name) { this.name = name; } public void sayHello(String name){ System.out.println("Hello" + name); } // 經過反射生成帶有參數的構造方法 public ReflectServiceImpl2 getInstance(){ ReflectServiceImpl2 obj = null; try { obj = (ReflectServiceImpl2) Class.forName("basic_class_01.ReflectServiceImpl2").getConstructor(String.class).newInstance("zhangsan"); } catch (Exception e) { e.printStackTrace(); } return obj; } }
反射優勢:能夠解除程序的耦合度,比較靈活。代理
反射缺點:運行比較慢。code
代理分爲兩個步驟:對象
1.代理對象和真實對象創建代理關係。blog
2.實現代理對象的代理邏輯方法。
JDK動態代理是java.lang.reflect.*包提供的方式,它必須藉助接口才能產生代理對象,因此先定義接口
public interface HelloWorld { void sayHelloWorld(); }
定義接口的實現類
public class HelloWorldImpl implements HelloWorld { @Override public void sayHelloWorld() { System.out.println("Hello World"); } }
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class JdkProxyExample implements InvocationHandler { // 真實對象 private Object target = null; /** * 創建代理對象和真實對象的代理關係,並返回代理對象 * @param target 真實對象 * @return 代理對象 */ public Object bind(Object target) { this.target = target; /* 第一個參數:target的類加載器 第二參數:target的接口類 第三個參數:定義實現方法邏輯的代理類,也就是實現InvocationHandler接口invoke方法的類 */ return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this); } /** * 代理方法邏輯 * @param proxy 代理對象 * @param method 當前調度方法 * @param args 當前方法參數 * @return 代理結果返回 * @throws Throwable 異常 */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("進入代理邏輯方法"); System.out.println("在調度真實對象以前的服務"); Object obj = method.invoke(target, args); // 至關於調用sayHelloWorld方法 System.out.println("在調度真實對象以後的服務"); return obj; } // 測試jdk動態代理 public void testJdkProxy(){ JdkProxyExample jdk = new JdkProxyExample(); // 綁定關係,由於掛在接口HelloWorld下,因此聲明代理對象HelloWorld proxy,獲取代理對象 HelloWorld proxy = (HelloWorld) jdk.bind(new HelloWorldImpl()); // 代理對象調用方法 proxy.sayHelloWorld(); } public static void main(String[] args) { JdkProxyExample jdkProxyExample = new JdkProxyExample(); jdkProxyExample.testJdkProxy(); } }
jdk動態代理必須提供接口才能使用,cglib不須要提供接口,只要一個非抽象類就能實現動態代理。
import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; import java.lang.reflect.Method; public class CglibProxyExample implements MethodInterceptor { /** * 生成CGLIB代理對象 * @param cls Class對象 * @return Class類的CGLIB代理對象 */ public Object getProxy(Class cls){ // CGLIB enhancer 加強類對象 Enhancer enhancer = new Enhancer(); // 設置加強類型 enhancer.setSuperclass(cls); // 定義代理邏輯對象爲當前對象,要求當前對象實現MethodInterceptor方法 enhancer.setCallback(this); // 生成並返回當前對象 return enhancer.create(); } /** * 代理邏輯方法 * @param proxy 代理對象 * @param method 當前調度方法 * @param args 當前方法參數 * @param methodProxy 方法代理 * @return 代理邏輯返回 * @throws Throwable 異常 */ @Override public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { System.out.println("調用真實方法前"); // CGLIB反射調用真實對象方法 Object res = methodProxy.invokeSuper(proxy, args); System.out.println("調用真實方法後"); return res; } // 測試cglib動態代理 public void testCGLIBProxy(){ CglibProxyExample cpe = new CglibProxyExample(); // 獲取代理對象 ReflectServiceImpl obj = (ReflectServiceImpl) cpe.getProxy(ReflectServiceImpl.class); // 代理對象執行方法 obj.sayHello("zhangsan"); } public static void main(String[] args) { CglibProxyExample cglibProxyExample = new CglibProxyExample(); cglibProxyExample.testCGLIBProxy(); } }