IOC基于Java底层的反射机制实现
反射机制:
核心:
Class cls = Class.forName(类名); 
Class ptypes[] = new Class[2]; 
ptypes[0] = Class.forName("java.lang.String"); 
ptypes[1] = Class.forName("java.util.Hashtable"); 
//参数调用   重点必须指定方法名,和相应的参数数量和类型
Method method = cls.getMethod("testMethod", ptypes); 

如何知道参数类型数量:
Method []m= cls.getMethods(); 
if(m!=null &&m.length>0) 

    for (int i = 0; i < m.length; i++) { 
        if(m[i].getName().equals("testMethod")){ 
            Parameter []p=m[i].getParameters(); 
            if(p!=null && p.length>0) 
              { 
                    for (Parameter parameter : p) { 
                    System.out.println(parameter.getType().getName()); 
                } 
            } 
        } 
    } 

如何调用:
Object args[] = new Object[2]; 
args[0] = "hello, my dear!"; 
Hashtable<String, String> ht = new Hashtable<String, String>(); 
ht.put("name", "阿蜜果"); 
args[1] = ht; 
String returnStr = (String) method.invoke(new ReflectionTest(), args); 
System.out.println("returnStr= " + returnStr); 

例子:
package com.gillion.javaReflection;

import java.awt.Button; 
import java.lang.reflect.Method; 
import java.lang.reflect.Parameter; 
import java.util.Hashtable; 
/** 
* @Description Java的反射机制 
* @author huyuangui@aliyun.com 
* @time 2015年1月8日 下午4:53:24 
* @version 1.0.0 
*/ 
public class ReflectionTest {

/** 
* 入口 
* @param args 
* @throws Exception 
*/ 
public static void main(String[] args) throws Exception { 
ReflectionTest reflection = new ReflectionTest(); 
reflection.getNameTest(); 
System.out.println(""); 
reflection.getMethodTest(); 
}

/** 
* 获取类名 全路径 
* @throws Exception 
*/ 
public void getNameTest() throws Exception { 
System.out.println("===========begin getNameTest============"); 
String name = "阿蜜果"; 
Class cls = name.getClass(); 
System.out.println("String类名: " + cls.getName()); 
Button btn = new Button(); 
Class btnClass = btn.getClass(); 
System.out.println("Button类名: " + btnClass.getName()); 
Class superBtnClass = btnClass.getSuperclass(); 
System.out.println("Button的父类名: " + superBtnClass.getName()); 
Class clsTest = Class.forName("java.awt.Button"); 
System.out.println("clsTest name: " + clsTest.getName()); 
System.out.println("===========end getNameTest============"); 
}

public void getMethodTest() throws Exception { 
System.out.println("===========begin getMethodTest=========="); 
Class cls = Class.forName("com.gillion.javaReflection.ReflectionTest"); 
Class ptypes[] = new Class[2]; 
ptypes[0] = Class.forName("java.lang.String"); 
ptypes[1] = Class.forName("java.util.Hashtable"); 
//参数调用 
Method method = cls.getMethod("testMethod", ptypes); 
/*** 
* 需要注入哪些参数,参数类型是什么,有多少个参数 
*/ 
Method []m= cls.getMethods(); 
if(m!=null &&m.length>0) 

for (int i = 0; i < m.length; i++) { 
if(m[i].getName().equals("testMethod")){ 
Parameter []p=m[i].getParameters(); 
if(p!=null && p.length>0) 

for (Parameter parameter : p) { 
System.out.println(parameter.getType().getName()); 





Object args[] = new Object[2]; 
args[0] = "hello, my dear!"; 
Hashtable<String, String> ht = new Hashtable<String, String>(); 
ht.put("name", "阿蜜果"); 
args[1] = ht;

String returnStr = (String) method.invoke(new ReflectionTest(), args); 
System.out.println("returnStr= " + returnStr); 
System.out.println("===========end getMethodTest=========="); 
}

public String testMethod(String str, Hashtable ht) throws Exception { 
String returnStr = "返回值"; 
System.out.println("测试testMethod()方法调用"); 
System.out.println("str= " + str); 
System.out.println("名字= " + (String) ht.get("name")); 
System.out.println("结束testMethod()方法调用"); 
return returnStr; 

 
 

(满满的是硬货)Spring深入研究一IOC实现的更多相关文章

  1. spring源码浅析——IOC

    =========================================== 原文链接: spring源码浅析--IOC   转载请注明出处! ======================= ...

  2. spring jpetstore研究入门(zz)

    spring jpetstore研究入门 分类: java2008-12-21 23:25 561人阅读 评论(2) 收藏 举报 springstrutsibatissearchweb框架servle ...

  3. spring(一)IOC & AOP

    参考文档: spring详解:http://www.cnblogs.com/ysocean/p/7466191.html(可以说非常详细了) aop源码详解:https://www.cnblogs.c ...

  4. spring.net中的IOC和DI-初使用

    前面准备:下载spring.net并解压 下载地址:spring.net下载地址 Ioc:控制反转         DI:依赖注入 一.IOC(控制反转) 1.新建一个控制台程序springTest, ...

  5. spring 学习 AOP和IOC

    自11开始接触三大框架,至今已俞5载, 当时风光无限的ssh,现在还在被广泛使用,并有扩大之势的只有spring了 spring主要特性,是广为使用的AOP(面向切面)和IOC(控制反转) 1.其中, ...

  6. spring学习(01)之IOC

    spring学习(01)之IOC IOC:控制反转——Spring通过一种称作控制反转(IOC)的技术促进了低耦合.当应用了IOC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创 ...

  7. spring源码分析---IOC(1)

    我们都知道spring有2个最重要的概念,IOC(控制反转)和AOP(依赖注入).今天我就分享一下spring源码的IOC. IOC的定义:直观的来说,就是由spring来负责控制对象的生命周期和对象 ...

  8. Spring框架学习之IOC(二)

    Spring框架学习之IOC(二) 接着上一篇的内容,下面开始IOC基于注解装配相关的内容 在 classpath 中扫描组件 <context:component-scan> 特定组件包 ...

  9. Spring框架学习之IOC(一)

    Spring框架学习之IOC(一) 先前粗浅地学过Spring框架,但当时忙于考试及后期实习未将其记录,于是趁着最近还有几天的空闲时间,将其稍微整理一下,以备后期查看. Spring相关知识 spri ...

随机推荐

  1. Excel 2007 批量删除隐藏的文本框

    该方法取自百度知道,该朋友给出函数,我详细写一下方法. 打开有文本框的excel文件. 按 Alt+F11 打开编辑器. 将下面的函数复制进去: Sub deltxbox()Dim s As Shap ...

  2. Linux and the Device Tree

    来之\kernel\Documentation\devicetree\usage-model.txt Linux and the Device Tree ----------------------- ...

  3. iOS_XCode7_Launch Image 的初使用

    之前一直没有做笔记的习惯,后来发现很多知识不常用,容易忘了,再去网上找的,很浪费时间,现在尝试着自己写写博客,可以是自己笔记,方便以后查询,如果有纰漏,欢迎指正. 1.Xcode7中 ,assets. ...

  4. ES6 笔记

    1.箭头函数 箭头函数里的this会引用 定义 箭头函数时,外部作用域 的 this .箭头函数只是 引用 外部作用域的 this ,本身不存在 this.同时因为没有 this ,所以 无法用new ...

  5. org.eclipse.swt.custom.StyledText.getScrollbarsMode()I

    错误: org.eclipse.swt.custom.StyledText.getScrollbarsMode()I 解决方法: 1 卸载,并手工清除myeclipse全部文件 2 重新安装myecl ...

  6. sql 游标

    --创建游标 DECLARE cursor_name CURSOR [ LOCAL | GLOBAL ] [ FORWARD_ONLY | SCROLL ] [ STATIC | KEYSET | D ...

  7. Java项目JUnit简单使用

    前面自己写了一个计算器,准备用在项目里 http://www.cnblogs.com/blog5277/p/5707304.html 由于项目是用户计算跟钱有关的,所以这可不敢出BUG 于是就用了JU ...

  8. localhost访问错误Forbidden You don't have permission to access / on this server.解决办法(亲测)

    在httpd.conf文件下找到这段: <Directory /> Options FollowSymLinks AllowOverride None Order deny,allow D ...

  9. 实验一 DOS

    实验一.DOS实验 一.           实验目的 DOS(Disk Operating System)是一个使用得十分广泛的磁盘操作系统,就连眼下流行的Windows9x/ME系统都是以它为基础 ...

  10. qt开源社区学习

    http://bbs.qter.org/forum.php?mod=forumdisplay&fid=52