IoC容器的设计(利用反射、注解和工厂模式实现)

1.实验要求

  1. 利用注解、反射和工厂模式设计一个简单的IoC容器
  2. 该IoC容器包含3个注解和一个IoC容器类(AnnotationConfigApplicationContext),其定义如下:

注解:

注解含义
@Component标注Bean
@Autowired标注需要被注入的对象
@Configuration标注为配置类
@ComponentScan注解扫描器

IoC容器类:

        3.自定义两个业务类Group和User,创建一个测试类Test,对IoC容器进行测试:

2.实验思路

本实验要求我们通过反射和自定义注解的设计,模拟实现spring框架IoC容器类的基础功能,要实现的注解有@Component、@Autowired、@Configuration和@ComponentScan

首先我们需要明确这些注解在spring框架中本身的功能是什么:

  1. @Component:将实体类实例化到容器中
  2. @ComponentScan:配置包路径,到该路径下去寻找bean
  3. @Autowired:自动装配(定义一个变量接收注入的类)
  4. @Configuration:标识为配置类

所以在该实验中,我们需要将自定义四个注解,然后将Group和User类使用@Component注解,在User类中创建Group类的实例化对象并设置为自动装配,这样就能在User类中调用Group类的方法;

然后我们需要自己实现一个IoC容器类,处理自定义的注解的基本逻辑;

接下来在test类中实例化IoC容器,并从中取得bean,调用其方法

3.实验代码

pom文件依赖配置:

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.10.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.14.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
  </dependency>

jdk版本:1.8

开发工具:IDEA

3.1自定义注解

@Autowired:

//自定义注解:标注需要被注入的对象
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Autowired {
}

@Component:

//自定义注解:标注Bean
@Target({ElementType.TYPE,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Component {
    String value() default "";
}

@ComponentScan:

//自定义注解 注解扫描器
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ComponentScan {
    String value() default "";
}

@Configuration:

//自定义注解 标注为配置类
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Configuration {
}

3.2 定义实体类

Group:

@Component
public class Group {
    public String getBeanName()
    {
        return this.getClass().getSimpleName();
    }
}

User:

@Component
public class User {
    @Autowired
    private Group group;

    public void print()
    {
        System.out.println("group name as follow:");
        System.out.println(group.getBeanName());
    }

}

3.3 定义配置类

MyConfig:

@Configuration
@ComponentScan("org.example")
public class MyConfig {
}

3.4 定义IoC容器类

接口:ApplicationContext

public interface ApplicationContext {
    Object getBean(Class clazz);
}

实现类:AnnotationApplicationContext

public class AnnotationApplicationContext implements ApplicationContext {

    //定义Map,用于存放bean对象
    private Map<String, Object> beanFactory = new HashMap<>();

    //创建IOC容器类时执行的方法
    public AnnotationApplicationContext(Class configClass) throws InstantiationException, IllegalAccessException {
        //处理@Configuration的逻辑(没有实质性的内容,只是要求必须有配置类)
        try {
            //获取@Configuration类型的对象
            Configuration configuration = (Configuration) configClass.getAnnotation(Configuration.class);
            //如果对象为空,则引发异常
            if (configuration.annotationType().toGenericString().equals("")) {
                //没有具体逻辑
            }
        } catch (Exception e) {
            System.out.println("找不到配置类");
            System.exit(-1);
        }

        //处理@ComponentScan的逻辑
        /*
        总的来说,就是扫描某一路径下的所有类,如果有的类被Component注解,则创建其实例(一个bean),加入beanFactory中
        如果有的类中的对象(一个Field类型的数据)被@Autowired注解标注,则用beanFactory中同类型的bean替代该对象
        */
        try {
            ComponentScan componentScan = (ComponentScan) configClass.getAnnotation(ComponentScan.class);

            //根据componentScanValue的值来获取要扫描的路径(默认值处理的不是很好)
            String componentScanValue = componentScan.value().toString();
            String path = "";
            if (!componentScanValue.equals(""))
            {
                String[] splitValue = componentScanValue.split("\\.");
                for (int i = 0; i < splitValue.length; i++) {
                    path += splitValue[i] + "/";
                }
                path = "classpath*:" + path + "**/*.class";
            }
            else {
                path = "classpath*:org/example/**/*.class";
            }
            //扫描路径,获取所有的class
            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource[] resources = resolver.getResources(path);
            for (Resource res : resources) {
                //获取类名
                String clsName = new SimpleMetadataReaderFactory().getMetadataReader(res).getClassMetadata().getClassName();
                //获取类对象
                Class thisClass = Class.forName(clsName);

                //判断当前类是否被Component注解标注
                Component component = (Component) thisClass.getAnnotation(Component.class);
                if (component != null) //说明该类被Component注解标注
                {
                    Constructor c = thisClass.getConstructor();
                    Object o = c.newInstance(); //创建该类的实例
                    beanFactory.put(o.getClass().getSimpleName(), o);//加入beanFactory中
                }

                //处理@Autowired注解
                for (Object bean : beanFactory.values()) { //查看beanFactory中所有的bean
                    Class beanClass = bean.getClass();
                    Field[] beanFields = beanClass.getDeclaredFields(); //获取bean的fields
                    for (Field field : beanFields) {
                        if (field.isAnnotationPresent(Autowired.class)) { //如果被Autowired注解标注
                            Object beanD = beanFactory.get(field.getType().getSimpleName()); //获取beanFactory中的bean
                            field.setAccessible(true); //关闭安全检查
                            field.set(bean, beanD); //用beanFactory中的bean来代替当前bean
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("没有ComponentScan注解");
            System.exit(-1);
        }
    }

    //getBean方法,获取某个bean(通过class)
    @Override
    public Object getBean(Class beanClass) {
        return beanFactory.get(beanClass.getSimpleName());
    }

}

3.5 测试类

public class test {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        //创建IOC容器,传入的参数是当前的配置类
        //因此可以根据@Configuration和@ComponentScan("org.example")来走接下来的逻辑
        AnnotationApplicationContext context = new AnnotationApplicationContext(MyConfig.class);
        //从容器中获得user对象
        User user = (User) context.getBean(User.class);
        //执行对象方法
        //可以看到通过user对象执行了group类的方法getBeanName()
        //但user类中,我们并没有实例化group对象,可以看到是通过注解来实现的
        user.print();
    }
}

4.实验结果

成功在user类中调用了group类的方法;

5.源码

上面的代码是完整的,下附工程文件:

https://download.csdn.net/download/qq_51235856/87944324

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我来设计一个简单的IoC容器。 首先,我们需要定义注解: ```java @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Component { String value() default ""; } @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface Autowired { String value() default ""; } @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Configuration { } @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface ComponentScan { String[] value() default {}; } ``` 这里定义了四个注解,分别是@Component、@Autowired、@Configuration和@ComponentScan。 @Component表示该类是一个组件,需要被IoC容器管理。 @Autowired表示该属性需要被自动注入。 @Configuration表示该类是一个配置类,用于配置IoC容器。 @ComponentScan表示需要扫描的包路径。 接下来,我们定义一个IoC容器类: ```java public class AnnotationConfigApplicationContext { private Map<String, Object> beans = new HashMap<>(); // 存储所有的bean private Map<Class<?>, Object> configurationBeans = new HashMap<>(); // 存储所有的@Configuration类 private Set<Class<?>> componentClasses = new HashSet<>(); // 存储所有的@Component类 public AnnotationConfigApplicationContext(Class<?> configurationClass) { scan(configurationClass); registerBeans(); autowireBeans(); } private void scan(Class<?> configurationClass) { ComponentScan componentScan = configurationClass.getAnnotation(ComponentScan.class); if (componentScan != null) { String[] basePackages = componentScan.value(); for (String basePackage : basePackages) { Set<Class<?>> classes = ClassScanner.getClasses(basePackage); for (Class<?> clazz : classes) { if (clazz.isAnnotationPresent(Component.class)) { componentClasses.add(clazz); } } } } } private void registerBeans() { for (Class<?> clazz : componentClasses) { Object bean = createBean(clazz); beans.put(clazz.getName(), bean); } for (Class<?> clazz : configurationBeans.keySet()) { Object bean = configurationBeans.get(clazz); beans.put(clazz.getName(), bean); } } private Object createBean(Class<?> clazz) { try { Object instance = clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(Autowired.class)) { String beanName = field.getType().getName(); Object bean = beans.get(beanName); if (bean == null) { throw new RuntimeException("Can not find bean: " + beanName); } field.setAccessible(true); field.set(instance, bean); } } return instance; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Create bean failed: " + clazz.getName()); } } private void autowireBeans() { for (Object bean : beans.values()) { Field[] fields = bean.getClass().getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(Autowired.class)) { String beanName = field.getType().getName(); Object autowiredBean = beans.get(beanName); if (autowiredBean == null) { throw new RuntimeException("Can not find bean: " + beanName); } field.setAccessible(true); try { field.set(bean, autowiredBean); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } } public <T> T getBean(Class<T> clazz) { Object bean = beans.get(clazz.getName()); if (bean == null) { throw new RuntimeException("Can not find bean: " + clazz.getName()); } return (T) bean; } public <T> T getBean(String beanName) { Object bean = beans.get(beanName); if (bean == null) { throw new RuntimeException("Can not find bean: " + beanName); } return (T) bean; } } ``` 这个IoC容器类包含了三个方法:scan()、registerBeans()和autowireBeans()。 scan()方法用于扫描所有的@Component类,并将它们保存到componentClasses中。 registerBeans()方法用于创建所有的bean,并将它们保存到beans中。 autowireBeans()方法用于自动注入所有的bean。 最后,我们定义一个测试类: ```java @Configuration @ComponentScan({"com.example.demo"}) public class AppConfig { } @Component public class UserDao { public void save() { System.out.println("Save user"); } } @Component public class UserService { @Autowired private UserDao userDao; public void save() { userDao.save(); } } public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); UserService userService = context.getBean(UserService.class); userService.save(); } } ``` 这个测试类中,我们定义了一个@Configuration类AppConfig,用于配置IoC容器,并指定需要扫描的包路径。 另外,我们定义了一个UserDao类和一个UserService类,它们都被标注为@Component,表示需要被IoC容器管理。 在UserService类中,我们使用@Autowired注解自动注入UserDao对象。 最后,在main()方法中,我们创建了一个AnnotationConfigApplicationContext对象,并传入AppConfig.class作为构造函数参数。然后,我们从容器中获取UserService对象,并调用它的save()方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

THE WHY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值