一、需求

  实现一个简易的IOC容器,管理Bean,从IOC容器的BeanFactory中获取实例,从而取代自己new实例的做法。

二、实现步骤分析

  

三、具体代码实现

  自定义注解类 MyComponent 和 MyAutowired:

 package MyIOCAndMyAop.Annotations;

 import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyComponent { } 
 package MyIOCAndMyAop.Annotations;

 import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAutowired { }

  MyIOC容器的实现:自己实现简单的IOC容器,来管理bean:BeanFactory<String, Object>,String为全类名,Object为通过类加载器加载进来的Class对象反射创建的bean。

 package MyIOCAndMyAop;

 import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import MyIOCAndMyAop.Annotations.MyAutowired;
import MyIOCAndMyAop.Annotations.MyComponent; public class MyIOC { // beanFactory 要声明为类变量,因为它不能被GC回收:
private static HashMap<String, Object> beanFactory = new HashMap<>(); /**
* 随着MyIOC类被加载到内存进行初始化,就会执行其静态代码块
* @param args
*/
static {
init();
} /**
* 获取BeanFactory
* @return
*/
public static HashMap<String, Object> getBeanFactory(){
return beanFactory;
} /**
* 根据全类名更新BeanFactory中的bean
* @param typeName
* @param proxyInstance
*/
public static void updateBeanFromBeanFactory(String typeName, Object proxyInstance) {
beanFactory.put(typeName, proxyInstance);
} /**
* 通过全类名获得对应的实例
* @param completeClassName
* @return
*/
public static Object getBean(String completeClassName) {
return beanFactory.get(completeClassName);
} public static void init() {
HashMap<String, Class> loadedClazzList;//<全类名, Class对象>
try {
//1.加载指定的类
File file = new File("C:\\workplace\\test\\bin");//!!!这里写死了路径不合适,可以做改进
loadedClazzList = loadAllClazz(file); //2.实例化并放入IOC容器中:对于那些有注解的类,做实例化
newInstance(loadedClazzList); // 3.完成依赖注入
autoWired(); // 4.测试:找到beanFactory中的某个bean,并执行其某个方法 ===> 这里有个问题,只能执行指定的方法,所以beanFactory中的所有bean都得有这个方法,这里先这么做了,但这明显不合理。
// test();
} catch (Exception e) {
e.printStackTrace();
}
} public static void test() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
for(Map.Entry<String, Object> entry : beanFactory.entrySet()) {
// System.out.println(entry.getKey() + " ---> ");
Method method = entry.getValue().getClass().getMethod("test");
method.invoke(entry.getValue());
}
} /**
* 对BeanFactory中管理的所有bean完成依赖注入。
* 交给IOC容器管理的类,需要注入成员变量,如果该成员变量是自定义的类,该类也是需要交给IOC容器管理的。
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws MalformedURLException
* @throws ClassNotFoundException
*/
public static void autoWired() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, MalformedURLException {
for(Map.Entry<String, Object> entry : beanFactory.entrySet()) {
Field[] fields = entry.getValue().getClass().getDeclaredFields();//!!!getFields():只能获取到运行时类中及其父类中声明为public的属性;getDeclaredFields():获取运行时类本身声明的所有的属性
for(Field field : fields) {
Annotation[] annotations = field.getAnnotations();
for(int i = 0; i < annotations.length; i++) {
if(annotations[i].annotationType() == MyAutowired.class) {
//从beanFactory中找到相应的bean,赋值给该成员变量,以完成依赖注入。
Object object = beanFactory.get(field.getType().getTypeName());
// System.out.println(field.getType().getTypeName());//MyIOCAndMyAop.bean.Student
//通过Field(反射)为成员变量赋值:
field.setAccessible(true);
field.set(entry.getValue(), object);
}
}
}
}
} /**
* 实例化: 放到loadedClazzlist集合中的Class对象都是需要做实例化的(加了@MyComponent注解的类)
*/
public static void newInstance(HashMap<String, Class> loadedClazzList) throws InstantiationException, IllegalAccessException, ClassNotFoundException, MalformedURLException {
for(Map.Entry<String, Class> entry : loadedClazzList.entrySet()) {
beanFactory.put(entry.getKey(), entry.getValue().newInstance());
}
} /**
* 加载指定路径下的类。
* 类加载:javase/src/classLoader/a01helloworld/A03GetExtClassLoader
* @return 类加载器加载进来的指定路径下的所有Class对象
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static HashMap<String, Class> loadAllClazz(File file) throws ClassNotFoundException, MalformedURLException, InstantiationException, IllegalAccessException {
//用于存放类加载器加载进来的Class对象<全类名, Class对象>
HashMap<String, Class> loadedClazzList = new HashMap<>(); URL[] urls = new URL[]{file.toURI().toURL()};
URLClassLoader classLoader = new URLClassLoader(urls); ArrayList<String> allCompleteClassName = getAllCompleteClassName(file); for(String element : allCompleteClassName) {
Class<?> clazz = classLoader.loadClass(element);
Annotation[] annotations = clazz.getAnnotations();// !!!拿到Class对象的时候,就进行筛选出有注解的Class再放到容器中,而不是把指定路径下的所有类都加载进来。
for(int i = 0; i < annotations.length; i++) {
if(annotations[i].annotationType() == MyComponent.class) {
loadedClazzList.put(element, clazz);//得到各个类对象了
}
}
}
return loadedClazzList;
} /**
* 得到allNeedLoadClassFiles中所有要加载的class文件的全类名
*/
private static ArrayList<String> getAllCompleteClassName(File file) {
// 所有要加载的class的全类名,如:classLoader.a02myclassloader.bean.Bean
ArrayList<String> completeClassNames = new ArrayList<>();
// 用于存放指定路径下所有要加载的class文件
ArrayList<File> allNeedLoadClassFiles = new ArrayList<File>(); getAllNeedLoadClassFile(file, allNeedLoadClassFiles); for (File element : allNeedLoadClassFiles) {
String filePath = element.getPath().replace("\\", "."); if(filePath.endsWith(".class")) {
//filePath.indexOf("bin.")+4:"bin."之后。filePath.lastIndexOf(".class"):".class"之前,该方法是从后往前找,性能更高。
String completeClassName = filePath.substring(filePath.indexOf("bin.")+4, filePath.lastIndexOf(".class"));
completeClassNames.add(completeClassName);
}
}
return completeClassNames;
} /**
* 通过递归获取指定路径下所有要加载的class文件
* 递归:javase/src/recursion/A_PrintFolder
* @param file
*/
private static ArrayList<File> getAllNeedLoadClassFile(File file, ArrayList<File> allNeedLoadClassFiles) {
if(!file.exists()) {//!!!这里要多一层判断
return allNeedLoadClassFiles;
} if (file.isDirectory()) {//是文件夹
File[] listFiles = file.listFiles();
for (File element : listFiles) {
getAllNeedLoadClassFile(element, allNeedLoadClassFiles);
}
} else {//是文件
allNeedLoadClassFiles.add(file);
}
return allNeedLoadClassFiles;
}
}

自己实现AOP 1.0版本,含步骤分解:https://www.cnblogs.com/laipimei/p/11137250.html

自己实现SpringAOP 2.0版本,含实现步骤分解:https://www.cnblogs.com/laipimei/p/11163377.html

自己实现IOC容器,java代码实现简易版IOC容器,IOC容器实现的步骤分解的更多相关文章

  1. [java代码库]-简易计算器(第二种)

    [java代码库]-简易计算器(第二种) 第二种方案:在程序中不使用if/switch……case等语句,完成计算器功能. <html> <head> <title> ...

  2. Java 语言实现简易版扫码登录

    基本介绍 相信大家对二维码都不陌生,生活中到处充斥着扫码登录的场景,如登录网页版微信.支付宝等.最近学习了一下扫码登录的原理,感觉蛮有趣的,于是自己实现了一个简易版扫码登录的 Demo,以此记录一下学 ...

  3. 纯css爱心代码-最近超级火的打火机与公主裙中的爱心代码(简易版)

    theme: cyanosis 最近打火机与公主裙中的爱心代码超级火,看着特别心动,让俺用css来写个简易版!!! 先看效果: 代码拆解: 主要是分为3大部分 分子颗粒 爱心 动画 代码实现: 分子颗 ...

  4. 10行代码实现简易版的Promise

    实现之前,我们先看看Promise的调用 const src = 'https://img-ph-mirror.nosdn.127.net/sLP6rNBbQhy0OXFNYD9XIA==/79910 ...

  5. [java代码库]-简易计算器(第一种)

    简易计算器(效果如图所示) 第一种方案:采用Javascript+html完成计算器,支持+-*/,结果显示不允许使用input输入域(可以考虑使用<span>) <html> ...

  6. JAVA实现二叉树(简易版--实现了二叉树的各种遍历)

    1,个人感觉二叉树的实现主要还是如何构造一颗二叉树.构造二叉树函数的设计方法多种多样,本例采用 addNode 方法实现.以下程序通过定义内部类来表示二叉树的结点,然后再实现了二叉树这种数据结构的一些 ...

  7. Jenkins日常运维笔记-重启数据覆盖问题、迁移、基于java代码发版(maven构建)

    之前在公司机房部署了一套jenkins环境,现需要迁移至IDC机房服务器上,迁移过程中记录了一些细节:1)jenkins默认的主目录放在当前用户家目录路径下的.jenkins目录中.如jenkins使 ...

  8. Java与Scala的两种简易版连接池

    Java版简易版连接池: import java.sql.Connection; import java.sql.DriverManager; import java.util.LinkedList; ...

  9. 从 Java 代码逆向工程生成 UML 类图和序列图

    from:http://blog.itpub.net/14780914/viewspace-588975/ 本文面向于那些软件架构师,设计师和开发人员,他们想使用 IBM® Rational® Sof ...

随机推荐

  1. 广义线性模型(Generalized Linear Model)

    广义线性模型(Generalized Linear Model) http://www.cnblogs.com/sumai 1.指数分布族 我们在建模的时候,关心的目标变量Y可能服从很多种分布.像线性 ...

  2. 还在羡慕BAT等公司的大流量的架构吗,commonrpc 是一个以netty 传输协议框架为基础(支持FTP)

    还在羡慕BAT等公司的大流量的架构吗?让你的java系统引用解耦,互相独立,commonrpc 就可以办到.commonrpc 是一个以netty 传输协议框架为基础, 自定义 spring shce ...

  3. python中的命名空间以及函数的嵌套

    一.动态传参 函数的形参中除了默认值参数和位置参数外,还有动态传参.当不确定形参有几个或者有很多的时候,就可以使用动态传参. 1.1 动态接收位置参数 在python中使用*来接收动态位置参数 def ...

  4. java多线程之线程安全

    线程安全和非线程安全是多线程的经典问题,非线程安全会在多个线程对同一个对象并发访问时发生. 注意1: 非线程安全的问题存在于实例变量中,如果是方法内部的私有变量,则不存在非线程安全问题. 实例变量是对 ...

  5. python 方法无法在线程中使用(附python获取网络流量)

    在python中,定义一个方法,直接调用可以,但是创建一个线程来调用就可能导致失败.这种现象多出现在使用com对象进行系统操作时,而且是以线程的形式调用. 异常提示如下:syntax error.WM ...

  6. Centos 7 防火墙 firewalld 简单使用说明

    1.firewalld简介 firewalld是centos7的一大特性,最大的好处有两个:支持动态更新,不用重启服务:第二个就是加入了防火墙的“zone”概念   2.firewalld命令行界面管 ...

  7. 利用Shell开发MySQL的启动脚本

    MySQL实例部署情况 01:MySQL程序安装目录:/data/apps/mysql 02:MySQL实例3306的配置文件为:/data/mysql/3306/my.cnf 03:MySQL实例3 ...

  8. PhpStorm 配置 PHPUnit

    配置说明 全局安装phpunit代码 composer global require phpunit/phpunit 该代码会自动保存在 /User/你的用户名/.composer/vendor/ph ...

  9. Flink UDF

    本文会主要讲三种udf: ScalarFunction TableFunction AggregateFunction 用户自定义函数是非常重要的一个特征,因为他极大地扩展了查询的表达能力.本文除了介 ...

  10. .NET CORE与Spring Boot编写控制台程序应有的优雅姿势

    本文分别说明.NET CORE与Spring Boot 编写控制台程序应有的“正确”方法,以便.NET程序员.JAVA程序员可以相互学习与加深了解,注意本文只介绍用法,不会刻意强调哪种语言或哪种框架写 ...