本文主要是介绍2、Spring4之Bean的两种配置方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. Bean 属性的配置方式
1). setter 方法注入(最常用的方式)
①. 在 Bean 中为属性提供 setter 方法:
public void setBrand(String brand) {
this.brand = brand;
}
public void setCorp(String corp) {
this.corp = corp;
}
public void setPrice(float price) {
this.price = price;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
②. 在配置文件中使用 property 注入属性值
<bean id="car" class="com.atguigu.spring.ioc.Car">
<property name="brand" value="Audi"></property>
<property name="corp" value="一汽"></property>
<property name="maxSpeed" value="200"></property>
<property name="price" value="200000"></property>
</bean>
2). 构造器注入:
①. bean 中提供构造器:
public Car(String brand, String corp, int price, int maxSpeed) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
this.maxSpeed = maxSpeed;
}
public Car(String brand, String corp, float price) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
}
public Car(String brand, String corp, int maxSpeed) {
super();
this.brand = brand;
this.corp = corp;
this.maxSpeed = maxSpeed;
}
②. 配置文件中使用 constructor-arg 节点配置使用构造器注入属性值
<bean id="car2" class="com.atguigu.spring.ioc.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="ChangAn"></constructor-arg>
<constructor-arg value="250000"></constructor-arg>
<constructor-arg value="190"/>
</bean>
③. 注意:对于重载的构造器可以通过参数的类型来匹配对应的构造器
<bean id="car3" class="com.atguigu.spring.ioc.Car">
<constructor-arg value="Buike"></constructor-arg>
<constructor-arg value="ShanghaiTongYong"></constructor-arg>
<constructor-arg value="180000"></constructor-arg>
</bean>
<!--
因为有重载的构造器:
public Car(String brand, String corp, float price)
public Car(String brand, String corp, int maxSpeed)
所以必须指定使用哪一个构造器来初始化属性值. 可以使用构造器参数的类型来选择需要的构造器!
-->
1). setter 方法注入(最常用的方式)
①. 在 Bean 中为属性提供 setter 方法:
public void setBrand(String brand) {
this.brand = brand;
}
public void setCorp(String corp) {
this.corp = corp;
}
public void setPrice(float price) {
this.price = price;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
②. 在配置文件中使用 property 注入属性值
<bean id="car" class="com.atguigu.spring.ioc.Car">
<property name="brand" value="Audi"></property>
<property name="corp" value="一汽"></property>
<property name="maxSpeed" value="200"></property>
<property name="price" value="200000"></property>
</bean>
2). 构造器注入:
①. bean 中提供构造器:
public Car(String brand, String corp, int price, int maxSpeed) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
this.maxSpeed = maxSpeed;
}
public Car(String brand, String corp, float price) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
}
public Car(String brand, String corp, int maxSpeed) {
super();
this.brand = brand;
this.corp = corp;
this.maxSpeed = maxSpeed;
}
②. 配置文件中使用 constructor-arg 节点配置使用构造器注入属性值
<bean id="car2" class="com.atguigu.spring.ioc.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="ChangAn"></constructor-arg>
<constructor-arg value="250000"></constructor-arg>
<constructor-arg value="190"/>
</bean>
③. 注意:对于重载的构造器可以通过参数的类型来匹配对应的构造器
<bean id="car3" class="com.atguigu.spring.ioc.Car">
<constructor-arg value="Buike"></constructor-arg>
<constructor-arg value="ShanghaiTongYong"></constructor-arg>
<constructor-arg value="180000"></constructor-arg>
</bean>
<!--
因为有重载的构造器:
public Car(String brand, String corp, float price)
public Car(String brand, String corp, int maxSpeed)
所以必须指定使用哪一个构造器来初始化属性值. 可以使用构造器参数的类型来选择需要的构造器!
-->
解决办法:
<bean id="car4" class="com.atguigu.spring.ioc.Car">
<constructor-arg value="Nissan"></constructor-arg>
<constructor-arg value="Zhengzhou"></constructor-arg>
<constructor-arg value="210" type="int"></constructor-arg>
</bean>
这篇关于2、Spring4之Bean的两种配置方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!