深入学习Spring框架之二构造器注入方式装配Bean

本文介绍了如何使用Spring框架通过构造器注入方式创建Bean对象。详细解释了不同类型的构造器注入,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一节我们来看看怎么使用Spring创建我们的Bean对象。

    容器是Spring的核心,Spring的容器有两种类型:Bean工厂,由BeanFactory接口定义,是最简单的容器;以及应用上下文,由ApplicationContext定义。Bean工厂对于大多数应用来说太低级了,因此应用上下文更为受欢迎。

    应用上下文又可以分为以下几种类型:

        ClassPathXmlApplicationContext----从类路径下的XML配置文件加载上下文,把应用上下文定义文件当作类资源

FileSystemXmlApplicationContext----读取文件系统下的XML配置文件并加载上下文定义

XmlWebApplicationContext----读取Web应用下的XML配置文件并装载上下文定义

 

    下面我们来应用Spring创建Bean对象,首先我们来模拟一个环境,G20峰会的晚会表演,据说很不赖,画面直接可以做壁纸

 


 
  1. /**

  2. * 面向接口编程,表演者接口

  3. * @author liuyc

  4. *

  5. */

  6. public interface Performer {

  7. public void perform();

  8. }


 
  1. /**

  2. * 歌手类

  3. * @author liuyc

  4. *

  5. */

  6. @SuppressWarnings("all")

  7. public class Singer implements Performer{

  8.  
  9. private String song;

  10.  
  11. private Instrument instrument;

  12.  
  13. public Singer() {

  14. }

  15.  
  16. public Singer(String song) {

  17. this.song = song;

  18. }

  19.  
  20. public Singer(String song, Instrument instrument) {

  21. super();

  22. this.song = song;

  23. this.instrument = instrument;

  24. }

  25.  
  26. @Override

  27. public void perform() {

  28. this.instrument.play(song);

  29. System.out.println("while singing ...");

  30. }

  31. }


 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  5.  
  6. <bean id="singer" class ="com.cm2easy.miki.example.chapter1.Singer"></bean>

  7.  
  8. </beans>

这样我们就声明了一个简单Bean,在xml配置文件中我们没有进行任何多余的配置,这个时候spring默认是调用类的默认构造器,所产生的结果与我们的传统创建对象的方法是一样的:

 

 

Singer singer = new Singer();

 

 

我们还可以调用有参的构造器声明一个Bean:

 


 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  5.  
  6. <bean id="singer" class ="com.cm2easy.miki.example.chapter1.Singer">

  7. <constructor-arg value="听妈妈的话"></constructor-arg>

  8. </bean>

  9.  
  10. </beans>

这种构造器注入的方式相当于我们调用有参构造new一个对象,效果是一模一样的:

 

 

Singer singer = new Singer("听妈妈的话");

 

 

我们不仅可以像上面那样通过构造器注入基本类型的变量,还可以通过构造器将对象的引用注入:

 


 
  1. /**

  2. * 乐器接口

  3. * @author liuyc

  4. *

  5. */

  6. public interface Instrument {

  7. public void play(String melodyName);

  8. }


 
  1. /**

  2. * 钢琴类

  3. * @author liuyc

  4. *

  5. */

  6. public class Piano implements Instrument{

  7. @Override

  8. public void play(String melodyName) {

  9. System.out.println("正在演奏" + melodyName);

  10. }

  11. }


 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  5.  
  6. <bean id="singer" class ="com.cm2easy.miki.example.chapter1.Singer">

  7. <constructor-arg value="听妈妈的话"></constructor-arg>

  8. <constructor-arg ref="piano"></constructor-arg>

  9. </bean>

  10. <bean id="piano" class="com.cm2easy.miki.example.chapter1.Piano"></bean>

  11. </beans>

如上所示,在调用public Singer(String song, Instrument instrument)构造器之前,先要声明Instrument的对象,再通过构造器注入生命singer对象,这样歌手不仅可以唱歌还可以演奏对应歌曲的钢琴曲了

 

构造器注入的方式声明Bean对象,无外乎上面的情况,掌握了以上配置要想通过构造器声明对象就可以游刃有余了。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值