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
    评论
Spring提供了几种依赖注入方式。其中一种方式是自动装配,它可以通过上下文自动寻找bean并为其配置属性。这种方式可以减少手动配置的工作量,提高开发效率。另一种方式是使用反射来调用bean的默认构造函数实例化对象,并通过set方法来注入属性值。这样可以实现bean的依赖注入。此外,Spring还提供了工厂方法的功能,可以通过工厂注入的方式来进行Spring依赖注入。工厂类可以屏蔽目标类的实例化步骤,调用者甚至不需要指定具体的目标类是什么。这种方式在一些遗留系统或第三方类库中还是会使用到。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Bean自动装配,注解](https://blog.csdn.net/weixin_41709536/article/details/108609172)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Spring框架学习教程,详解Spring注入bean的几种方式](https://blog.csdn.net/Java___interview/article/details/120550945)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值