java 反射 ioc_利用反射与注解写一个Spring的IOC

beanFactory = new HashMap<>();

public AnnotationConfigApplicationContext() {

this.initBeanByAnnotation();

}

public Object getBean(Class clazz){

return beanFactory.get(clazz);

}

public void initBeanByAnnotation() {

path = AnnotationConfigApplicationContext.class.getClassLoader().getResource("").getFile();

// /E:/IdeaProjecty/SpringIOCTest/out/production/SpringIOCTest/

//System.out.println(path);

Classclazz = Application.class;

ComponentScan componentScan = clazz.getAnnotation(ComponentScan.class);

if(componentScan != null){

String[] value = componentScan.value();

for (String componentPath : value) {

if(componentPath != null && componentPath.length() > 0){

// cn.noteblogs.bean

//System.out.println(componentPath);

loadClassInDefinedDir(componentPath);

}

}

}

}

private void loadClassInDefinedDir(String componentPath) {

//cn.noteblogs.bean 需要将这个路径变为 /E:/IdeaProjecty/SpringIOCTest/out/production/SpringIOCTest/cn/noteblogs/bean

componentPath = componentPath.replaceAll("\\.", "/");

String fileDir = path + componentPath;

///E:/IdeaProjecty/SpringIOCTest/out/production/SpringIOCTest/cn/noteblogs/bean

//System.out.println(classPath);

//加载这个文件夹下的所有class类,并判断是否有@Component

findClassByFile(new File(fileDir));

}

private void findClassByFile(File fileDir) {

if(fileDir.isDirectory()){

File[] files = fileDir.listFiles();

if(files == null || files.length == 0) return;

for (File file : files) {

if(file.isDirectory()){

findClassByFile(file);

}else{

loadClassByAnnotation(file);

}

}

}

}

private void loadClassByAnnotation(File file) {

// /E:/IdeaProjecty/SpringIOCTest/out/production/SpringIOCTest/cn/noteblogs/bean/user.class

// 变为cn.noteblogs.bean.user

String classPath = file.getAbsolutePath().substring(path.length() - 1).replaceAll("\\\\", "\\.");

//cn.noteblogs.bean.user.class

// System.out.println(classPath);

if(classPath != null && classPath.length() > 0 && classPath.contains(".class")){

classPath = classPath.replaceAll("\\.class", "");

//System.out.println(classPath);

Class> clazz = null;

try {

clazz = Class.forName(classPath);

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

Component component = clazz.getAnnotation(Component.class);

if(component != null){

Object instance = null;

try {

instance = clazz.newInstance();

} catch (InstantiationException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

Class>[] interfaces = clazz.getInterfaces();

// Mapif(interfaces != null && interfaces.length > 0){

beanFactory.put(interfaces[0], instance);

}else{

beanFactory.put(clazz, instance);

}

//获取该对象的所有域,然后判断是否有value注解进行赋值

Field[] declaredFields = clazz.getDeclaredFields();

if(declaredFields != null && declaredFields.length > 0){

for (Field f : declaredFields) {

Value annotation = f.getAnnotation(Value.class);

if(annotation != null){

String value = annotation.value();

f.setAccessible(true);

try {

f.set(instance, value);

} catch (IllegalAccessException e) {

e.printStackTrace();

}

}

}

}

}

}

}

}

```

```java

@ComponentScan({"cn.noteblogs.bean","cn.noteblogs.dao"})

public class Application {

public static void main(String[] args) {

AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();

User user = (User)ac.getBean(User.class);

System.out.println(user);

UserDao dao = (UserDao)ac.getBean(UserDao.class);

System.out.println(dao.getClass());

}

}

```

整体项目结构

![image-20210202154913770](http://cdn.noteblogs.cn/image-20210202154913770.png)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值