国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[Spring Framework]學習筆記--Dependency injection(DI)

2019-11-14 20:48:11
字體:
來源:轉載
供稿:網友
[SPRing Framework]學習筆記--Dependency injection(DI)

1. 通過構造函數實現DI

簡單類型實例

package examples;public class ExampleBean {    // Number of years to calculate the Ultimate Answer    private int years;    // The Answer to Life, the Universe, and Everything    private String ultimateAnswer;
    //如果不能在debug模式下進行編譯,必須要加下面一行。針對下面的方式3,通過參數名字。  @ConstructorProperties({"years", "ultimateAnswer"})
public ExampleBean(int years, String ultimateAnswer) {        this.years = years;        this.ultimateAnswer = ultimateAnswer;    }}

相應的xml配置為

<bean id="exampleBean" class="examples.ExampleBean">  //方式1,通過類型,如果存在兩個參數同類型的話,不行。    <constructor-arg type="int" value="7500000"/>    <constructor-arg type="java.lang.String" value="42"/>  //方式2,通過參數位置。
    <constructor-arg index="0" value="7500000"/>    <constructor-arg index="1" value="42"/>
  //方式3,通過參數名字。
    <constructor-arg name="years" value="7500000"/>    <constructor-arg name="ultimateAnswer" value="42"/>
</bean>

對象類型實例

package x.y;public class Foo {    public Foo(Bar bar, Baz baz) {        // ...    }}

xml配置

<beans>    <bean id="foo" class="x.y.Foo">        <constructor-arg ref="bar"/>
        <constructor-arg ref="baz"/>    </bean>    <bean id="bar" class="x.y.Bar"/>    <bean id="baz" class="x.y.Baz"/></beans>

參數也可以像下面這樣指定

    <constructor-arg>        <ref bean="bar"/>    </constructor-arg>

如果是通過工廠模式,可以采用下面的方式

<bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance">    <constructor-arg ref="anotherExampleBean"/>    <constructor-arg ref="yetAnotherBean"/>    <constructor-arg value="1"/></bean><bean id="anotherExampleBean" class="examples.AnotherBean"/><bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {    // a private constructor    private ExampleBean(...) {        ...    }    // a static factory method; the arguments to this method can be    // considered the dependencies of the bean that is returned,    // regardless of how those arguments are actually used.    public static ExampleBean createInstance (        AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {        ExampleBean eb = new ExampleBean (...);        // some other Operations...        return eb;    }}

2. 通過set方法來實現DI

<bean id="exampleBean" class="examples.ExampleBean">    <!-- setter injection using the nested ref element -->    <property name="beanOne">        <ref bean="anotherExampleBean"/>    </property>    <!-- setter injection using the neater ref attribute -->    <property name="beanTwo" ref="yetAnotherBean"/>    <property name="integerProperty" value="1"/></bean><bean id="anotherExampleBean" class="examples.AnotherBean"/><bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

注:property的name是和set方法中的名字一致的。

public class ExampleBean {    private AnotherBean beanOne;    private YetAnotherBean beanTwo;    private int i;    public void setBeanOne(AnotherBean beanOne) {        this.beanOne = beanOne;    }    public void setBeanTwo(YetAnotherBean beanTwo) {        this.beanTwo = beanTwo;    }    public void setIntegerProperty(int i) {        this.i = i;    }}

總結:在我們日常的工程中,上面兩種方式如何使用呢?

可以從需要來看,如果這個屬性是必須的,那就放在構造函數中;如果是可選的,那就用set的方式好了。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 专栏| 上林县| 略阳县| 淮北市| 乌苏市| 四平市| 昭通市| 淳化县| 拉孜县| 永泰县| 扬中市| 读书| 治多县| 内江市| 密山市| 扶风县| 福清市| 灵武市| 屏边| 武夷山市| 桃源县| 裕民县| 华阴市| 屯留县| 德阳市| 桐庐县| 尉犁县| 穆棱市| 漾濞| 铜川市| 潢川县| 阆中市| 饶河县| 板桥市| 兴安县| 万山特区| 彰化县| 理塘县| 旺苍县| 南康市| 农安县|