IOC/DI源码解读(部分)

自定义

1.自定义内容

  • @Configuration
  • @Bean
  • @Resource
  • ApplicationContext
  • AnnotationConfigApplicationContext

2.结构

在这里插入图片描述

3.自定义

  • @MyConfiguration
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.TYPE})
public @interface MyConfiguration {
}
  • @MyBean
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD})
public @interface MyBean {
}
  • @MyResource
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD, ElementType.FIELD})
public @interface MyResource {
    //安全机制  必须要注入
    String name();//必须要设置此属性的值(无默认值)
}
  • MyApplicationContext
public interface MyApplicationContext {
    //获取bean的方法
    public Object getBean(String id);
}
  • MyAnnotationConfigApplicationContext
public class MyAnnotationConfigApplicationContext implements MyApplicationContext {
    //bean容器
    private Map<String, Object> beans = new HashMap<>();

    //AppConfig.class
    public MyAnnotationConfigApplicationContext(Class clz) {
        //1.传入的clz先判断是否有@MyConfiguration注解,有则创建这个bean对象,存到beans中
        //2.取出appConfig中所有的方法,判断这些方法上是否有@Bean注解
        //3.如果有则激活该方法,获取返回值,以改方法名作为键,值做值,存到beans托管
        //4.DI循环所有的beans,查看每个beans中的方法是否有@MyResource,有则激活
        try {
            //1.传入的clz先判断是否有@MyConfiguration注解,有则创建这个bean对象,存到beans中

            //看传入的clz类是不是配置类@MyConfiguration
            if (!clz.isAnnotationPresent(MyConfiguration.class)) {
                throw new RuntimeException("当前" + clz.getName() + "不是配置类,容器初始化失败");
            }
            //激活配置类,作为值注入
            Object obj = clz.newInstance();
            //默认键值为首字母小写的类名,否则为方法名
            String beanName = getBeanName(clz);
            beans.put(beanName, obj);
            //-----------------------------------此处将配置类本身激活注入


            //2.取出appConfig中所有的方法,判断这些方法上是否有@Bean注解

            //取出所有的方法
            Method[] ms = clz.getMethods();
            //遍历方法
            for (Method m : ms) {
                //取出方法的所有注解
                Annotation[] anos = m.getAnnotations();
                for (Annotation ano : anos) {
                    if (ano instanceof MyBean) {


                        //3.如果有则激活该方法,获取返回值,以改方法名作为键,值做值,存到beans托管
                        //激活方法,返回值就是需要注入的类
                        Object o = m.invoke(obj, null);
                        //方法名作为键值
                        String id = m.getName();
                        //注入
                        beans.put(id, o);
                    }
                }
            }


            //4.DI循环所有的beans,查看每个beans中的方法是否有@MyResource,有则激活
            for (Method m : ms) {//也可以循环容器中已经托管的类
                Annotation[] anos = m.getAnnotations();
                for (Annotation ano : anos) {
                    if (ano instanceof MyBean) {
                        //取出方法名,作为bean中的键
                        String id = m.getName();
                        //根据键取出对应的类
                        Object o = beans.get(id);
                        //取出对应类中所有的方法
                        Method[] mms = o.getClass().getMethods();

                        for (Method method : mms) {
                            Annotation[] aa = method.getAnnotations();
                            for (Annotation ann : aa) {
                                //是set方法且有注释
                                if (ann instanceof MyResource && method.getName().startsWith("set")) {
                                    //取出对应的name
                                    String beanid = ((MyResource) ann).name();
                                    Object beanObject = beans.get(beanid);
                                    //激活当前方法,将对象注入
                                    method.invoke(o, beanObject);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //创建bean名
    private String getBeanName(Class clz) {
        //取出类名
        String className = clz.getName();
        //将类名转数组
        char[] chs = className.toCharArray();
        //首字母小写
        chs[0] = Character.toLowerCase(chs[0]);
        return new String(chs);
    }


    @Override
    public Object getBean(String id) {
        return beans.get(id);
    }
}

4.测试

  • AppConfig
@MyConfiguration
public class AppConfig {
    @MyBean//注入Person  id=p  value=Person
    public Person p() {
        return new Person();
    }

    @MyBean
    public PersonDao personDaoHibernateImpl() {
        return new PersonDaoHibernateImpl();
    }

    @MyBean
    public PersonDao personDaoMybatisImpl() {
        return new PersonDaoMybatisImpl();
    }

    @MyBean//@MyResource    自动注入PersonDao的实现类name
    public PersonBiz personBizImpl() {
        return new PersonBizImpl();
    }
}
  • Person
public class Person {  }
  • PersonBiz
public interface PersonBiz {
    public void add();
}
  • PersonBizImpl
public class PersonBizImpl implements PersonBiz {
    private PersonDao personDao;

    @MyResource(name = "personDaoMybatisImpl")
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

    @Override
    public void add() {
        personDao.add();
    }
}
  • PersonDao
public interface PersonDao {
    public void add();
}
  • PersonDaoHibernateImpl
public class PersonDaoHibernateImpl implements PersonDao {
    @Override
    public void add() {
        System.out.println("HibernateImpl的dao()");
    }
}
  • PersonDaoMybatisImpl
public class PersonDaoMybatisImpl implements PersonDao {
    @Override
    public void add() {
        System.out.println("Myhibernate的dao()");
    }
}

AnnotationConfigApplicationContext为入口解读@ComponentScan中includeFilters与excludeFilters属性

请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值