美文网首页
Spring的常用类的作用

Spring的常用类的作用

作者: 奇点一氪 | 来源:发表于2020-10-09 16:48 被阅读0次

    ClassPathXmlApplicationContext

    核心在创建IoC容器对象,用容器获取bean实例,有了实例就可以根据API提供的方法使用了。 而且,一个IoC容器可以获取多个实例:
    // 1. 创建 Spring 的 IOC 容器

    ClassPathXmlApplicationContext applicationContext= new ClassPathXmlApplicationContext("spring.xml"); 
    
    1. 从 IOC 容器中获取 bean 的实例
      HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
      Car car = (Car) ctx.getBean("car");

    //这里是重复的例子,用bean.xml中的id = car
    Car car2 = (Car) ctx.getBean(“car2”);

    //3. 使用 bean //有了实例,就可以通过实例来调用实例的方法,那么实例有哪些方法,文档中是要写清楚的。方法可以直接用。

    相关文章

      网友评论

          本文标题:Spring的常用类的作用

          本文链接:https://www.haomeiwen.com/subject/fcnlpktx.html