springboot()

自动配置

Configuration 注解 + Bean 注解

// 获取容器中所有的组件
        String[] allBean = run.getBeanDefinitionNames();
        for (String name : allBean) {
            System.out.println(name);
        }

        // 第一个参数是组件的名字,等二个参数是组件的类型
        Pet tom01 = run.getBean("pet", Pet.class);
        Pet tom02 = run.getBean("pet", Pet.class);
        System.out.println("组件:" + (tom01==tom02));

        User userTest = run.getBean("userTest", User.class);
        // 为 true,说明默认情况下,springboot会检查是否存在这个组件,如果存在,要保证这个bean被调用多少次都是单实例的
        // @Configuration(proxyBeanMethods = false),springboot不会检查是否存在这个组件,每个@Bean方法被调用多少次返回的组件都是新创建的
        // 当 proxyBeanMethods 设置为 false 时,下面的语句为 false
        // 当我们只是往容器中注册组件时,别人不需要依赖我们的组件时,可以将 proxyBeanMethods 设置为 false,springboot 启动的速度会更快
        System.out.println("组件的依赖:" + (userTest.getPet() == tom01));
————————

自动配置

Configuration 注解 + Bean 注解

// 获取容器中所有的组件
        String[] allBean = run.getBeanDefinitionNames();
        for (String name : allBean) {
            System.out.println(name);
        }

        // 第一个参数是组件的名字,等二个参数是组件的类型
        Pet tom01 = run.getBean("pet", Pet.class);
        Pet tom02 = run.getBean("pet", Pet.class);
        System.out.println("组件:" + (tom01==tom02));

        User userTest = run.getBean("userTest", User.class);
        // 为 true,说明默认情况下,springboot会检查是否存在这个组件,如果存在,要保证这个bean被调用多少次都是单实例的
        // @Configuration(proxyBeanMethods = false),springboot不会检查是否存在这个组件,每个@Bean方法被调用多少次返回的组件都是新创建的
        // 当 proxyBeanMethods 设置为 false 时,下面的语句为 false
        // 当我们只是往容器中注册组件时,别人不需要依赖我们的组件时,可以将 proxyBeanMethods 设置为 false,springboot 启动的速度会更快
        System.out.println("组件的依赖:" + (userTest.getPet() == tom01));