自己尝试写了简陋的SpringIoc 真的很简陋

自己定义了两个注解:@Entity  类注解   @aut 属性注解   三个 实体类 School  Student Peolpe 类

package com.gitee.student.demo.iocdemo;

/**
 * Created by ywj on 2018/3/1.
 */
@Entity
public class School {
    private Student sut;

    @aut
    private People people;

    public People getPeople() {
        return people;
    }

    public void setPeople(People people) {
        this.people = people;
    }

    public Student getSut() {
        return sut;
    }

    public void setSut(Student sut) {
        this.sut = sut;
    }
}
package com.gitee.student.demo.iocdemo;

/**
 * Created by ywj on 2018/3/1.
 */
@Entity
public class Student {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

package com.gitee.student.demo.iocdemo;

/**
 * Created by ywj on 2018/3/1.
 */
@Entity
public class People {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

三个实体类如上代码;

就是 扫描指定包下所有的带有 @Entity 注解的类并且 示例话到 Map中 如果 School中的 student 属性中 会有 @aut注解 则会 班Student 示例注入到 这里 

package com.gitee.student.demo.iocdemo;

import java.io.File;
import java.lang.reflect.Field;
import java.util.*;

/**
 * Created by ywj on 2018/3/2.
 */
public class BeanHome {


    private  List<String> classPaths =new ArrayList<String>();
    private  Map<String,Object> classes =new HashMap<String, Object>();
    private  Map<String,List<String>> map= new HashMap<String, List<String>>();

    public void createNew(String basePack) throws Exception {
        searchClass(basePack);
        createClass(map);
    }

    public void searchClass(String basePack) throws Exception {
        //包名
        String classpath = BeanHome.class.getResource("/").getPath();
        basePack = basePack.replace(".", File.separator);
        String searchPath = classpath + basePack;
        doPath(new File(searchPath));
        for (String s : classPaths) {
            s = s.replace(classpath.replace("/","\\").replaceFirst("\\\\",""),"").replace("\\",".").replace(".class","");
            Class cls = Class.forName(s);
            if (cls.isAnnotationPresent(Entity.class)){
                map.put(s,new ArrayList<String>());
                for (Field f:cls.getDeclaredFields()){
                    if (f.isAnnotationPresent(aut.class)){
                        Class<?> type = f.getType();
                        List<String> fields = map.get(s);
                        fields.add(type.toString().replace("class ",""));
                    }
                }
            }
        }
    }

    public void createClass(Map<String,List<String>> map) throws Exception {
        Set<String> pname = map.keySet();
        for (String name:pname){
            if (classes.get(name)==null){
                Class cls = Class.forName(name);
                classes.put(name,cls.newInstance());
                List<String> cname = map.get(name);
                if (cname.size()>0){
                    for (String c:cname){
                        if (classes.get(c)==null) {
                            Class<?> aClass = Class.forName(c);
                            Object o = aClass.newInstance();
                            classes.put(c, o);
                        }
                        for (Field f:cls.getDeclaredFields()){
                            if (f.getType().toString().split(" ")[1].equals(c)){
                                f.setAccessible(true);
                                f.set(classes.get(name),classes.get(c));
                            }
                        }
                    }
                }
            }
        }
    }


    private void doPath(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f1 : files) {
                doPath(f1);
            }
        } else {
            if (file.getName().endsWith(".class")) {
                classPaths.add(file.getPath());
            }
        }
    }

    public List<String> getClassPaths() {
        return classPaths;
    }

    public void setClassPaths(List<String> classPaths) {
        this.classPaths = classPaths;
    }

    public Map<String, Object> getClasses() {
        return classes;
    }

    public void setClasses(Map<String, Object> classes) {
        this.classes = classes;
    }

    public Map<String, List<String>> getMap() {
        return map;
    }

    public void setMap(Map<String, List<String>> map) {
        this.map = map;
    }
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值