天天看点

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依赖注入的两种常用方式:属性注入与构造器注入