论坛首页 Java企业应用论坛

菜鸟开发的一个小型Ioc容器,恳求大家指点

浏览 9960 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-05-11   最后修改:2013-05-11
本人是Java菜鸟一名,看过反射技术后,就练习写了一个小小的Ioc容器工具,请大家帮忙指点一下,先提前谢谢了.

地址:http://dl.vmall.com/c0u7lix55w

下面贴一个属性注入例子
1:A Java bean
package org.jmin.test.ioc.property;  
public class Man {  
  private String name;  
  private int age;  
  public int getAge() {  
   return age;  
  }  
  public void setAge(int age) {  
   this.age = age;  
  }  
  public String getName() {  
   return name;  
  }  
  public void setName(String name) {  
   this.name = name;  
  }  
}

2:Code of container call
package org.jmin.test.ioc.property;  

import org.jmin.ioc.BeanContainer;  
import org.jmin.ioc.BeanElementFactory;  
import org.jmin.ioc.BeanParameter;  
import org.jmin.ioc.BeanParameterFactory;  
import org.jmin.ioc.element.InjectionProperty;  
import org.jmin.ioc.impl.BeanContainerImpl;  
   
public class PropertyCase{  
   
  public static void test()throws Throwable{  
    BeanContainer container = new BeanContainerImpl();  
    BeanElementFactory beanElementFactory =container.getBeanElementFactory();  
    BeanParameterFactory beanParameterFactory =container.getBeanParameterFactory();  
    BeanParameter nameParmeter = beanParameterFactory.createStringParameter("Chris");  
    BeanParameter ageParmeter = beanParameterFactory.createIntegerParameter(28);  
      
    InjectionProperty nameProperty = beanElementFactory.createInjectionProperty("name",nameParmeter);  
    InjectionProperty ageProperty = beanElementFactory.createInjectionProperty("age",ageParmeter);  
    container.registerClass("Bean1", Man.class, new InjectionProperty[] {nameProperty, ageProperty});  
    Man man = (Man)container.getBean("Bean1");  
    if(man!=null){  
      if("Chris".equals(man.getName()) && (28== man.getAge())){  
        System.out.println("[Container].........Property injected success......... ");  
      }else{  
        throw new Error("[Container]...........Property injected failed............");  
      }  
     }  
   }  
   
   public static void main(String[] args)throws Throwable{  
     test();  
   }  
}

3:XML configuration
<?xml version="1.0"?>  
<beans>  
 <bean id ="Bean1" class="org.jmin.test.ioc.property.Man">  
  <property name="name" value="Chris"/>  
  <property name="age"  value="28"/>  
 </bean>  
</beans> 

package org.jmin.test.ioc.property;  
import org.jmin.ioc.impl.config.BeanContext;  
public class PropertyXMLCase{  
  public static void test()throws Throwable{  
    BeanContext context=new BeanContext("org/jmin/test/ioc/property/pojo.xml");  
    Man man = (Man)context.getBean("Bean1");  
    if(man!=null){  
      if("Chris".equals(man.getName()) && (28== man.getAge())){  
       System.out.println("[XML].........Property injected success..........");  
      }else{  
       throw new Error("[XML]...........Property injected failed............");  
      }  
    }  
  }  
      
  public static void main(String[] args)throws Throwable{  
   test();  
  }  
} 


更多代码可以从这里获取: http://dl.vmall.com/c0u7lix55w
   发表时间:2013-05-12  
spring ioc 很好用了
0 请登录后投票
   发表时间:2013-05-12   最后修改:2013-05-14
weng 写道
spring ioc 很好用了

Spring是不错了,我这里发布这个,主要是用来学习锻炼的,不敢奢望和Spring作比较
0 请登录后投票
   发表时间:2013-05-13  
JavaTear2014 写道
weng 写道
spring ioc 很好用了

Spring好用,难道就不可以有其他的Ioc工具存在?
照这样的逻辑,美国很多产品很好,难道中国同类企业都要关门,然后通通都去买美国产品? 太搞笑了吧.

在此发布这个产品,主要是和大家交流,共同进步,难道这也有错?

激动了哥们,没错,人家没说你有错
0 请登录后投票
   发表时间:2013-05-13  
JavaTear2014 写道
weng 写道
spring ioc 很好用了

Spring好用,难道就不可以有其他的Ioc工具存在?
照这样的逻辑,美国很多产品很好,难道中国同类企业都要关门,然后通通都去买美国产品? 太搞笑了吧.

在此发布这个产品,主要是和大家交流,共同进步,难道这也有错?


淡定
0 请登录后投票
   发表时间:2013-05-13  
从代码看,你只是简单的知道IOC能干什么, 还有很大的提升空间, 你这个谈不上工具, 更不能叫IOC容器
0 请登录后投票
   发表时间:2013-05-13   最后修改:2013-05-13
inta 写道
从代码看,你只是简单的知道IOC能干什么, 还有很大的提升空间, 你这个谈不上工具, 更不能叫IOC容器


首先非常感谢大家的回帖,尽管是批评。下面我想表达几点

1:问题的标题已经很清楚了,发布是希望得到大家的指点

2:这里只是一个属性注入的例子代码而已

3:到底是不是一个Ioc容器,请到地址:http://dl.vmall.com/c0u7lix55w 下载全部代码看看之后,
   再评论好吗?

4:热烈欢迎大家继续批评






0 请登录后投票
   发表时间:2013-05-13  
请大家继续批评,拍砖! 谢谢了!
0 请登录后投票
   发表时间:2013-05-13  
你太激动了,没人敢拍砖了
0 请登录后投票
   发表时间:2013-05-13  
楼主敏感,这不是好事!
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics