文章目录
- 1.目录
- 2.MyScope.java 自定义的Scope注解枚举
- 3.Scope.java 自定义Scope注解
- 4.MonsterService.java 指定bean的作用域
- 5.BeanDefintion.java bean的定义信息
- 6.SunSpringApplicationContext.java Spring容器
- 7.测试
1.目录
java_Scope_6">2.MyScope.java 自定义的Scope注解枚举
java">package com.sunxiansheng.sunspring.annotation.myenum;
public enum MyScope {SINGLETON,PROTOTYPE;
}
java_Scope_26">3.Scope.java 自定义Scope注解
java">package com.sunxiansheng.sunspring.annotation;import com.sunxiansheng.sunspring.annotation.myenum.MyScope;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 Scope {MyScope value() default MyScope.SINGLETON;}
java_bean_54">4.MonsterService.java 指定bean的作用域
java">package com.sunxiansheng.sunspring.compent;import com.sunxiansheng.sunspring.annotation.Component;
import com.sunxiansheng.sunspring.annotation.Scope;
import com.sunxiansheng.sunspring.annotation.myenum.MyScope;
@Component
@Scope(value = MyScope.PROTOTYPE)
public class MonsterService {
}
java_bean_77">5.BeanDefintion.java bean的定义信息
java">package com.sunxiansheng.sunspring.ioc;import com.sunxiansheng.sunspring.annotation.myenum.MyScope;
public class BeanDefintion {private MyScope scope;private Class<?> clazz;public MyScope getScope() {return scope;}public void setScope(MyScope scope) {this.scope = scope;}public Class<?> getClazz() {return clazz;}public void setClazz(Class<?> clazz) {this.clazz = clazz;}@Overridepublic String toString() {return "BeanDefintion{" +"scope='" + scope + '\'' +", clazz=" + clazz +'}';}}
java_Spring_125">6.SunSpringApplicationContext.java Spring容器
java">package com.sunxiansheng.sunspring.ioc;import com.sunxiansheng.sunspring.annotation.Component;
import com.sunxiansheng.sunspring.annotation.ComponentScan;
import com.sunxiansheng.sunspring.annotation.Scope;
import com.sunxiansheng.sunspring.annotation.myenum.MyScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.File;
import java.net.URL;
import java.util.concurrent.ConcurrentHashMap;
public class SunSpringApplicationContext {private static final Logger log = LoggerFactory.getLogger(SunSpringApplicationContext.class);private ConcurrentHashMap<String, BeanDefintion> beanDefintionMap = new ConcurrentHashMap<>();private ConcurrentHashMap<String, Object> singletonObjects = new ConcurrentHashMap<>();public SunSpringApplicationContext(Class<?> configClass) throws ClassNotFoundException, InstantiationException, IllegalAccessException {beanDefinitionScan(configClass);}private void beanDefinitionScan(Class<?> configClass) throws ClassNotFoundException {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);BeanDefintion beanDefintion = new BeanDefintion();if (aClass.isAnnotationPresent(Scope.class)) {Scope scope = aClass.getDeclaredAnnotation(Scope.class);MyScope value = scope.value();beanDefintion.setScope(value);} else {beanDefintion.setScope(MyScope.SINGLETON);}beanDefintion.setClazz(aClass);Component component = aClass.getDeclaredAnnotation(Component.class);String beanName = component.value();if ("".equals(beanName)) {beanName = className.substring(0, 1).toLowerCase() + className.substring(1);}beanDefintionMap.put(beanName, beanDefintion);log.info("beanName={}, beanDefintion={}", beanName, beanDefintion);} 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);}}
7.测试