Spring 依赖注入方式
1.属性注入
2.构造方法注入
3.工厂方式注入
Java代码
package com.smart.ditype;
public class Car {
private int maxSpeed;
private String brand;
private double price;
private String corp;
public Car() {
}
public Car(double price, String brand) {
this.price = price;
this.brand = brand;
}
public Car(String brand, String corp, double price, int maxSpeed) {
this.brand = brand;
this.corp = corp;
this.price = price;
this.maxSpeed = maxSpeed;
}
public Car(String brand, String corp, double price) {
this.brand = brand;
this.corp = corp;
this.price = price;
}
public Car(String brand, String corp, int maxSpeed) {
this.brand = brand;
this.corp = corp;
this.maxSpeed = maxSpeed;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getCorp() {
return corp;
}
public void setCorp(String corp) {
this.corp = corp;
}
}
package com.smart.ditype;
public class CarFactory {
public Car createHongQiCar() {
Car car = new Car();
car.setBrand("红旗CA72");
return car;
}
public static Car createStaticCar() {
Car car = new Car();
car.setBrand("红旗CA73");
return car;
}
}
1.属性注入:属性注入指通过SetXxx()方法注入Bean的属性值或者依赖方法。由于属性注入方式具有可选择性和灵活性高的特点,因此属性注入是实际应用中最常采用的注入方式。
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 id="car" class="com.smart.ditype.Car">
<property name="brand">
<value>红旗CA72</value>
</property>
<property name="maxSpeed">
<value>200</value>
</property>
<property name="price">
<value>200000.00</value>
</property>
</bean>
</beans>
2.构造方法注入:构造方法注入是属性注入外的另一种常用的注入方式,它保证一些必须的属性在Bean实例时得到设置,确保Bean在实例化后可以直接使用
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="car1" class="com.smart.ditype.Car">
<constructor-arg type="java.lang.String">
<value>红旗CA72</value>
</constructor-arg>
<constructor-arg type="java.lang.Double">
<value>200000.00</value>
</constructor-arg>
</bean>
<!--通过构造方法注入bean 指定入参类型注入数据不能搞定时,通过指定位置入参-->
<bean id="car2" class="com.smart.ditype.Car">
<constructor-arg index="0">
<value>红旗CA72</value>
</constructor-arg>
<constructor-arg index="1">
<value>中国一汽</value>
</constructor-arg>
<constructor-arg index="2">
<value>200000.00</value>
</constructor-arg>
<constructor-arg index="3">
<value>200</value>
</constructor-arg>
</bean>
<!--通过构造方法注入,当指定位置不能全搞定时,可以加上类型一起注入参数-->
<bean id="car3" class="com.smart.ditype.Car">
<constructor-arg index="0">
<value>红旗CA72</value>
</constructor-arg>
<constructor-arg index="1">
<value>中国一汽</value>
</constructor-arg>
<constructor-arg index="2" type="java.lang.Double">
<value>200000.00</value>
</constructor-arg>
</bean>
<bean id="car4" class="com.smart.ditype.Car">
<constructor-arg index="0">
<value>红旗CA72</value>
</constructor-arg>
<constructor-arg index="1">
<value>中国一汽</value>
</constructor-arg>
<constructor-arg index="2" type="int">
<value>200</value>
</constructor-arg>
</bean>
<bean id="office" class="com.smart.ditype.Office">
</bean>
</beans>
3.工厂方式注入:工厂方式是应用中经常用到的一种设计模式,主要用来控制反转和单实例设计思想的主要实现方法。
<?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="CarFactory" class="com.smart.ditype.CarFactory">
</bean>
<bean id="car5" factory-bean="CarFactory" factory-method="createHongQiCar"></bean>
<!--静态工厂方法实例化注入bean-->
<bean id="car6" class="com.smart.ditype.CarFactory" factory-method="createStaticCar"></bean>
</beans>