当前位置:   article > 正文

Spring Ioc创建Bean对象的三种方式_ioc对于javabean的创建方式有几种?

ioc对于javabean的创建方式有几种?

IOC创建对象的方式

首先我们创建一个User类:

package com.jie.ioc01;
public class User {

    private String name;
    private int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

使用无参构造创建对象,Spring默认的方式

​ 注意:如果User类没有无参构造的话运行将会报错

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

    <!--默认使用无参构造方式创建对象-->
    <bean id="user" class="com.jie.ioc01.User">
        <!--要使用property必须要有set方法-->
        <property name="name" value="张三"></property>
        <property name="age" value="19"></property>
    </bean>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

测试:

package com.jie.ioc01;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUser {

    public static void main(String[] args) {

        //获取bean.xml中所有的java bean对象
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

        //获取java bean 中id为user的java对象
        User user = (User)context.getBean("user");

        //输出该对象
        System.out.println(user);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

输出结果:

User{name='张三', age=19}
  • 1

使用有参构造方式(三种)

都需要使用标签

第一种:使用下标(index)

​ index=“0” 就是有参构造方法里面的第一个参数,以此类推

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

    <!--使用有参构造第一种方式:使用下标-->
    <bean id="user" class="com.jie.ioc01.User">
        <constructor-arg index="0" value="李四"></constructor-arg>
        <constructor-arg index="1" value="19"></constructor-arg>
    </bean>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

测试,输出结果:

User{name='李四', age=19}	
  • 1

第二种:根据类型来创建(type)

​ 不推荐这种,要是构造方法中参数的类型都一样的话容易分不清楚

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

    <!--使用有参构造第二种方式——不推荐:根据类型来创建-->
    <bean id="user" class="com.jie.ioc01.User">
        <constructor-arg type="java.lang.String" value="王五"></constructor-arg>
        <constructor-arg type="int" value="16"></constructor-arg>
    </bean>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

测试,输出结果:

User{name='王五', age=16}
  • 1

第三种:使用参数名(name)

​ 推荐这种方式的,简单明了,一一对应类中的属性

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

   <!--使用有参构造第三种方式——常用的:使用参数名来创建-->
    <bean id="user" class="com.jie.ioc01.User">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="22"></constructor-arg>
    </bean>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

测试,输出结果:

User{name='张三', age=22}
  • 1

扩展:

​ 在写上new ClassPathXmlApplicationContext(“bean.xml”);后,bean.xml中的所有java bean对象都被创建了,接下来我们做一个测试:

在创建一个几个类:

package com.jie.ioc01;

public class User02 {

    public User02() {
        System.out.println("User02被创建了");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
package com.jie.ioc01;

public class User03 {

    public User03() {
        System.out.println("User03也被创建了");
    }
    
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

bean.xml中添加bean

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

	<!--User02-->
    <bean id="user02" class="com.jie.ioc01.User02">
    </bean>

    <!--User03-->
    <bean id="user03" class="com.jie.ioc01.User03">
    </bean>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

测试:

package com.jie.ioc01;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUser {

    public static void main(String[] args) {

        //获取bean.xml中所有的java bean对象
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

运行结果:

User02被创建了
User03也被创建了
  • 1
  • 2

而且这些javabean对象都是单例的,接下来我们也来测试下:

package com.jie.ioc01;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUser {

    public static void main(String[] args) {

        //获取bean.xml中所有的java bean对象
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

        //获取java bean 中id为user的java对象
        User user = (User)context.getBean("user");

        //获取java bean 中id为user的java对象
        User user2 = (User)context.getBean("user");

        //输出该对象
        System.out.println(user == user2);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

输出结果:

User02被创建了
User03也被创建了
true
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/241585
推荐阅读
相关标签
  

闽ICP备14008679号