ioc
创建注解
@ComponentScan
package org.example.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ComponentScan {
String value();
}
@Component
package org.example.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Component {
String value() ;
}
@Scope
package org.example.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Scope {
String value() default "";
}
扫包类
package org.example;
import org.example.annotation.ComponentScan;
@ComponentScan("org.example")
public class MyApplication {
}
需要交给spring容器管理类
package org.example.service;
import org.example.annotation.Component;
import org.example.annotation.Scope;
@Component("userService")
@Scope("prototype")
public class UserService {
public void test(){
System.out.println("true = " );
}
}
ApplicationContext
package org.example;
import org.example.annotation.Component;
import org.example.annotation.ComponentScan;
import org.example.annotation.Scope;
import org.example.common.BeanDefinition;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.IntFunction;
public class ApplicationContext {
private ConcurrentHashMap<String,Object> singleMaps=new ConcurrentHashMap<String,Object>();
private ConcurrentHashMap<String, BeanDefinition> beanDefinitionMap=new ConcurrentHashMap<String, BeanDefinition>();
private Class<?> clazz;
public ApplicationContext(Class<?> clazz) {
this.clazz = clazz;
}
public void scan(){
if (clazz.isAnnotationPresent(ComponentScan.class)){
ComponentScan annotation = (ComponentScan) clazz.getDeclaredAnnotation(ComponentScan.class);
String value = annotation.value();
ClassLoader classLoader = clazz.getClassLoader();
String s = value.replaceAll("\\.", "/");
URL url = classLoader.getResource(s);
File file = new File(url.getFile());
if (file.isDirectory()){
ArrayList<File> list = new ArrayList<>();
getAllFile(file,list);
File[] files = list.stream().toArray(new IntFunction<File[]>() {
@Override
public File[] apply(int value) {
return new File[value];
}
});
assert files != null;
for (File file1 : files) {
if (file1.isFile()){
String path = file1.getPath();
try {
Class<?> aClass = classLoader.loadClass(path.substring(path.indexOf("org"), path.indexOf(".")).replace("\\","."));
if (aClass.isAnnotationPresent(Component.class)){
Component component = aClass.getAnnotation(Component.class);
String beanName = component.value();
BeanDefinition beanDefinition = new BeanDefinition();
if (aClass.isAnnotationPresent(Scope.class)){
Scope scope = aClass.getAnnotation(Scope.class);
beanDefinition.setScope(scope.value());
}else{
beanDefinition.setScope("singleton");
singleMaps.put(beanName,aClass.getDeclaredConstructor().newInstance());
}
beanDefinition.setClazz(aClass);
beanDefinitionMap.put(beanName,beanDefinition);
}
} catch (ClassNotFoundException | NoSuchMethodException e) {
e.printStackTrace();
throw new RuntimeException(path+"类加载失败");
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}else{
throw new RuntimeException("配置项非文件");
}
}
}
private void getAllFile(File file, List<File> files){
if (file.isDirectory()){
File[] files1 = file.listFiles();
for (File file1 : files1) {
getAllFile(file1,files);
}
}else if (file.isFile()){
files.add(file);
}
}
private Object createBean(Class<?> clazz){
try {
return clazz.getDeclaredConstructor().newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}
public Object getBean(String beanName){
BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
if (beanDefinition!=null){
if ("singleton".equals(beanDefinition.getScope())){
return singleMaps.get(beanName);
}else if ("prototype".equals(beanDefinition.getScope())){
return createBean(beanDefinition.getClazz());
}
}
return null;
}
}
测试
public static void main(String[] args) {
ApplicationContext applicationContext = new ApplicationContext(MyApplication.class);
applicationContext.scan();
System.out.println("applicationContext.getBean(\"userService\") = " + applicationContext.getBean("userService"));
System.out.println("applicationContext.getBean(\"userService\") = " + applicationContext.getBean("userService"));
System.out.println("applicationContext.getBean(\"userService\") = " + applicationContext.getBean("userService"));
System.out.println("applicationContext.getBean(\"userService\") = " + applicationContext.getBean("userService"));
}