zoukankan      html  css  js  c++  java
  • 简单模拟IOC容器:返回对象并能抛出异常

    本次要求:已知com.zzj.vo包下分别有Tiger、lion、Elephant三个Java源文件,请据此实现以下功能:
    ①、自定义一个名为Component的注解,要求该注解只能用于类且代码运行时该注解依然有效;
    ②、为Tiger和Lion类添加component注解
    ③、在Application类中定义静态代码块,该代码块可自动将有Component注解修饰的类创建对象并放在Map集合中;然后定义一个名为getBean的static方法,要求传入Class类实例时返回该Class类对应类的对象,如果没有Class类对应的对象,则引发NoSuchBeanDefinitionException异常并提示“No qualifying bean of type 'com.lq.vo.类名'available

    Component注解

    package com.zzj.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 {
    
    }

    定义三个java类并添加注解

    package com.zzj.vo;
    public class Elephant {
    
    }
    
    package com.zzj.vo;
    import com.zzj.annotation.Component;
    @Component
    public class Lion {
    
    }
    
    package com.zzj.vo;
    import com.zzj.annotation.Component;
    @Component
    public class Tiger {
    
    }

    application类

    package com.zzj;
    
    import java.io.File;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.beans.factory.NoSuchBeanDefinitionException;
    
    import com.zzj.annotation.Component;
    
    public class Application {
    
            //定义map集合存放对应类和对应的class对象
        private static Map<Class,Object> map = new HashMap<Class,Object>();
        
        static{
            
            String packageName = "com.zzj.vo";
            //获取包的绝对路径
            String path = ClassLoader.getSystemResource("").getPath() + packageName.replace(".",File.separator);
            //路径中文转码,由于是静态方法不能使用
            //path = java.net.URLDecoder.decode(path,"utf-8");
            //采用字符替换
            path = path.replace("%e7%a8%8b%e5%ba%8f%e7%8c%bf","程序猿");
            
            //获取包里的所有类
            File[] files = new File(path).listFiles();
            
            //获取所有类的相对路径(包名+类名,无扩展名)
            for (File file : files){
                String fileName = file.getName();
                String className = packageName + "." + fileName.substring(0,fileName.indexOf("."));
                
                //将获取的类放入Map集合,key为类型,value为映射对象(默认为对象地址)
                try {
                    Class clazz = Class.forName(className);
                    if(clazz.getAnnotation(Component.class) != null){
                        map.put(clazz,clazz.newInstance());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
            }
            
        }
        
            //抛出异常
            public static Object getBean(Class clazz){
            Object object =  map.get(clazz);
            if(object == null){
                throw new NoSuchBeanDefinitionException("No qualifying bean of type '"+clazz.getName()+"'available");
            }
            
            return object;
        }
        
    }
        

    异常类

    package com.zzj.exception;
    
    public class NoSuchBeanDefinitionException extends RuntimeException{
    
        private static final long serialVersionUID = 8124869159299733396L;
    
        public NoSuchBeanDefinitionException(String msg) {
            
            super(msg);
            
        }
        
    }

    测试类

    package com.zzj.test;
    
    import com.zzj.vo.Lion;
    
    import com.zzj.Application;
    
    /*
     已知com.zzj.vo包下分别有Tiger、lion、Elephant三个Java源文件,请据此实现以下功能:
    ①、自定义一个名为Component的注解,要求该注解只能用于类且代码运行时该注解依然有效;
    ②、为Tiger和Lion类添加component注解
    ③、在Application类中定义静态代码块,该代码块可自动将有Component注解修饰的类创建对象并放在Map集合中;然后定义一个名为getBean的static方法,
    要求传入Class类实例时返回该Class类对应类的对象,如果没有Class类对应的对象,则引发NoSuchBeanDefinitionException异常并
    提示“No qualifying bean of type 'com.lq.vo.类名'available”;
    */
    public class Test {
        
        public static void main(String[] args){
            
            Object object = Application.getBean(Lion.class);
            System.out.println(object);
            
        }
        
    }

    运行结果

    有对应对象的:输出对象地址

    没有对应对象的:抛出异常

  • 相关阅读:
    LeetCode15 3Sum
    LeetCode10 Regular Expression Matching
    LeetCode20 Valid Parentheses
    LeetCode21 Merge Two Sorted Lists
    LeetCode13 Roman to Integer
    LeetCode12 Integer to Roman
    LeetCode11 Container With Most Water
    LeetCode19 Remove Nth Node From End of List
    LeetCode14 Longest Common Prefix
    LeetCode9 Palindrome Number
  • 原文地址:https://www.cnblogs.com/yimengxianzhi/p/12244143.html
Copyright © 2011-2022 走看看