自己定义了两个注解:@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;
}
}