您的位置:首页 > 编程语言 > Java开发

Java微容器

2013-04-12 15:39 113 查看
Java微容器

1:基于Bean元素定义方式构造

2:提供友好的使用接口

3:扩展性好,支持类型转换器

4:代码结构清晰,可读性好

5:工具极少依赖外部Jar包

6:编译压缩后体积小(170k),不臃肿.

7:提供容器内部原理说明文档

8:提供参考例子代码,包括两种方式,A:直接调用容器,B:XML配置方式

9:100%国人原创轻量级Ioc工具

国内下载: http://pan.baidu.com/share/home?uk=2218126399

这里例举一个属性注入测试代码

1:定义一个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: 直接调用容器获取Bean实例

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].........属性注入测试成功..........");
}else{
throw new Error("[Container]...........属性注入测试失败............");
}
}
}

//启动测试方法
public static void main(String[] args)throws Throwable{
test();
}
}


3:XML调用方式

<?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].........属性注入测试成功..........");
}else{
throw new Error("[XML]...........属性注入测试失败............");
}
}
}

//启动测试方法
public static void main(String[] args)throws Throwable{
test();
}
}

更多代码下载地址

国内下载: http://pan.baidu.com/share/home?uk=2218126399
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: