zoukankan      html  css  js  c++  java
  • 【Spring实战】—— 5 设值注入

    本篇主要讲解了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
  • 相关阅读:
    20175325 实现mypwd(选做,加分)
    A公司 推荐算法大赛 总结
    CSLM 配置粗解
    iOS开发之百度地图导航
    iOS开发之百度地图的集成——地图标注&POI检索
    iOS开发之集成百度地图踩过的那些坑(基于 Xcode7.0/iOS9.2)
    Swift开发第二篇——extension及fatalError
    iOS开发之AFN的基本使用
    iOS开发之多线程技术——NSOperation篇
    iOS开发之多线程技术——GCD篇
  • 原文地址:https://www.cnblogs.com/xing901022/p/4248433.html
Copyright © 2011-2022 走看看