依赖注入:
Spring 依赖注入(Dependency Injection,DI)和控制反转含义相同,它们是从两个角度描述的同一个概念。
当某个 Java 实例需要另一个 Java 实例时,传统的方法是由调用者创建被调用者的实例
(例如,使用 new 关键字获得被调用者实例),而使用 Spring 框架后,被调用者的实例不再由调用者创建,而是由 Spring 容器创建,这称为控制反转。
Spring 容器在创建被调用者的实例时,会自动将调用者需要的对象实例注入给调用者,调用者通过 Spring 容器获得被调用者实例,这称为依赖注入。
依赖注入主要有两种实现方式,分别是 setter 注入(又称设值注入)和构造函数注入。
1)setter 注入
指 IoC 容器使用 setter 方法注入被依赖的实例。通过调用无参构造器或无参 static 工厂方法实例化 Bean 后,调用该 Bean 的setter 方法,即可实现基于setter的 DI。
在 Spring 实例化 Bean 的过程中,首先会调用默认的构造方法实例化 Bean 对象,然后通过 Java 的反射机制调用 setXxx() 方法进行属性的注入。因此,setter 注入要求 Bean 的对应类必须满足以下两点要求。
* 必须提供一个默认的无参构造方法。
* 必须为需要注入的属性提供对应的 setter 方法。
使用 setter 注入时,在 Spring 配置文件中,需要使用 <bean> 元素的子元素 <property> 为每个属性注入值。而使用构造注入时,在配置文件中,主要使用 <constructor-arg> 标签定义构造方法的参数,使用其 value 属性(或子元素)设置该参数的值。
setter注入
Person类
package com.openlab.Bean;
import lombok.Data;
@Data
public class Person {
private Car car;
public void car(){
car.show();
}
}
·car类
package com.openlab.Bean;
import lombok.Data;
@Data
public class Car {
private String brand;
private String corp;
private double price;
private int maxpeed;
public void show() {
System.out.println(toString());
}
}
·beans.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-3.0.xsd">
<bean id="person" class="com.openlab.Bean.Person">
<property name="car" ref="car"></property>
</bean>
<bean id="car" class="com.openlab.Bean.Car">
<property name="brand" value="baoma"/>
<property name="corp" value="gongsi"/>
<property name="maxpeed" value="2000"/>
<property name="price" value="34.75"/>
</bean>
</beans>
测试文件
package com.openlab.test;
import com.openlab.Bean.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class personTest {
@Test
public void test01(){
ApplicationContext context=new ClassPathXmlApplicationContext("beans2.xml");
Person per = context.getBean(Person.class);
per.car();
}
}
2)构造函数注入
指 IoC 容器使用构造函数注入被依赖的实例。可以通过调用带参数的构造函数实现依赖注入,每个参数代表一个依赖。
构造函数注入
·person文件
package com.openlab.Bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
public class Person {
private Car car;
public Person(Car car) {
System.out.println("在person的构造函数内");
this.car=car;
}
public void car(){
car.show();
}
}
·car文件
package com.openlab.Bean;
import lombok.*;
@Data
public class Car {
private String brand;
private String corp;
private double price;
private int maxpeed;
public void show() {
System.out.println(toString());
}
//必须要有构造函数,无参和有参
public Car() {
}
public Car(String brand, String corp, double price, int maxpeed) {
this.brand = brand;
this.corp = corp;
this.price = price;
this.maxpeed = maxpeed;
}
}
·beans.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-3.0.xsd">
<bean id="car" class="com.openlab.Bean.Car">
<constructor-arg value="baoma"/>
<constructor-arg value="gongsi" />
<constructor-arg value="30.78" type="double"/>
<constructor-arg value="2000" type="int"/> //按顺序写
</bean>
<bean id="person" class="com.openlab.Bean.Person">
<constructor-arg ref="car" type="com.openlab.Bean.Car"/>
</bean>
</beans>
测试文件如上setter注入所示