激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Spring依賴注入的兩種方式(根據(jù)實(shí)例詳解)

Spring依賴注入的兩種方式(根據(jù)實(shí)例詳解)

2020-10-05 12:35阿木俠 Java教程

這篇文章主要介紹了Spring依賴注入的兩種方式(根據(jù)實(shí)例詳解),非常具有實(shí)用價(jià)值,需要的朋友可以參考下

1,Set注入    2,構(gòu)造注入

Set方法注入:

原理:通過類的setter方法完成依賴關(guān)系的設(shè)置

name屬性的取值依setter方法名而定,要求這個(gè)類里面這個(gè)對(duì)應(yīng)的屬性必須有setter方法。

Set方法注入時(shí)spring中配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8"?>
<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
  <bean id="car" class="org.spring01.Car">
    <constructor-arg value="奔馳"></constructor-arg>
    <constructor-arg type="java.lang.String">
      <value>土豪金</value>
    </constructor-arg>
    <constructor-arg value="高級(jí)轎車"></constructor-arg>
  </bean>
   
  <bean id="person" class="org.spring01.Person">
    <property name="name" value="張三"></property>
    <property name="age" value="11"></property>
    <property name="car" ref="car"></property>
  </bean>
</beans>

定義Car類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package org.spring01;
 
public class Car {
  private String name;//車名
  private String color;//顏色
  private String clas;//等級(jí)
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getColor() {
    return color;
  }
  public void setColor(String color) {
    this.color = color;
  }
  public String getClas() {
    return clas;
  }
  public void setClas(String clas) {
    this.clas = clas;
  }
  public Car(String name, String color, String clas) {
    super();
    this.name = name;
    this.color = color;
    this.clas = clas;
  }
 
  public Car() {
    super();
    // TODO Auto-generated constructor stub
  }
  @Override
  public String toString() {
    return "Car [name=" + name + ", color=" + color + ", clas=" + clas
        + "]";
  }
   
}

定義Person類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package org.spring01;
 
public class Person {
  private String name;//名字
  private int age;//年齡
  private Car car;//他的車
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public Car getCar() {
    return car;
  }
  public void setCar(Car car) {
    this.car = car;
  }
  public Person(String name, int age, Car car) {
    super();
    this.name = name;
    this.age = age;
    this.car = car;
  }
  public Person() {
    super();
    // TODO Auto-generated constructor stub
  }
  @Override
  public String toString() {
    return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
  }
   
}

測(cè)試類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package org.spring01;
 
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class SpringTest{
    @Test
    public void toGetPerson(){
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      Person person = (Person) context.getBean("person");
      System.out.println(person);
    }
    @Test
    public void toGetCar(){
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      Car car = (Car) context.getBean("car");
      System.out.println(car);
    }  
}

使用單元測(cè)試(JUnit)測(cè)試toGetPerson()方法,結(jié)果為:

?
1
Person [name=張三, age=11, car=Car [name=奔馳, color=土豪金, clas=高級(jí)轎車]]

構(gòu)造方法注入:

原理:通過構(gòu)造函數(shù)完成依賴關(guān)系的設(shè)定

構(gòu)造注入指的是在接受注入的類中,定義一個(gè)構(gòu)造方法,并在構(gòu)造方法的參數(shù)中定義需要注入的元素,其中,index表示構(gòu)造方法中的參數(shù)索引(第一個(gè)參數(shù)索引為0)。

構(gòu)造方法注入時(shí)spring中配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
  <bean id="car" class="org.spring02.Car">
    <constructor-arg value="大眾"></constructor-arg>
    <constructor-arg type="java.lang.String">
      <value>白色</value>
    </constructor-arg>
    <constructor-arg value="中級(jí)轎車"></constructor-arg>
  </bean>
   
   
  <bean id="person" class="org.spring02.Person">
    <constructor-arg index="0" value="李四"></constructor-arg>
    <constructor-arg index="1" value="23"></constructor-arg>
    <constructor-arg index="2" ref="car"></constructor-arg>
  </bean>
</beans>

定義Car類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package org.spring02;
 
public class Car {
  private String name;//車名
  private String color;//顏色
  private String clas;//等級(jí)
   
  public Car(String name, String color, String clas) {
    super();
    this.name = name;
    this.color = color;
    this.clas = clas;
  }
 
  public Car() {
    super();
    // TODO Auto-generated constructor stub
  }
  @Override
  public String toString() {
    return "Car [name=" + name + ", color=" + color + ", clas=" + clas
        + "]";
  }
   
}

定義Person類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package org.spring02;
 
public class Person {
  private String name;//名字
  private int age;//年齡
  private Car car;//他的車
   
  public Person(String name, int age, Car car) {
    super();
    this.name = name;
    this.age = age;
    this.car = car;
  }
  public Person() {
    super();
    // TODO Auto-generated constructor stub
  }
  @Override
  public String toString() {
    return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
  }
   
}

測(cè)試類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package org.spring02;
 
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class SpringTest{
    @Test
    public void toGetPerson(){
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext01.xml");
      Person person = (Person) context.getBean("person");
      System.out.println(person);
    }
    @Test
    public void toGetCar(){
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext01.xml");
      Car car = (Car) context.getBean("car");
      System.out.println(car);
    }
}

使用單元測(cè)試(JUnit)測(cè)試toGetPerson()方法,結(jié)果為:

?
1
Person [name=李四, age=23, car=Car [name=大眾, color=白色, clas=中級(jí)轎車]]

上面的例子都采用了單元測(cè)試的方法檢測(cè)運(yùn)行結(jié)果,需要導(dǎo)庫(kù): JUnit

Demo的大體結(jié)構(gòu):

Spring依賴注入的兩種方式(根據(jù)實(shí)例詳解)

我們可以看到,set方法和構(gòu)造方法都可以設(shè)值成功, 實(shí)際開發(fā)中最常用到的是set方法設(shè)值。但這兩種依賴注入的方式并沒有絕對(duì)的好壞,只是使用的場(chǎng)合不同。

使用構(gòu)造注入可以在構(gòu)建對(duì)象的同時(shí)完成依賴關(guān)系到的建立,所以如果要建立的對(duì)象的關(guān)系很多,使用構(gòu)造注入會(huì)在構(gòu)造方法上留下很多參數(shù),可讀性極差,所以當(dāng)對(duì)象的關(guān)系比較多的時(shí)候采用set方法注入。

使用set方法注入是通過類的setter方法完成依賴關(guān)系的設(shè)置的,所以不能保證相關(guān)的數(shù)據(jù)在執(zhí)行時(shí)不被更改設(shè)定。所以如果想使一些數(shù)據(jù)變?yōu)橹蛔x或者私有,就要采用構(gòu)造注入了。

建議采用以set注入為主,構(gòu)造注入為輔的注入策略。對(duì)于依賴關(guān)系無(wú)須變化的注入,盡量采用構(gòu)造注入;而其他的依賴關(guān)系的注入,則考慮采用set注入。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/weixin_36380516/article/details/72301260?utm_source=tuicool&utm_medium=referral

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99影视电影电视剧在线播放 | 草莓福利视频在线观看 | 国产精品午夜在线观看 | 在线成人精品视频 | 精品成人久久久 | 亚洲精中文字幕二区三区 | 在线成人av观看 | 在线亚州 | 久久大陆 | www噜噜偷拍在线视频 | 性色av免费在线观看 | av免费入口| 色婷婷综合久久久中文一区二区 | 性高跟鞋xxxxhd4kvideos | 久久无| av性色全交蜜桃成熟时 | 国产精品九九久久一区hh | av电影免费在线看 | 成人毛片网 | 毛片免费大全短视频 | 成人免费看片a | 国产99视频精品免视看9 | 免费毛片电影 | 久久国产成人午夜av浪潮 | 久久亚洲一区二区三区成人国产 | 毛片免费看电影 | 午夜一级| 久久精品re| 国产午夜精品久久久久婷 | 国产一级在线观看视频 | 精品一区二区三区日本 | 久久国产精品久久精品国产演员表 | 免费国产a | 免费啪视频在线观看 | 国产午夜精品久久久久久免费视 | 精品久久久久久久久中文字幕 | 久久成人视屏 | 久草手机在线观看视频 | 精品国产高清一区二区三区 | 国产一区免费在线 | 久久精品一区二区三区国产主播 |