欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年05月17日 09:12:00   作者:阿木俠  
這篇文章主要介紹了Spring依賴注入的兩種方式(根據(jù)實例詳解),非常具有實用價值,需要的朋友可以參考下

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

Set方法注入:

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

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

Set方法注入時spring中配置文件:

<?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="高級轎車"></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類:

package org.spring01; 
 
public class Car { 
  private String name;//車名 
  private String color;//顏色 
  private String clas;//等級 
  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類:

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 + "]"; 
  } 
   
} 

測試類:

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); 
    }   
} 

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

Person [name=張三, age=11, car=Car [name=奔馳, color=土豪金, clas=高級轎車]]

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

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

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

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

<?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="中級轎車"></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類:

package org.spring02; 
 
public class Car { 
  private String name;//車名 
  private String color;//顏色 
  private String 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類:

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 + "]"; 
  } 
   
} 

測試類:

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); 
    } 
} 

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

Person [name=李四, age=23, car=Car [name=大眾, color=白色, clas=中級轎車]]

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

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

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

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

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用ElasticSearch6.0快速實現(xiàn)全文搜索功能的示例代碼

    使用ElasticSearch6.0快速實現(xiàn)全文搜索功能的示例代碼

    本篇文章主要介紹了使用ElasticSearch6.0快速實現(xiàn)全文搜索功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • java中線程池的關(guān)閉問題

    java中線程池的關(guān)閉問題

    這篇文章主要介紹了java中線程池的關(guān)閉問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • SpringMVC中的HandlerAdapter解析

    SpringMVC中的HandlerAdapter解析

    這篇文章主要介紹了SpringMVC中的HandlerAdapter解析,HandlerAdapter是一個關(guān)鍵的組件,用于將請求與處理程序方法進行適配和調(diào)度,它充當(dāng)了控制器和處理程序之間的橋梁,負(fù)責(zé)將請求的參數(shù)和處理程序方法進行匹配,并將結(jié)果返回給前端,需要的朋友可以參考下
    2023-10-10
  • ThreadPoolExecutor核心線程數(shù)和RocketMQ消費線程調(diào)整詳解

    ThreadPoolExecutor核心線程數(shù)和RocketMQ消費線程調(diào)整詳解

    這篇文章主要介紹了ThreadPoolExecutor核心線程數(shù)和RocketMQ消費線程調(diào)整詳解,Rocketmq 消費者在高峰期希望手動減少消費線程數(shù),通過DefaultMQPushConsumer.updateCorePoolSize方法可以調(diào)用內(nèi)部的setCorePoolSize設(shè)置多線程核心線程數(shù),需要的朋友可以參考下
    2023-10-10
  • JdbcTemplate方法介紹與增刪改查操作實現(xiàn)

    JdbcTemplate方法介紹與增刪改查操作實現(xiàn)

    這篇文章主要給大家介紹了關(guān)于JdbcTemplate方法與增刪改查操作實現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者使用JdbcTemplate具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Spring的@ConfigurationProperties注解詳解

    Spring的@ConfigurationProperties注解詳解

    這篇文章主要介紹了Spring的@ConfigurationProperties注解詳解,@ConfigurationProperties該注解是用來獲取yml或者properties配置文件的配置信息,下面根據(jù)一些配置信息給出案例代碼進行講解,需要的朋友可以參考下
    2023-11-11
  • SpringBoot整合SpringSecurity和JWT的示例

    SpringBoot整合SpringSecurity和JWT的示例

    這篇文章主要介紹了SpringBoot整合SpringSecurity和JWT的示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 淺談@RequestParam(required = true)的誤區(qū)

    淺談@RequestParam(required = true)的誤區(qū)

    這篇文章主要介紹了@RequestParam(required = true)的誤區(qū),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java中雙向鏈表詳解及實例

    Java中雙向鏈表詳解及實例

    這篇文章主要介紹了Java中雙向鏈表詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • SpringBoot中的application.properties無法加載問題定位技巧

    SpringBoot中的application.properties無法加載問題定位技巧

    這篇文章主要介紹了SpringBoot中的application.properties無法加載問題定位技巧,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05

最新評論