自己动手打造ICO容器
来源:互联网 发布:ubuntu下载torrent 编辑:程序博客网 时间:2024/05/16 18:03
需要五个类
(注解类)
(主函数用于测试)
接口
(容器类),需要用此容器管理的类全部集成此类
package com.metarnet.extend;
import java.lang.reflect.Field;
import com.metarnet.Injects.Inject;
/**
* 容器
*/
public class IOC {
/**
* 初始化
* @param <T>
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public void Init() throws Exception
{
Field[] fields = getClass().getDeclaredFields();
for(Field field : fields){
Inject inject=field.getAnnotation(Inject.class);
if(inject!=null)
{
field.setAccessible(true);
field.set(this,Class.forName(inject.ClassName().trim()).newInstance());
}
}
}
}
(注解类)
/**
*
*/
package com.metarnet.Injects;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 类的依赖注入
* @author yanfan
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Inject {
public String ClassName();
}
(主函数用于测试)
package com.metarnet.Main;
import com.metarnet.Injects.Inject;
import com.metarnet.Interfaces.Interface1;
import com.metarnet.extend.IOC;
public class TestIOC extends IOC{
@Inject(ClassName = "com.metarnet.Interfaces.imps.Interface1Impl")
private Interface1 interface1;
@Inject(ClassName = "com.metarnet.Interfaces.imps.Interface1Impl2")
private Interface1 interface2;
public Interface1 getInterface1() {
return interface1;
}
public void setInterface1(Interface1 interface1) {
this.interface1 = interface1;
}
public static void main(String[] args) {
TestIOC ioc = new TestIOC();
try {
ioc.Init();
} catch (Exception e) {
e.printStackTrace();
}
ioc.interface1.SayHello();
ioc.interface2.SayHello();
}
}
接口
package com.metarnet.Interfaces;
public interface Interface1{
public void SayHello();
}
继承接口类1package com.metarnet.Interfaces.imps;
import com.metarnet.Interfaces.Interface1;
public class Interface1Impl implements Interface1{
@Override
public void SayHello() {
System.out.println("Hello World,My Name Is Interface1Impl");
}
}
继承接口类2package com.metarnet.Interfaces.imps;
import com.metarnet.Interfaces.Interface1;
public class Interface1Impl2 implements Interface1{
@Override
public void SayHello() {
System.out.println("Hello World,My Name Is Interface1Impl2");
}
}
另附资源下载地址 http://download.csdn.net/detail/yanfan0916/4773699
- 自己动手打造ICO容器
- 自己动手打造ORM(一)
- 自己动手打造PE系统
- 自己动手开发IOC容器
- 自己动手,丰衣足食,打造 Pager 控件
- 自己动手打造精确网络时钟
- 自己动手打造田园小窝
- 自己动手打造ajax图片上传
- 使用ico容器进行框架设计
- 使用ico容器进行框架设计
- spring的ico容器 ApplicationContext.xml
- 自己动手用VB打造桌面小钟
- 自己动手打造嵌入式Linux软硬件开发环境
- 深入浅出PE文件格式---自己动手打造PE Show
- 自己动手打造嵌入式Linux软硬件开发环境
- 自己动手打造企业级短信平台(上)
- 自己动手打造企业级短信平台(下)
- 自己动手打造嵌入式Linux软硬件开发环境
- ExtJS中使用vtype
- MongoDB的简单QPS测试(单机)
- QB7 vs VB2010 Console 程序效率对比~
- (24)21.2.13 线程组
- 通过ExtJS FormPanel提交数据给struts action
- 自己动手打造ICO容器
- 任务five咋就一个数
- [Script]如何获取物料的成本
- 串行接口
- 趣文:通俗解释主要编程语言及其用途
- SSH2 + ExtJS + JSON
- linux新手必须知道的十个知识点
- Mysql的简单QPS测试(单机)
- 常见浏览器内核介绍~