文章目录
- 1.任务介绍以及类加载器
-
- 2.新建一个模块sun-spring用来写自己的spring容器
- 1.新建模块
- 2.查看是否交给父模块管理
- 3.引入日志
- 3.具体实现
- 1.目录
- 2.Component.java
- 3.ComponentScan.java
- 4.MonsterDao.java
- 5.MonsterService.java
- 6.SunSpringConfig.java
- 7.SunSpringApplicationContext.java
- 8.AppMain.java
- 9.测试
1.任务介绍以及类加载器
1.介绍
2.类加载器
1.介绍
2.自己程序的类路径
2.新建一个模块sun-spring用来写自己的spring容器
1.新建模块
2.查看是否交给父模块管理
3.引入日志
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.4.2</version>
</dependency>
</dependencies>
3.具体实现
1.目录
2.Component.java
package com.sunxiansheng.sunspring.annotation;
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 Component {
String value() default "";
}
3.ComponentScan.java
package com.sunxiansheng.sunspring.annotation;
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 ComponentScan {
String packagePath() default "";
}
4.MonsterDao.java
package com.sunxiansheng.sunspring.compent;
import com.sunxiansheng.sunspring.annotation.Component;
@Component(value = "monsterDao")
public class MonsterDao {
}
5.MonsterService.java
package com.sunxiansheng.sunspring.compent;
import com.sunxiansheng.sunspring.annotation.Component;
@Component(value = "monsterService")
public class MonsterService {
}
6.SunSpringConfig.java
package com.sunxiansheng.sunspring.ioc;
import com.sunxiansheng.sunspring.annotation.ComponentScan;
@ComponentScan(packagePath = "com.sunxiansheng.sunspring.compent")
public class SunSpringConfig {
}
7.SunSpringApplicationContext.java
package com.sunxiansheng.sunspring.ioc;
import com.sunxiansheng.sunspring.annotation.Component;
import com.sunxiansheng.sunspring.annotation.ComponentScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.net.URL;
public class SunSpringApplicationContext {
private static final Logger log = LoggerFactory.getLogger(SunSpringApplicationContext.class);
public SunSpringApplicationContext(Class<?> configClass) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
ComponentScan componentScan = configClass.getDeclaredAnnotation(ComponentScan.class);
String path = componentScan.packagePath();
log.info("要扫描的包路径为={}", path);
ClassLoader classLoader = SunSpringApplicationContext.class.getClassLoader();
path = path.replace(".", "/");
URL resource = classLoader.getResource(path);
File file = new File(resource.getFile());
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
String absolutePath = f.getAbsolutePath();
if (absolutePath.endsWith(".class")) {
String className = extractClassName(absolutePath);
String fullPath = path.replace("/", ".") + "." + className;
Class<?> aClass = classLoader.loadClass(fullPath);
if (aClass.isAnnotationPresent(Component.class)) {
log.info("这是一个Spring bean={}", aClass);
} else {
log.info("这不是一个Spring bean={}", aClass);
}
}
}
}
}
public Object getBean(String name) {
return null;
}
private String extractClassName(String filePath) {
int lastSlashIndex = filePath.lastIndexOf('/');
int lastDotIndex = filePath.lastIndexOf('.');
return filePath.substring(lastSlashIndex + 1, lastDotIndex);
}
}
8.AppMain.java
package com.sunxiansheng.sunspring;
import com.sunxiansheng.sunspring.ioc.SunSpringApplicationContext;
import com.sunxiansheng.sunspring.ioc.SunSpringConfig;
public class AppMain {
public static void main(String[] args) {
try {
SunSpringApplicationContext sunSpringApplicationContext = new SunSpringApplicationContext(SunSpringConfig.class);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
9.测试