[置顶] 关于JAVA动态加载类[简单IOC的实现] -- vb2005xu自己动手

    xiaoxiao2022-06-30  74

    JAVA和PHP在动态加载类上有很大的不同,对于PHP来说,想要动态加载类只需,以下代码就可以实现,这个也可以简单实现为 一个单一入口的demo MVC.php class MVC{ var $cur_ctl ;//当前控制器 var $cur_act ;//当前action function MVC() { $this->cur_ctl = null ;$this->cur_act = null ; if (isset($_REQUEST['ctl']) && ! empty($_REQUEST['ctl'])) { $this->cur_ctl = $_REQUEST['ctl'] ; } if (isset($_REQUEST['act']) && ! empty($_REQUEST['act'])) { $this->cur_act = $_REQUEST['act'] ; } } function run() { $ctl_name = 'C_' ; //控制器前缀 $act_name = 'a_' ; //事件前缀 if (empty($this->cur_ctl)) $ctl_name = "{$ctl_name}Default" ; else $ctl_name = "{$ctl_name}{$this->cur_ctl}" ; if (empty($this->cur_act)) $act_name = "{$act_name}Index" ; else $act_name = "{$act_name}{$this->cur_act}" ; // exec this require "{$ctl_name}.php"; $ExecMVC = & new $ctl_name(); $ExecMVC->$act_name(); } } index.php require('MVC.php'); $mvc = & new MVC(); $mvc->run(); 控制器的实现例子 class C_Default { function C_Default() { } function a_index() { echo 'suess' ; } function a_bbs() { echo 'bbs' ; } } 就可以在 index.php中通过ctl和act来实现动态页面的跳转,例如index.php?act=bbs 但是对于JAVA来讲,就相对麻烦一些,这里给出一个简单的动态加载类的demo,希望对大家有所帮助: import java.util.Properties; import java.lang.ClassLoader ; import java.io.* ; /** * 读取JAVA资源配置文件中或命令参数中指定的接口实现类 */ public class MySimpleIoc { private ClassLoader myClassLoader ; private static MySimpleIoc ioc ; private Properties properties ;//配置文件 private MySimpleIoc() { myClassLoader = MySimpleIoc.class.getClassLoader() ; this.initConfFile() ; } public static MySimpleIoc getInstance() { if (ioc == null) ioc = new MySimpleIoc() ; return ioc ; } //加载指定的IOC配置文件./ioc.conf private void initConfFile() { try { // "/"表示以当前类的包的根路径为基准路径 InputStream in = MySimpleIoc.class.getResourceAsStream("./ioc.conf"); properties = new Properties(); properties.load(in); }catch(Exception e) { this.println ("IOC配置文件[./ioc.conf]读取失败,请检查") ; e.printStackTrace(); } } private void println(Object msg) { System.out.println (msg) ; } //clazz 是类名的全称在配置文件中对应的字符串 -- java.lang.String public Object getBean(String clazz){ Object implClass = null ; String implClassName = properties.getProperty(clazz) ; try { //这里可以扩展下,以防止出现空指针异常 使用一个缺省的测试类来做 implClass = this.myClassLoader.loadClass(implClassName).newInstance() ; } catch (ClassNotFoundException ex) { this.println ("Class: " + implClassName + "had not found : " + ex); ex.printStackTrace() ; } catch (InstantiationException ex) { this.println ("Class: " + implClassName + ".newInstance() reported InstantiationException : " + ex ); ex.printStackTrace() ; } catch (IllegalAccessException ex) { this.println ("Class: " + implClassName + "reported IllegalAccessException: " + ex); ex.printStackTrace() ; } return implClass ; } public static void main(String args[]) { I接口类型 iavs = null ; iavs = (I接口类型) MySimpleIoc.getInstance().getBean("配置文件中设置的接口实现类的标签名称"); System.out.println (iavs.toString() ) ; //这个是实现类的方法 System.out.println ("======================================") ; } } 配置文件ioc.conf同这个类文件应该在同一目录中,可以自行更改上面的文件路径 #要求类名的全称 -- eg. java.lang.String #attackerVarityServce attackerVarityService=vb2005xu.attacker.service.IAttackerVarityServce attackerVarityImpl=vb2005xu.attacker.service.impl.AttackerVarityImpl #attackerRecord attackerRecordService=vb2005xu.attacker.service.bo.AttackerRecord attackerRecordImpl=vb2005xu.attacker.dao.AttackerRecordImpl #attackerDBDao attackerDBDaoService=vb2005xu.attacker.dao.IAttackerDBDao attackerDBDaoImpl=vb2005xu.attacker.dao.db4o.AttackerDBDaoImpl
    转载请注明原文地址: https://ju.6miu.com/read-1126027.html

    最新回复(0)