Loonframework-Inversion of Control(简称LIoC)-0.1.0版发布

Loonframework-Inversion of Control(简称LIoC),是一款基于Java反射机制的“微型”Ioc实现,部分参考了另外一个IoC实现yan项目(http://yan.codehaus.org/),LIoC能够运行在JRE1.4及以上版本中(由于LIoC基于Java反射机制,即便是未签名的Applet也能够使用),构建LIoC的目地在于以尽可能简练的方式实现Java组件间的低耦合调用,LIoC对依赖注入的方法没有任何约束,它可以对javabean的getter/setter,或者有规则的method,乃至任意变量及它们的任意组合进行注入。(另外LIoC尚有一个需要在JRE1.5及以上版本运行的实现,外部接口与此版类似,内部使用了asm3.1类库及nio包,支持annotation与xml配置,开发中……)


下载地址:http://code.google.com/p/greenvm/downloads/list

 

下面我给出一些简单的应用实例:

 

以下是几个示例中使用到的类:

 

Bar.java

 

package org.loon.test; public class Bar { Foo foo; public Bar(Foo foo) { this.foo = foo; } public void greeting() { foo.greet(this); } public String toString() { return "bar"; } }

 

Foo.java

 

package org.loon.test; public class Foo { private final String hi; public Foo(String hi) { this.hi = hi; } public void greet(Object obj) { System.out.println(hi + " " + obj); } }

 

Bean.java

 

package org.loon.test; public class Bean implements IBean { String a; int b; short c; long d; public Bean() { } public Bean(String a, int b, short c, long d) { this.a = a; this.b = b; this.c = c; this.d = d; } public String getA() { return a; } public void setA(String a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public short getC() { return c; } public void setC(short c) { this.c = c; } public long getD() { return d; } public void setD(long d) { this.d = d; } public void call(String message){ System.out.println(message); } }

 

IBean.java

 

package org.loon.test; public interface IBean { void call(String message); String getA(); int getB(); short getC(); long getD(); }

 

实例一,简单的注入依赖:

 

package org.loon.test; import org.loon.framework.ioc.Ioc; import org.loon.framework.ioc.IocFactory; /** * Copyright 2008 - 2009 * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. * * @project loonframework * @author chenpeng * @email:ceponline@yahoo.com.cn * @version 0.1 */ public class Sample1 { public static void main(String[] args) { // 构造函数 Object[] constructor = { "hello" }; // 绑定指定类及构造函数 Ioc ioc = IocFactory.bind(Foo.class, constructor); // 获得指定类的实体对象 Foo foo = (Foo) ioc.getObject(); foo.greet("Testing"); // 释放资源 IocFactory.destroy(); } }

 

实例二,多组件联合操作:

 

package org.loon.test; import org.loon.framework.ioc.IocContainer; /** * Copyright 2008 - 2009 * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. * * @project loonframework * @author chenpeng * @email:ceponline@yahoo.com.cn * @version 0.1 */ public class Sample2 { public static void main(String[] args) { // 创建一个ioc容器 IocContainer container = new IocContainer(); // 添加字符串"hello" container.addValue("hello"); // 添加Foo的构造器Foo container.addConstructor(Foo.class); // 添加Bar的构造器 container.addConstructor(Bar.class); // 获得Bar的实例 Bar bar = (Bar) container.getInstance(Bar.class); bar.greeting(); // 释放资源 container.destroy(); } }

 

实例三,虚调用指定对象:

 

package org.loon.test; import java.io.PrintStream; import org.loon.framework.ioc.Ioc; import org.loon.framework.ioc.IocFactory; /** * Copyright 2008 - 2009 * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. * * @project loonframework * @author chenpeng * @email:ceponline@yahoo.com.cn * @version 0.1 */ public class Sample3 { public static void main(String[] args) { PrintStream out = System.out; // Ioc ioc = IocFactory.bind(Bean.class.getName(),new Object[]{"测试",new // Integer(1), new Short((short)2), new Long(3)}); // 以字符串方式获得Bean实例 Ioc ioc = IocFactory.bind(Bean.class.getName()); // PS:lfioc能够自动识别set、get方法,比如set字段a时,a、A、setA三种写法在lfioc中都是允许的。 // 为Bean中setA注入数据 ioc.setMethod("a", "加强纪律性,革命无不胜"); // 为setB注入数据 ioc.setMethod("setB", 1); try { // 执行方法getB out.println("字符串调用getB:" + ioc.doInvoke("b")); // 执行方法getA out.println("字符串调用getA:" + ioc.doInvoke("getA")); // 执行方法call ioc.doInvoke("call",new Object[]{"好好学习,天天向上"}); } catch (Exception e) { e.printStackTrace(); } out.println(); // 获得IBean接口 IBean itest = (IBean) ioc.getObject(); // 执行方法getA out.println("接口调用getA:" + itest.getA()); // 执行方法getB out.println("接口调用getB:" + itest.getB()); // 执行方法call itest.call("天天学习,好好向上"); } }

 

 

下载地址:http://code.google.com/p/greenvm/downloads/list

 

 

近期鄙人博客中总是在写关于Java游戏开发的教程,但是很遗憾没啥子人气……偶观察了一下丢在博客上的wowzio插件,发现很多网友都是查非Java游戏开发的内容转过来的|||……爱不够啊≡(▔﹏▔)≡,没动力了~~~于是非常缓慢的在写TLOH文档, 自己挖自己的坟, 又开始鼓捣Loonframework的非游戏部分|||……

 

不过我知道我国有部分人士以“使用轮子为荣,发明轮子为耻”,所以同样不寄希望于有什么人使用乃至于完善这个组件≧△≦,不过愚以为这里面部分针对class操作的函数吾辈写的还是比较省事的,有需要的可以拆出去单用,反正是开源的……但是,偶很“厚道”的将注释几乎都删了,想拆就自己琢磨吧,吼吼(-__-)b……

 


posted on 2009-05-25 23:40 cping 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/cping1982/archive/2009/05/25/2257923.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值