天天看點

Spring依賴注入的兩種常用方式:屬性注入與構造器注入

set方法注入

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 配置bean 
		class: bean 的全類名,通過反射的方式在IOC 容器中建立Bean,是以要求
		Bean中必須有無參的構造方法
		id: 辨別容器中的bean. id是唯一的,類中通過此id擷取對象
	-->
	<bean id="helloWorld2" class="com.atguigu.spring.beans.HelloWorld">
	<!-- 通過set方法進行注入 -->
		<property name="name2" value="China"></property>
	</bean>
</beans>
           

Main.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		// 1.建立Spring的IOC容器對象
		// ApplicationContext代表的是IOC容器
		// ClassPathXmlApplicationContext:是 ApplicationContext接口的實作類。該實作類從類路徑下加載配置檔案
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		// 2.從IOC容器中擷取Bean示例
		// getBean方法是在BeanFactory中定義的,有兩種常用的getBean方法
		// 方法一:getBean(xml中bean的id名稱);通過id擷取
		HelloWorld helloWorld = (HelloWorld)ctx.getBean("helloWorld2");
		// 方法二:getBean(Class<T>);通過類型擷取
		// 缺點:當IOC容器中配置了兩個bean時,此方法不知道傳回哪一個,會報錯
		HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
		// 3.調用hello方法
		helloWorld.hello();
		
	}
}
           
Spring依賴注入的兩種常用方式:屬性注入與構造器注入
Spring依賴注入的兩種常用方式:屬性注入與構造器注入

HelloWorld.java

package com.atguigu.spring.beans;

public class HelloWorld {
	private String name;
	// setName2與applicationContext.xml中的property中的name的值對應。即set方法注入
	public void setName2(String name) {
		this.name = name;
	}
	
	public void hello() {
		System.out.println("hello: " + name);
	}
}

           

構造方法注入

Spring依賴注入的兩種常用方式:屬性注入與構造器注入

Car.java

package com.atguigu.spring.beans;

public class Car {
	private String brand;
	private String corp;
	private double price;
	private int maxSpeed;
	public Car(String brand, String corp, double price) {
		super();
		this.brand = brand;
		this.corp = corp;
		this.price = price;
	}
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
	}	
}
           

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 通過構造方法配置bean的屬性 -->
	<!-- 方式一 -->
	<bean id="car" class="com.atguigu.spring.beans.Car">
		<constructor-arg value="Baoma"></constructor-arg>
		<constructor-arg value="Beijing"></constructor-arg>
		<constructor-arg value="3000"></constructor-arg>
	</bean>
	<!-- 方式二 -->
	<bean id="car" class="com.atguigu.spring.beans.Car">
		<constructor-arg value="Baoma" index="0"></constructor-arg>
		<constructor-arg value="Beijing" index="1"></constructor-arg>
		<constructor-arg value="3000"  index="2"></constructor-arg>
	</bean>
		<!-- 方式三 當有多個重載構造方法時,可以指定參數的位置和參數的類型,以區分重載的構造器 -->
	<bean id="car" class="com.atguigu.spring.beans.Car">
		<constructor-arg value="Baoma" index="0"></constructor-arg>
		<constructor-arg value="Beijing" index="1"></constructor-arg>
		<constructor-arg value="240" type="int"></constructor-arg>
	</bean>
</beans>
           

Main.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
	public static void main(String[] args) {
		// 1.建立Spring的IOC容器對象
		// ApplicationContext代表的是IOC容器
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		// 2.從IOC容器中擷取Bean示例
		Car car = ctx.getBean(Car.class);
		System.out.println(car);		
	}
}
           
Spring依賴注入的兩種常用方式:屬性注入與構造器注入