1、BeanDefinition
package com.csdn.myspring; import lombok.AllArgsConstructor; import lombok.Data; @Data @AllArgsConstructor public class BeanDefinition {private String beanName;private Class beanClass; }
2、扫描包的工具类MyTools
package com.csdn.myspring; import java.io.File; import java.io.IOException; import java.net.JarURLConnection; import java.net.URL; import java.net.URLDecoder; import java.util.Enumeration; import java.util.LinkedHashSet; import java.util.Set; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class MyTools {public static Set<Class<?>> getClasses(String pack) {// 第一个class类的集合Set<Class<?>> classes = new LinkedHashSet<Class<?>>();// 是否循环迭代boolean recursive = true;// 获取包的名字 并进行替换String packageName = pack;String packageDirName = packageName.replace('.', '/');// 定义一个枚举的集合 并进行循环来处理这个目录下的thingsEnumeration<URL> dirs;try {dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);// 循环迭代下去while (dirs.hasMoreElements()) {// 获取下一个元素URL url = dirs.nextElement();// 得到协议的名称String protocol = url.getProtocol();// 如果是以文件的形式保存在服务器上if ("file".equals(protocol)) {// 获取包的物理路径String filePath = URLDecoder.decode(url.getFile(), "UTF-8");// 以文件的方式扫描整个包下的文件 并添加到集合中findClassesInPackageByFile(packageName, filePath, recursive, classes);} else if ("jar".equals(protocol)) {// 如果是jar包文件// 定义一个JarFileSystem.out.println("jar类型的扫描");JarFile jar;try {// 获取jarjar = ((JarURLConnection) url.openConnection()).getJarFile();// 从此jar包 得到一个枚举类Enumeration<JarEntry> entries = jar.entries();findClassesInPackageByJar(packageName, entries, packageDirName, recursive, classes);} catch (IOException e) {// log.error("在扫描用户定义视图时从jar包获取文件出错");e.printStackTrace();}}}} catch (IOException e) {e.printStackTrace();}return classes;}private static void findClassesInPackageByJar(String packageName, Enumeration<JarEntry> entries, String packageDirName, final boolean recursive, Set<Class<?>> classes) {// 同样的进行循环迭代while (entries.hasMoreElements()) {// 获取jar里的一个实体 可以是目录 和一些jar包里的其他文件 如META-INF等文件JarEntry entry = entries.nextElement();String name = entry.getName();// 如果是以/开头的if (name.charAt(0) == '/') {// 获取后面的字符串name = name.substring(1);}// 如果前半部分和定义的包名相同if (name.startsWith(packageDirName)) {int idx = name.lastIndexOf('/');// 如果以"/"结尾 是一个包if (idx != -1) {// 获取包名 把"/"替换成"."packageName = name.substring(0, idx).replace('/', '.');}// 如果可以迭代下去 并且是一个包if ((idx != -1) || recursive) {// 如果是一个.class文件 而且不是目录if (name.endsWith(".class") && !entry.isDirectory()) {// 去掉后面的".class" 获取真正的类名String className = name.substring(packageName.length() + 1, name.length() - 6);try {// 添加到classesclasses.add(Class.forName(packageName + '.' + className));} catch (ClassNotFoundException e) {// .error("添加用户自定义视图类错误 找不到此类的.class文件");e.printStackTrace();}}}}}}private static void findClassesInPackageByFile(String packageName, String packagePath, final boolean recursive, Set<Class<?>> classes) {// 获取此包的目录 建立一个FileFile dir = new File(packagePath);// 如果不存在或者 也不是目录就直接返回if (!dir.exists() || !dir.isDirectory()) {// log.warn("用户定义包名 " + packageName + " 下没有任何文件");return;}// 如果存在 就获取包下的所有文件 包括目录File[] dirfiles = dir.listFiles(// 自定义过滤规则 如果可以循环(包含子目录) 或则是以.class结尾的文件(编译好的java类文件)(file)-> {return (recursive && file.isDirectory()) || (file.getName().endsWith(".class"));});// 循环所有文件for (File file : dirfiles) {// 如果是目录 则继续扫描if (file.isDirectory()) {findClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), recursive, classes);} else {// 如果是java类文件 去掉后面的.class 只留下类名String className = file.getName().substring(0, file.getName().length() - 6);try {// 添加到集合中去// classes.add(Class.forName(packageName + '.' +// className));// 经过回复同学的提醒,这里用forName有一些不好,会触发static方法,没有使用classLoader的load干净classes.add(Thread.currentThread().getContextClassLoader().loadClass(packageName + '.' + className));} catch (ClassNotFoundException e) {// log.error("添加用户自定义视图类错误 找不到此类的.class文件");e.printStackTrace();}}}} }
3、MyAnnotationConfigApplicationContext
package com.csdn.myspring; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.*; public class MyAnnotationConfigApplicationContext {private Map<String, Object> ioc = new HashMap<>();public MyAnnotationConfigApplicationContext() {}public MyAnnotationConfigApplicationContext(String pack) {//遍历包,找到目标类(原材料)Set<BeanDefinition> beanDefinitions = findBeanDefinitions(pack);//根据原材料获取beancreateObject(beanDefinitions);//自动装配autowireObject(beanDefinitions);}public void autowireObject(Set<BeanDefinition> beanDefinitions) {Iterator<BeanDefinition> iterator = beanDefinitions.iterator();while (iterator.hasNext()) {BeanDefinition beanDefinition = iterator.next();Class clazz = beanDefinition.getBeanClass();Field[] declaredFields = clazz.getDeclaredFields();for (Field declaredField : declaredFields) {Autowired annotation = declaredField.getAnnotation(Autowired.class);if (annotation!=null) {Qualifier qualifier = declaredField.getAnnotation(Qualifier.class);if (qualifier!=null) {try {String beanName = qualifier.value();Object bean = getBean(beanName);String fieldName = declaredField.getName();String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);Method method = clazz.getMethod(methodName, declaredField.getType());Object object = getBean(beanDefinition.getBeanName());method.invoke(object, bean);} catch (NoSuchMethodException e) {throw new RuntimeException(e);} catch (InvocationTargetException e) {throw new RuntimeException(e);} catch (IllegalAccessException e) {throw new RuntimeException(e);}Object object = getBean(beanDefinition.getBeanName());}}}}}public Set<BeanDefinition> findBeanDefinitions(String pack) {//获取包下的所有类Set<Class<?>> classes = MyTools.getClasses(pack);Iterator<Class<?>> iterator = classes.iterator();Set<BeanDefinition> beanDefinitions = new HashSet<>();while (iterator.hasNext()) {//2、遍历这些类,找到添加了注解的类Class<?> clazz = iterator.next();Component componentAnnotation = clazz.getAnnotation(Component.class);if (componentAnnotation != null) {//获取Component注解的值String beanName = componentAnnotation.value();if ("".equals(beanName)) {//获取类名首字母小写String className = clazz.getName().replaceAll(clazz.getPackage().getName() + ".", "");beanName = className.substring(0, 1).toLowerCase() + className.substring(1);}//3、将这些类封装成BeanDefinition,装载到集合中beanDefinitions.add(new BeanDefinition(beanName, clazz));}}return beanDefinitions;}public void createObject(Set<BeanDefinition> beanDefinitions) {Iterator<BeanDefinition> iterator = beanDefinitions.iterator();while (iterator.hasNext()) {BeanDefinition beanDefinition = iterator.next();Class clazz = beanDefinition.getBeanClass();String beanName = beanDefinition.getBeanName();try {//创建对象Object object = clazz.getConstructor().newInstance();//完成属性的赋值Field[] declaredFields = clazz.getDeclaredFields();for (Field declaredField : declaredFields) {Value valueAnnotation = declaredField.getAnnotation(Value.class);if (valueAnnotation!=null) {String value = valueAnnotation.value();String fieldName = declaredField.getName();String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);Method method = clazz.getMethod(methodName, declaredField.getType());//完成数据类型转换Object val = null;switch (declaredField.getType().getName()) {case "java.lang.Integer" -> val=Integer.parseInt(value);case "java.lang.String"-> val = value;case "java.lang.Float"-> Float.parseFloat(value);}method.invoke(object, val);}}//存入缓存ioc.put(beanName, object);} catch (InstantiationException e) {throw new RuntimeException(e);} catch (IllegalAccessException e) {throw new RuntimeException(e);} catch (InvocationTargetException e) {throw new RuntimeException(e);} catch (NoSuchMethodException e) {throw new RuntimeException(e);}}}public Object getBean(String beanName) {return ioc.get(beanName);}public static void main(String[] args) {MyAnnotationConfigApplicationContext myAnnotationConfigApplicationContext = new MyAnnotationConfigApplicationContext("com.csdn.myspring");Person bean = (Person) myAnnotationConfigApplicationContext.getBean("b");System.out.println(bean.getName());} }
4、@Component
package com.csdn.myspring; 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 ""; }
5、@Value
package com.csdn.myspring; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Value {String value(); }
6、@Autowired
package com.csdn.myspring; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Autowired { }
7、@Qualifier
package com.csdn.myspring; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Qualifier {String value(); }
@Qualifier是一个Spring框架的注解,用于标识一个Bean的特定实例。当有多个Bean实现了同一接口或类时,@Qualifier可以指定要使用的实例。
通常情况下,Spring框架根据类型来自动装配依赖,但如果有多个 Bean 与依赖的类型匹配,则会产生歧义。这时就需要使用 @Qualifier 来指定具体匹配的 Bean。
例如:
public interface Animal {// ... }@Component @Qualifier("cat") public class Cat implements Animal {// ... }@Component @Qualifier("dog") public class Dog implements Animal {// ... }@Service public class AnimalService {@Autowired@Qualifier("cat")private Animal animal;// ... }
在这个例子中,AnimalService 类需要注入一个 Animal 接口的实例,但有两个实现类 Cat 和 Dog。使用 @Qualifier 标记 Cat 和 Dog 实例,然后在 AnimalService 中使用 @Autowired 和 @Qualifier("cat") 标记,就可以明确指定注入 Cat 实例了。