(II)第二节:HelloWordl 案例

HelloWorld:通过各种方式给容器中注册对象

  以前是自己new对象,现在所有的对象交给容器创建;给容器中注册组件

  框架使用流程:

  (1)导包

    使用 Maven 的方式,添加依赖:

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring-version>4.0.0.RELEASE</spring-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    注意:Spring 运行的时候依赖一个 日志包,如果没有就会报错。

  (2)编写配置文件

    Spring的配置文件中,集合了Spring的ioc容器管理的所有组件(类)

    Idea2020版创建顺序:New—》XML Configuration File----》Spring Config

  (3)测试

    使用 Spring 创建对象,为属性赋值

    Person 类:

public class Person {
    private String lastName;
    private Integer age;
    private String gender;
    private String email;
    //省略有参无参、get/set()、toString()
}

    ioc.xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--注册一个Person对象,Spring会自动创建这个Person对象-->
    <!--
        一个bean标签可以注册一个组件(对象,类)
        class:写要注册的组件全类名
        id:这个对象的唯一标识
    -->
    <bean id="person01" class="com.njf.spring.bean.Person">
        <!--
            使用property标签为Person对象的属性赋值
            name="lastName": 指定属性名
            value="子龙": 指定属性值
         -->
        <property name="lastName" value="子龙"></property>
        <property name="age" value="18"></property>
        <property name="email" value="zilong@achang.com"></property>
        <property name="gender" value="男"></property>
    </bean>

</beans>

    通过 Spring的 IOC 容器创建 Person 类实例

   /**
    * 从容器中拿到这个组件
    */
   @Test
   public void test() {
      //ApplicationContext:代表ioc容器
      //ClassPathXmlApplicationContext:当前应用的xml配置文件在ClassPath下
      //根据Spring的配置文件得到ioc容器对象
      ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");

      //容器帮我们创建好了对象
      Person person01 = (Person) ioc.getBean("person01");
      Person person02 = (Person) ioc.getBean("person01");
      System.out.println("person02 = " + person02);
      System.out.println(person01 == person02); //true

      //System.out.println("=============================");
      //Person person03 = (Person) ioc.getBean("person03");//No bean named 'person02' available

   }

  

  总结

    src,源码包开始路径,成为类路径的开始; 所有源码包的东西都会被合并放在类路径

java: /bin/

web: /WEB-INF/classes/

  (1)ApplicationContext(IOC容器的接口)中的

—new ClassPathXmlApplicationContext(“ioc.xml”):ioc配置文件在类路径下
—FileSystemXmlApplicationContext(“F://ioc.xml”):ioc配置文件在磁盘路径下

  (2)给容器中注册了组件;我们也从容器中按照id拿到了组件的对象了吗?

      组件的创建工作,是容器完成的

    Person对象是什么时候被创建的?

      容器中对象的创建在容器创建完成时就已经创建好了

  (3)同一个组件在 ioc 容器中是单实例的,且在容器启动完成都已经创建好了

  (4)如果容器中没有组件,无法获得 报异常----> No bean named ‘person02’ available

  (5)ioc容器在创建这个组件对象时,会利用其setter()方法为javaBean的属性赋值

  (6)javaBean的属性名是由什么决定的?

    由getter/setter方法后面的名决定,首字母小写所有的getter/setter都自动生成!!!千万别乱改

原文地址:https://www.cnblogs.com/niujifei/p/15390182.html