Spring控制反转容器的使用例子
详细代码如下:
spring-config.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 name="product" class="com.mstf.bean.Product"/>
<!-- 通过参数名传递参数 -->
<bean name="featuredProduct" class="com.mstf.bean.Product">
<constructor-arg name="name" value="孝感"/>
<constructor-arg name="description" value="简介"/>
<constructor-arg name="price" value="9.95"/>
</bean>
<!-- 通过指数方式传递参数 -->
<bean name="featuredProduct2" class="com.mstf.bean.Product">
<constructor-arg index="0" value="孝感"/>
<constructor-arg index="1" value="简介"/>
<constructor-arg index="2" value="9.95"/>
</bean>
<bean id="calendar" class="java.util.Calendar" factory-method="getInstance"/>
<!-- 通过构造器方式实例化 -->
<bean name="employee1" class="com.mstf.bean.Employee">
<property name="homeAddress" ref="simpleAddress"/>
<property name="firstName" value="孝"/>
<property name="lastName" value="感"/>
</bean>
<!-- 构造器方式依赖注入 -->
<bean name="employee2" class="com.mstf.bean.Employee">
<constructor-arg name="firstName" value="孝"/>
<constructor-arg name="lastName" value="感"/>
<constructor-arg name="homeAddress" ref="simpleAddress"/>
</bean>
<!-- 被依赖类(Employee依赖于Address类,以下配置是为了每个Employee实例都能包含Address实例) -->
<bean name="simpleAddress" class="com.mstf.bean.Address">
<constructor-arg name="line1" value="湖北省孝感市"/>
<constructor-arg name="line2" value=""/>
<constructor-arg name="city" value="孝感市"/>
<constructor-arg name="state" value="China"/>
<constructor-arg name="zipCode" value="99999"/>
<constructor-arg name="country" value="CN"/>
</bean>
</beans>
Address
package com.mstf.bean;
/**
* Address类和Employee类属于setter方式依赖注入
* @author wangzheng
*
*/
public class Address {
private String line1;
private String line2;
private String city;
private String state;
private String zipCode;
private String country;
public Address(String line1, String line2, String city, String state, String zipCode, String country) {
super();
this.line1 = line1;
this.line2 = line2;
this.city = city;
this.state = state;
this.zipCode = zipCode;
this.country = country;
}
@Override
public String toString() {
return "Address [line1=" + line1 + ", line2=" + line2 + ", city=" + city + ", state=" + state + ", zipCode="
+ zipCode + ", country=" + country + "]";
}
}
Employee
package com.mstf.bean;
/**
* Employee类和Address类属于setter方式依赖注入
* @author wangzheng
*
*/
public class Employee {
private String firstName;
private String lastName;
private Address homeAddress;
public Employee() {
}
public Employee(String firstName, String lastName, Address homeAddress) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.homeAddress = homeAddress;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Address getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
@Override
public String toString() {
return "Employee [firstName=" + firstName + ", lastName=" + lastName + ", homeAddress=" + homeAddress + "]";
}
}
Product
package com.mstf.bean;
import java.io.Serializable;
/**
* 带参数的构造器来初始化类
* @author wangzheng
*
*/
public class Product implements Serializable { // product类
private static final long serialVersionUID = 1L;
private String name;
private String descripition;
private float price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescripition() {
return descripition;
}
public void setDescripition(String descripition) {
this.descripition = descripition;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Product() {
}
public Product(String name, String descripition, float price) {
super();
this.name = name;
this.descripition = descripition;
this.price = price;
}
}
我们有两个方法来进行软件设计:一个是让其足够的简单以至于让BUG无法藏身;另一个就是让其足够的复杂,让人找不到BUG。前者更难一些。

浙公网安备 33010602011771号