【Spring实战】—— 5 设值注入

本文介绍Spring框架中的依赖注入功能,并通过实例演示如何使用设值注入为对象注入依赖。包括定义接口与实现类、配置Bean及使用Spring上下文加载Bean。

本篇主要讲解了Spring的最常用的功能——依赖注入。

注入的方式,是使用Getter Setter注入,平时大多的编程也都是使用这种方法。

  举个简单的例子,还是表演者。

  表演者有自己的属性,年龄或者表演的歌曲等等。还需要一些复杂的属性,比如乐器,每一种乐器会发出不同的声音。

  下面看一下表演者Performer

package com.spring.test.action1;

public interface Performer {
    void perform() throws PerformanceException;
}

  我们自己定义一个钢琴演奏者,该表演者有年龄和歌曲,还有额外的一种乐器属性。

复制代码
package com.spring.test.setter;

import com.spring.test.action1.PerformanceException;
import com.spring.test.action1.Performer;

public class Instrumentalist implements Performer{
    private String song;
    private int age;
    private Instrument instrument;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSong() {
        return song;
    }
    public void setSong(String song) {
        this.song = song;
    }
    public Instrument getInstrument() {
        return instrument;
    }
    public void setInstrument(Instrument instrument) {
        this.instrument = instrument;
    }
    public Instrumentalist(){}
    public void perform() throws PerformanceException {
        System.out.println("Instrumentalist age:"+age);
        System.out.print("Playing "+song+":");
        instrument.play();
    }
}
复制代码

  乐器的构造如下,依然使用接口方式:

package com.spring.test.setter;

public interface Instrument {
    public void play();
}

  萨克斯实现该乐器接口

复制代码
package com.spring.test.setter;

public class Saxophone implements Instrument {
    public Saxophone(){}
    public void play() {
        System.out.println("TOOT TOOT TOOT");
    }
}
复制代码

  以上就是基本的类的构造了。

  下面看一下Spring的配置文件:

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     <bean id="sax" class="com.spring.test.setter.Saxophone"/>
     <bean id="kenny" class="com.spring.test.setter.Instrumentalist">
         <property name="song" value="Jingle Bells" />
         <property name="age" value="25" />
         <property name="instrument" ref="sax" />
     </bean>
</beans>
复制代码

  在配置文件中,可以发现,设值注入时,使用name来指定注入哪个属性

  name的命名方式依据变量名称。

  1 首字母不区分大小写,其他部分与变量名称相同。

  2 注入的属性类型,可以是String , int , double , float等,当属性是String或int时,可以根据变量的类型自动转换。

  3 注入的是一个bean,则直接使用ref链接到另一个bean即可。

  下面是应用上下文的代码:

复制代码
package com.spring.test.setter;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.test.action1.PerformanceException;

public class test {
    public static void main(String[] args) throws PerformanceException {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        Instrumentalist performer = (Instrumentalist)ctx.getBean("kenny");
        performer.perform();
    }
}
复制代码

  执行结果如下:

Instrumentalist age:25
Playing Jingle Bells:TOOT TOOT TOOT

 

本文转自博客园xingoo的博客,原文链接:【Spring实战】—— 5 设值注入,如需转载请自行联系原博主。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值