spring获取bean三种方式,依赖注入

spring-day02

spring获取bean的三种方式

谈到spring如何获取bean,首先要注意的是spring通过xml配置文件将bean注册到IOC容器中,但是spring是怎样创建对象呢?
spring是利用反射+工厂模式来创建对象的,并且创建的默认是单例对象的

根据id获取

由于id是bean的唯一标识,并且不能够重复,所以根据bean的id就可以唯一获取到一个组件对象

<bean id="studentOne" class="com.xinkai.spring.pojo.Student" scope="prototype"></bean>

测试代码:
 // 获取ioc容器
 ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
 // 获取bean
 Student studentOne = (Student) ioc.getBean("studentOne");
 System.out.println(studentOne);

根据类型来获取bean(用的多)

<bean id="studentOne" class="com.xinkai.spring.pojo.Student" scope="prototype"></bean>
测试代码:

ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
Student student = ioc.getBean(Student.class);
System.out.println(student);

但是如果有两个类型相同,但是id不同的bean会发生什么?

<bean id="studentOne" class="com.xinkai.spring.pojo.Student" scope="prototype"></bean>
<bean id="studentTwo" class="com.xinkai.spring.pojo.Student"></bean>

如果仍然以类型来寻找bean,那么就会报错,不知道寻找哪一个bean,这个时候需要用到第三种获取bean的方式了

通过类型和id来获取bean(用的少)

// scope的默认值是单例,否则就是多例
<bean id="studentOne" class="com.xinkai.spring.pojo.Student" scope="prototype"></bean>
<bean id="studentTwo" class="com.xinkai.spring.pojo.Student"></bean>

测试代码:
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
Student student2 = ioc.getBean("studentOne", Student.class);
System.out.println(student2);

可以使用实体类的接口获取bean吗

	如果有一个接口Person,其中Student继承了Person,那么就可以使用接口来获取该实体类对象
例如:
Person person = ioc.getBean(Person.class);

结论:
通过bean的类型,bean所继承的类的类型,bean所实现接口的类型都可以获取bean对象
以后用到的更多是面向接口编程。

当然了,如果根据bean的接口获取对象时,该接口有好多个实现类,那么肯定会抛异常,因为spring不知道该获取哪一个对象

spring的依赖注入

set方法注入

1、通过属性赋值,利用成员变量的set方法进行赋值,set注入
其中property:通过成员变量的set方法赋值, name:属性名 value:属性值

    <bean id="studentTwo" class="com.xinkai.spring.pojo.Student">
        <property name="sid" value="1001"></property>
        <property name="sname" value="张三"></property>
        <property name="age" value="20"></property>
        <property name="gender" value="男"></property>
    </bean>

        ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student studentTwo = ioc.getBean("studentTwo", Student.class);
        System.out.println(studentTwo);

构造器赋值

    <bean id="studentThree" class="com.xinkai.spring.pojo.Student">
        <constructor-arg value="1002"></constructor-arg>
        <constructor-arg value="李四"></constructor-arg>
        <constructor-arg value="24"></constructor-arg>
        <constructor-arg value="女"></constructor-arg>
    </bean>

Student studentThree = ioc.getBean("studentThree", Student.class);
System.out.println(studentThree);

解释:如果有多个构造器,或者说构造器中参数的顺序不一致呢,可以利用name确定属性名

总结:以上都是属于xml配置文件进行注册bean的,其中注册的bean默认是单例的,并且id时唯一不能重复,class只能是实体类,不能是接口类型,scope可以修改成多例。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值