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

Spring框架依賴注入方法示例

 更新時(shí)間:2017年11月23日 15:35:10   作者:逆水行舟  
這篇文章主要介紹了Spring框架依賴注入方法示例,分享了三種方法示例,具有一定參考價(jià)值,需要的朋友可以了解下。

在閱讀這篇文章之前,大家可以先參閱理解Spring中的依賴注入和控制反轉(zhuǎn)一文,了解下依賴注入和控制反轉(zhuǎn)的相關(guān)內(nèi)容。

三種依賴注入的方式

屬性注入,通過(guò)setter方法注入bean的屬性值或依賴的對(duì)象 構(gòu)造注入 工廠方法注入(很少使用)
例子

這里我們使用了spring-4.3.2,maven配置文件

<dependency>
 <groupid>org.springframework</groupid>
 spring-core</artifactid>
 <version>${org.springframework-version}</version>
 <exclusions>
  <exclusion>
   <groupid>commons-logging</groupid>
   commons-logging</artifactid>
  </exclusion>
 </exclusions>
</dependency>
<dependency>
 <groupid>org.springframework</groupid>
 spring-beans</artifactid>
 <version>${org.springframework-version}</version>
</dependency>
<dependency>
 <groupid>org.springframework</groupid>
 spring-aop</artifactid>
 <version>${org.springframework-version}</version>
</dependency>
<dependency>
 <groupid>org.springframework</groupid>
 spring-context</artifactid>
 <version>${org.springframework-version}</version>
</dependency>
<dependency>
 <groupid>commons-logging</groupid>
 commons-logging</artifactid>
 <version>1.2</version>
</dependency>
<!-- Junit -->
<dependency>
 <groupid>junit</groupid>
 junit</artifactid>
 <version>3.8.1</version>
 <scope>test</scope>
</dependency>

applicationContext.xml配置文件

<!--?xml version="1.0" encoding="UTF-8"?-->
<beans xmlns="https://www.springframework.org/schema/beans" xmlns:p="https://www.springframework.org/schema/p" xmlns:util="https://www.springframework.org/schema/util" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
  https://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
 
 <!-- 
  配置bean
  id:標(biāo)識(shí)容器中bean對(duì)象
  class:bean的全類名,通過(guò)反射的方式在IOC容器中創(chuàng)建Bean,所以要求Bean類必須有無(wú)參構(gòu)造器
  -->
 <bean class="com.spring.test.HelloWorld" id="helloWorld">
  <property name="name" value="crystal"></property>
 </bean>
 
 <!-- 通過(guò)構(gòu)造方法配置bean,可以指定參數(shù)的位置和類型,以區(qū)分重載的構(gòu)造函數(shù) -->
 <bean class="com.spring.test.Car" id="car">
  <constructor-arg index="0" value="BENCHI"></constructor-arg>
  <constructor-arg index="1" type="double" value="200000.0"></constructor-arg>
 </bean>
 <bean class="com.spring.test.Car" id="car1">
  <!-- 如果字面值包含特殊字符,使用<![CDATA[]]>包裹起來(lái)
   屬性值可以使用value子節(jié)點(diǎn)來(lái)配置
   -->
  <constructor-arg type="java.lang.String">
   <value><!--[CDATA[<shanghai-->]]></value>
  </constructor-arg>
  <constructor-arg index="1" type="int" value="200"></constructor-arg>
 </bean>
 
 <bean class="com.spring.test.Person" id="person">
  <property name="name" value="Crystal"></property>
  <property name="age" value="20"></property>
  <!-- 可以使用ref建立引用之間的關(guān)系 -->
  <!-- 
  <property name="car" ref="car"></property>
   -->
  <!-- 
   <property name="car">
    <ref bean="car2"/>
   </property>
   -->
   <!-- 
   <property name="car">
   <bean class="com.spring.test.Car">
    <constructor-arg value="changanFute"></constructor-arg>
    <constructor-arg value="3000000"></constructor-arg>
    <constructor-arg value="240"></constructor-arg>
   </bean>
   </property> 
   -->
   <!-- 測(cè)試賦值null -->
   <!-- <property name="car"><null/></property> -->
   <property name="car" ref="car1"></property>
   <!-- 為級(jí)聯(lián)屬性賦值,注意:屬性需要先初始化后才能為級(jí)聯(lián)屬性賦值,和structs2不同 -->
   <property name="car.price" value="400000"></property>
 </bean>
 
 <!-- 測(cè)試配置集合屬性 -->
 <bean class="com.spring.test.collections.Person" id="person3">
  <property name="name" value="barry"></property>
  <property name="age" value="21"></property>
  <property name="cars">
   <list>
    <ref bean="car">
    <ref bean="car1">
    <bean class="com.spring.test.Car">
     <constructor-arg value="changanFute"></constructor-arg>
     <constructor-arg value="3000000"></constructor-arg>
     <constructor-arg value="240"></constructor-arg>
    </bean>
   </ref></ref></list>
  </property>
 </bean>
 
 <!-- 配置Map的屬性值 -->
 <bean class="com.spring.test.collections.NewPerson" id="newPerson">
  <property name="name" value="lina"></property>
  <property name="age" value="22"></property>
  <property name="cars">
   <!-- 使用map節(jié)點(diǎn)及map的entry子節(jié)點(diǎn)配置Map類型的成員變量 --><map>
    <entry key="AA" value-ref="car"></entry>
    <entry key="BB" value-ref="car1"></entry></map>
  </property>
 </bean>
 
 
 <!-- 配置Properties屬性值 -->
 <bean class="com.spring.test.collections.DataSource" id="dataSource">
  <property name="properties">
   <!-- 使用props和prop子節(jié)點(diǎn)來(lái)為Properties屬性賦值 -->
   <props>
    <prop key="user">root</prop>
    <prop key="password">1234</prop>
    <prop key="jdbcUrl">jdbc:mysql://test</prop>
    <prop key="jdbcDriver">com.mysql.jdbc.Driver</prop>
   </props>
  </property>
 </bean>
 
 <!-- 配置單例的集合bean,以供多個(gè)bean進(jìn)行引用,需要導(dǎo)入util命名空間 -->
 <util:list id="cars">
  <ref bean="car">
  <ref bean="car1">
 </ref></ref></util:list>
 
 <bean class="com.spring.test.collections.Person" id="person4">
  <property name="name" value="Jackie"></property>
  <property name="age" value="30"></property>
  <property name="cars" ref="cars"></property>
 </bean>
 
 <!-- 通過(guò)p命名空間為bean的屬性賦值,需要先導(dǎo)入p命名空間,相對(duì)于傳統(tǒng)配置方式更為簡(jiǎn)潔 -->
 <bean class="com.spring.test.collections.Person" id="person5" p:age="32" p:cars-ref="cars" p:name="Queue"></bean>
</beans>

1. 下面是簡(jiǎn)單的屬性注入、構(gòu)造注入的測(cè)試類

Car.java

package com.spring.test;
public class Car {
	private String name;
	private int maxSpeed;
	private double price;
	public Car() {
	}
	public Car(String name, double price) {
		this.name = name;
		this.price = price;
	}
	public Car(String name, int maxSpeed) {
		this.name = name;
		this.maxSpeed = maxSpeed;
	}
	public Car(String name, double price, int maxSpeed) {
		this.name = name;
		this.price = price;
		this.maxSpeed = maxSpeed;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	@Override
	 public String toString() {
		return "Car [name:" + name + ", price:" + price + ", maxSpeed:" + maxSpeed + "]";
	}
}

HelloWorld.java

package com.spring.test;
public class HelloWorld {
	private String name;
	public HelloWorld() {
		System.out.println("HelloWorld constructor...");
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		System.out.println("setName:" + name);
		this.name = name;
	}
	@Override
	 public String toString() {
		return "hello," + name;
	}
}

Person.java

package com.spring.test;
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;
	}
	@Override
	 public String toString() {
		return "Person: [name=" + name + ", age=" + age + ", car=" + car + "]";
	}
}

Main.java

package com.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
	public static void main(String[] args) {
		HelloWorld hello = new HelloWorld();
		hello.setName("barry");
		System.out.println("print:"+ hello + "\n");
		// 裝入 Spring 配置文件
		/**
   * 裝入 Spring 配置文件
   * ApplicationContext是IOC容器,它有兩個(gè)主要實(shí)現(xiàn)類(ClassPathXmlApplicationContext和FileSystemXmlApplicationContext)
   * ApplicationContext在初始化上下文時(shí)就實(shí)例化所有單例的Bean
   */
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		//HelloWorld hello1 = (HelloWorld) context.getBean("helloWorld"); // 通過(guò)id獲取bean對(duì)象
		HelloWorld hello1 = context.getBean(HelloWorld.class);
		// 通過(guò)類型獲取bean對(duì)象(要求在IOC容器里該類型的對(duì)象只能有一個(gè))
		System.out.println(hello1);
	}
	@Test
	 public void testContructor() {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Car car = (Car) context.getBean("car");
		// 通過(guò)類型獲取bean對(duì)象(要求在IOC容器里該類型的對(duì)象只能有一個(gè))
		Car car1 = (Car) context.getBean("car1");
		System.out.println(car);
		System.out.println(car1);
		Person person = (Person) context.getBean("person");
		System.out.println(person);
	}
}

2. 下面是集合的測(cè)試類
NewPerson.java

package com.spring.test.collections;
import java.util.Map;
import com.spring.test.Car;
public class NewPerson {
	private String name;
	private int age;
	private Map<string, car=""> cars;
	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 Map<string, car=""> getCars() {
		return cars;
	}
	public void setCars(Map<string, car=""> cars) {
		this.cars = cars;
	}
	@Override
	 public String toString() {
		return "Person: [name=" + name + ", age=" + age + ", cars=" + cars + "]";
	}
}

Person.java

package com.spring.test.collections;
import java.util.List;
import com.spring.test.Car;
public class Person {
	private String name;
	private int age;
	private List<car> cars;
	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 List<car> getCars() {
		return cars;
	}
	public void setCars(List<car> cars) {
		this.cars = cars;
	}
	@Override
	 public String toString() {
		return "Person: [name=" + name + ", age=" + age + ", cars=" + cars + "]";
	}
}

DataSource.java

package com.spring.test.collections;
import java.util.Properties;
public class DataSource {
	private Properties properties;
	public Properties getProperties() {
		return properties;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	@Override
	 public String toString() {
		return "DataSource: [properties:" + properties + "]";
	}
}

Main.java

package com.spring.test.collections;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
	@Test
	 public void testCollections() {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person) context.getBean("person3");
		System.out.println(person);
		NewPerson newPerson = (NewPerson) context.getBean("newPerson");
		System.out.println(newPerson);
		DataSource dataSource = (DataSource) context.getBean("dataSource");
		System.out.println(dataSource);
		Person person4 = (Person) context.getBean("person4");
		System.out.println(person4);
		Person person5 = (Person) context.getBean("person5");
		System.out.println(person5);
	}
}

總結(jié)

以上就是本文關(guān)于Spring框架依賴注入方法示例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

  • No ‘Access-Control-Allow-Origin‘ header is present跨域及解決

    No ‘Access-Control-Allow-Origin‘ header is&nb

    這篇文章主要介紹了No ‘Access-Control-Allow-Origin‘ header is present跨域及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • JavaSE系列基礎(chǔ)包裝類及日歷類詳解

    JavaSE系列基礎(chǔ)包裝類及日歷類詳解

    這篇文章主要介紹的是JavaSE中常用的基礎(chǔ)包裝類以及日歷類的使用詳解,文中的示例代碼簡(jiǎn)潔易懂,對(duì)我們學(xué)習(xí)JavaSE有一定的幫助,感興趣的小伙伴快來(lái)跟隨小編一起學(xué)習(xí)吧
    2021-12-12
  • RestTemplate發(fā)送HTTP?POST請(qǐng)求使用方法詳解

    RestTemplate發(fā)送HTTP?POST請(qǐng)求使用方法詳解

    這篇文章主要為大家介紹了RestTemplate發(fā)送HTTP?POST請(qǐng)求的使用方法詳解,有需要的朋友可以借鑒參考下希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • MyBatis無(wú)縫對(duì)接Spring的方法

    MyBatis無(wú)縫對(duì)接Spring的方法

    Spring框架與MyBatis框架是Java互聯(lián)網(wǎng)技術(shù)的主流框架。那么mybatis如何無(wú)縫對(duì)接spring呢?下面通過(guò)本文給大家介紹,需要的的朋友參考下吧
    2017-09-09
  • OKHttp3(支持Retrofit)的網(wǎng)絡(luò)數(shù)據(jù)緩存Interceptor攔截器的實(shí)現(xiàn)

    OKHttp3(支持Retrofit)的網(wǎng)絡(luò)數(shù)據(jù)緩存Interceptor攔截器的實(shí)現(xiàn)

    本篇文章主要介紹了OKHttp3(支持Retrofit)的網(wǎng)絡(luò)數(shù)據(jù)緩存Interceptor攔截器的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Java方法及數(shù)組相關(guān)原理解析

    Java方法及數(shù)組相關(guān)原理解析

    這篇文章主要介紹了Java方法及數(shù)組相關(guān)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Kotlin基礎(chǔ)教程之操作符與操作符重載

    Kotlin基礎(chǔ)教程之操作符與操作符重載

    這篇文章主要介紹了Kotlin基礎(chǔ)教程之操作符與操作符重載的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • mybatis使用xml進(jìn)行增刪改查代碼解析

    mybatis使用xml進(jìn)行增刪改查代碼解析

    這篇文章主要介紹了mybatis使用xml進(jìn)行增刪改查代碼解析,分享了相關(guān)配置和代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • java中如何區(qū)分==和equals

    java中如何區(qū)分==和equals

    這篇文章主要介紹了java中如何區(qū)分==和equals,文中講解的非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • Java使用開源Rxtx實(shí)現(xiàn)串口通訊

    Java使用開源Rxtx實(shí)現(xiàn)串口通訊

    這篇文章主要為大家詳細(xì)介紹了Java使用開源Rxtx實(shí)現(xiàn)串口通訊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12

最新評(píng)論