首页 > 编程语言 > 详细

【Spring】二、入门和IOC创建对象

时间:2021-07-30 23:04:53      阅读:23      评论:0      收藏:0      [点我收藏+]

3、入门:第一个Spring

3.1、使用

1、编写实体类

public class HelloSpring {

    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "HelloSpring{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ‘}‘;
    }
}

2、配置元数据

Spring容器创建、组装并管理Bean对象;

  • id:变量名;
  • class:要实例化的pojo对象;
  • property:对象的属性
    • name:属性名;
    • 基本数据类型:使用value作为属性值;
    • 引用数据类型:使用ref作为属性值,引用容器中的其他Bean
  • 原理:通过反射实现。默认先通过无参构造创建实例,再通过getter()获取属性名,通过setter()设置属性值。
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="hello" class="indi.jaywee.pojo.HelloSpring">
        <property name="id" value="7"/>
        <property name="name" value="jaywee"/>
    </bean>
</beans>

3、实例化容器

可以传入多个参数

ApplicationContext context = new ClassPathXmlApplicationContext("xxx.xml","xxx.xml");

4、获取Bean

要获取哪个bean对象,参数就写哪个对象的 id

context.getBean("beanId");

3.2、测试

@Test
public void testToString() {
    // 实例化容器
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

    HelloSpring hello = (HelloSpring) context.getBean("hello");

    System.out.println(hello);
}

测试结果

技术分享图片

如果用户有不同需求,只需修改XML文件中元数据的配置即可,不再需要修改程序代码。

3.3、练习

改进IOC理论推导中的需求实现。

  1. 配置Spring元数据

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="userDaoImpl" class="indi.jaywee.dao.UserDaoImpl"/>
        <bean id="userDaoMySqlImpl" class="indi.jaywee.dao.UserDaoMySqlImpl"/>
        <!--
            引用数据类型:使用ref作为属性值,引用容器中的其他Bean。
        -->
        <bean id="userServiceImpl" class="indi.jaywee.service.UserServiceImpl">
            <property name="userDao" ref="userDaoImpl"/>
        </bean>
    
    </beans>
    
  2. 测试

    @Test
    public void testSpring(){
        // 实例化容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    
        UserService userService = (UserServiceImpl)context.getBean("userServiceImpl");
    
        userService.getUserInfo();
    }
    

4、IOC创建对象

原理:通过反射实现

IOC配置元数据时,创建 Bean对象的方式如下:

4.1、无参构造

要求:必须要有无参构造方法和setter方法。

  • 通过无参构造方法创建实例;
  • 通过setter()设置属性值。
<bean id="hello" class="indi.jaywee.pojo.HelloSpring">
    <property name="id" value="7"/>
    <property name="name" value="jaywee"/>
</bean>

4.2、有参构造

要求:要有相应的有参构造方法,允许重载。

通过有参构造方法为变量赋值,可以通过以下 3种方式:

4.2.1、下标

根据构造方法中声明的参数顺序,下标从0开始。

<bean id="hello" class="indi.jaywee.pojo.HelloSpring">
    <constructor-arg index="0" value="7"/>
    <constructor-arg index="1" value="jaywee"/>
</bean>

4.2.2、类型

不推荐使用!因为当参数列表中有多个相同类型的参数时无法使用。

  • 基本数据类型:直接写类名;
  • 引用数据类型:需要写全限类名。
<bean id="hello" class="indi.jaywee.pojo.HelloSpring">
    <constructor-arg type="int" value="7"/>
    <constructor-arg type="java.lang.String" value="jaywee"/>
</bean>

4.2.4、参数名

推荐使用!

<bean id="hello" class="indi.jaywee.pojo.HelloSpring">       
    <constructor-arg name="id" value="7"/>
    <constructor-arg name="name" value="jaywee"/>
</bean>

4.3、对象创建时间

在实例化容器时,元数据中配置的所有Bean对象都已创建完成了。

  • 如果是用无参构造方法,Bean对象都已创建完成,属性尚未赋值;

  • 如果是用有参构造方法,Bean对象都已创建完成,属性已被赋值。即创建并组装完成。

进行如下测试

4.3.1、实体类

实体类一旦被实例化,就会执行构造方法中的代码。

  • 只要有输出语句:说明Bean已完成创建;
  • 只要输出的toString()中有设置好的值,说明Bean已完成装配。
public class HelloSpring {

    private int id;
    private String name;

    public HelloSpring() {
        System.out.println("无参构造方法:Hello Spring被创建了!");
        System.out.println(this.toString());
    }

    public HelloSpring(int id, String name) {
        System.out.println("有参构造方法:Hello Spring被创建了!");
        this.id = id;
        this.name = name;
        System.out.println(this.toString());
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "HelloSpring{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ‘}‘;
    }
}

4.3.2、测试

@Test
public void testToString() {
    // 实例化容器
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
}

修改元数据的配置,测试在不同构造方法的情况下,Bean对象的创建和装配情况:

  1. 有参构造方法

    • 配置元数据

      <bean id="hello" class="indi.jaywee.pojo.HelloSpring">
          <constructor-arg name="id" value="7"/>
          <constructor-arg name="name" value="jaywee"/>
      </bean>
      
    • 测试结果

      技术分享图片

    • 结论:在实例化容器时,有参构造方法的Bean对象已创建并组装完成。

  2. 无参构造方法

    • 配置元数据

      <bean id="hello" class="indi.jaywee.pojo.HelloSpring">
          <property name="id" value="7"/>
          <property name="name" value="jaywee"/>
      </bean>
      
    • 测试结果:

      技术分享图片

    • 结论:在实例化容器时,无参构造方法的Bean对象已创建完成,但还未组装。

    • 进一步测试:

      @Test
      public void testToString() {
          // 实例化容器
          ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
      
      
          HelloSpring hello = (HelloSpring) context.getBean("hello");
          System.out.println("从容器中获取Bean");
      
          System.out.println(hello);
      }
      
    • 测试结果:

      技术分享图片

    • 结论:在从容器中获取Bean时,无参构造方法才完成组装。

4.4.4、结论

不管是用哪种构造方法,在实例化容器时,元数据中配置的所有Bean对象都已完成创建。但是不同构造方法的组装情况有所不同。

  • 有参构造方法:Bean对象不仅完成创建,而且完成了组装;(类似单例模式饿汉式
  • 无参构造方法:获取Bean对象时,才完成组装。(类似单例模式懒汉式

【Spring】二、入门和IOC创建对象

原文:https://www.cnblogs.com/secretmrj/p/15082677.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!