日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

[Spring Framework]学习笔记--Dependency injection(DI)

發布時間:2024/10/12 javascript 72 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Spring Framework]学习笔记--Dependency injection(DI) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 通過構造函數實現DI

簡單類型實例

package examples;public class ExampleBean {// Number of years to calculate the Ultimate Answerprivate int years;// The Answer to Life, the Universe, and Everythingprivate 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 constructorprivate 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的方式好了。

轉載于:https://www.cnblogs.com/lemonbar/p/3896863.html

總結

以上是生活随笔為你收集整理的[Spring Framework]学习笔记--Dependency injection(DI)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。