自学内容网 自学内容网

1.Spring-容器-注册

一、@Bean和获取Bean

(1)创建IoC容器:

SpringApplication.run(类名.class, args);
ConfigurableApplicationContext ioc = SpringApplication.run(Spring01IocApplication.class, args);

 

(2)将对象注册到IoC容器中:

@Bean

(3)获取到IoC容器中的组件对象

1.按照名字获取:需要强转

Person zhangsan = (Person) ioc.getBean("zhangsan");

2.按照类型获取 :同一个类型只有唯一一个

Person bean = ioc.getBean(Person.class);

3.按照类型获取 :同一个类型有多个

Map<String, Person> type = ioc.getBeansOfType(Person.class);

4.按照类型和名字获取 

Person bean = ioc.getBean("zhangsan", Person.class);

 

@SpringBootApplication
public class Spring01IocApplication {
    /**
     * 组件的创建时机:容器启动过程中就会创建组件对象(构造方法在容器创建完成前)
     * 单实例特性:所有组件默认是单例的,每次获取直接从容器中拿。容器提前会创建组件
     */
    public static void main(String[] args) {
        // 一.启动Spring应用上下文:ApplicationContext  IOC容器
        ConfigurableApplicationContext ioc = SpringApplication.run(Spring01IocApplication.class, args);
        System.out.println("-------------------------------------------------------------------------IOC容器创建完成:----------------------------------------------------------------");
        // 二.获取到容器中的组件:
        Dog bean = ioc.getBean(Dog.class);
        System.out.println("Dog=:" + bean);
        Dog bean1 = ioc.getBean(Dog.class);
        System.out.println("Dog=:" + bean1);
        Dog bean2 = ioc.getBean(Dog.class);
        System.out.println("Dog=:" + bean2);
    }

    public static void test01BeanAnnotation(String[] args) {
        // 一.启动Spring应用上下文:ApplicationContext  IOC容器
        ConfigurableApplicationContext ioc = SpringApplication.run(Spring01IocApplication.class, args);
        System.out.println("IOC容器对象:" + ioc);

        // 二.获取到容器中所有组件的名字:容器中装了哪些组件:
//        String[] beanDefinitionNames = ioc.getBeanDefinitionNames();
//        for (String beanDefinitionName : beanDefinitionNames) {
//            System.out.println(beanDefinitionName);
//        }

        // 四.获取容器中的组件对象: → 从容器中“彻底”(而非只获取组件名)获取Bean→ 前提:组件已经放入容器中
        //组件的四大特性:(名字、类型)、对象、作用域
        /* 名字:默认是方法名,即zhangsan,也可以自定义名字:@Bean("zhangsan111")
        组件名全局唯一,若组件名重复,则一定会给容器中放最先声明的那个(按照顺序排前面的那个)。
         */




        //4.1按照组件名获取对象 getBean(String name),       但需要强转,解决:4,4按照类型+名字获取组件对象
         /*从容器中获取组件对象:
        ①若组件不存在,则抛出异常NoSuchBeanDefinitionException
        ②若组件不唯一,按照类型只要一个时,则抛出异常NoUniqueBeanDefinitionException
        按照名字只要一个时,精确获取到指定对象。
        ③若组件不唯一,要一堆时,返回所有组件的List集合,可以正常运行。            →          4.3按照组件类型获取这种类型的所有组件对象 getBeanOfType(Map<String, T> typeToAutowireCandidates)
         */

        //Object zhangsan = ioc.getBean("zhangsan");//此处已知类型为Person,故可以直接强转
        Person zhangsan = (Person) ioc.getBean("zhangsan");
        System.out.println("从容器中获取到的组件对象:" + zhangsan);


        //4.2按照组件类型获取对象 getBean(Class<T> requiredType),若组件不存在,则抛出异常NoSuchBeanDefinitionException
//        Person bean = ioc.getBean(Person.class);
//        System.out.println("从容器中获取到的组件对象:" + bean);

        //4.3按照组件类型获取这种类型的所有组件对象 getBeanOfType(Map<String,T>
        Map<String, Person> type = ioc.getBeansOfType(Person.class);
        System.out.println("从容器中获取到的组件对象:" + type);

        //4.4按照类型和名字获取组件对象、
        Person bean = ioc.getBean("zhangsan", Person.class);
        System.out.println("从容器中获取到的组件对象:" + bean);

    }

    // 三.给容器中注册一个自己的组件;容器中每个组件都有自己的名字,方法名就是组件的名字 → 给容器中放了一个组件@Bean
    //获取组件名
    @Bean(name = "zhangsan")
    public Person zhangsan() {
        Person person = new Person();
        person.setName("张三");
        person.setAge(18);
        person.setGender("男");
        return person;
    }

    @Bean(name = "lisi")
    public Person lisi() {
        Person person = new Person();
        person.setName("李四");
        person.setAge(18);
        person.setGender("男");
        return person;
    }

    @Bean
    public Dog dog() {
        return new Dog();
    }

}

(4)注意

①组件的创建时机:容器启动过程中就会创建组件对象(构造方法在容器创建完成前)

②单实例特性:所有组件默认是单例的,每次获取直接从容器中拿。容器提前会创建组件 

二、@Configuration:管理组件:使用配置类

将注册进容器中的组件分类管理:使用配置类 分类管理 组件

使用@Configuration告诉Spring容器,这是一个配置类。

配置类也是组件,他是组件中的一种。

 

 

三、MVC分层注解

 首先,分层注解是给人看的,对于Spring来说,实际上标啥注解都是@Component。

但是,需遵守开发规范,即

@ComponentScan 

且分层注解所在能起作用的前提是这些组件必须在主程序所在的包及其子包结构下。假设没在时,可以这样解决,

 

四、@Import:第三方组件想要导入容器中,是没办法快速标注分层注解的。

解决方法1:

        @Bean,自己new,注册给容器 

解决方法2: 

        @Import

 五、若主类上注解太多,则新建一个配置类,专用于放置注解


原文地址:https://blog.csdn.net/weixin_58738870/article/details/142419158

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!