前言
:面向对象语言最基本的元素就是对象,在Spring中把对象都封装为一个个的Bean,即通过Bean容器来管理对象;那么接下来我们看下在Spring中如何创建所需要的Bean。
一、环境准备
员工类
package com.xlb.bean;
public class Employee {
private String name;
private Integer gender;//0=女 1=男
private Double salary;
public void setName(String name) {
this.name = name;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public void showInfo(){
System.out.println("name:" + name + " gender:" + gender + " salary:" + salary);
}
}
一、通过XML配置文件创建Bean
定义配置文件: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 id="employee" class="com.xlb.bean.Employee"/>
</beans>
如上配置文件所示,就写完了一个最基本的Bean配置文件,在该配置文件中定义了一个名为employee的Bean对象,在代码中我们可以通过下面的方式获取该Bean对象。
测试结果
package com.xlb;
import com.xlb.bean.Employee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBean {
public static void main(String[] args) {
test1();
}
public static void test1(){
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
Employee emp = ctx.getBean("employee", Employee.class);
emp.showInfo();
}
}
在上面的配置文件中,只配置了一个Bean对象,不过对于Bean对象内部的元素并没有进行初始化,这个初始化过程在Spring中称为依赖注入,只需要按照下面的方式配置即可:
<?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="employee" class="com.xlb.bean.Employee">
<property name="name" value="sala"/>
<property name="gender" value="0"/>
<property name="salary" value="10000"/>
</bean>
</beans>
再看一下输出结果:
可以看到,我们在上面配置的数据已经被初始化到对象中去。不过我们刚刚所注入的都是基本数据类型或者是其包装类型,如果我想注入集合类型或者引用类型,我们应该按照下面的方式:
简单修改下员工类
package com.xlb.bean;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
public class Employee {
private String name;
private Integer gender;//0=女 1=男
private Double salary;
List<String> hobbyList;
Set<String> hobbySet;
HashMap<String, String> trafficSpend;//不同交通工具通勤耗时
private Car car;//开的什么车
private List<Car> carList;
public void setName(String name) {
this.name = name;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public void showInfo(){
System.out.println("name:" + name + " gender:" + gender + " salary:" + salary);
System.out.println("hobbyList:" + hobbyList + " hobbySet:" + hobbySet);
System.out.println("trafficSpend:" + trafficSpend + " car:" + car);
System.out.println("carList:" + carList);
}
public void setHobbyList(List<String> hobbyList) {
this.hobbyList = hobbyList;
}
public void setHobbySet(Set<String> hobbySet) {
this.hobbySet = hobbySet;
}
public void setTrafficSpend(HashMap<String, String> trafficSpend) {
this.trafficSpend = trafficSpend;
}
public void setCar(Car car) {
this.car = car;
}
public void setCarList(List<Car> carList) {
this.carList = carList;
}
}
配置类
<?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.xlb.bean.Car">
<property name="name" value="梅赛德斯-迈巴赫"/>
<property name="color" value="黑色"/>
<property name="speed" value="300"/>
</bean>
<bean id="car1" class="com.xlb.bean.Car">
<property name="name" value="凯迪拉克"/>
<property name="color" value="黑色"/>
<property name="speed" value="200"/>
</bean>
<bean id="car2" class="com.xlb.bean.Car">
<property name="name" value="奥迪A4L"/>
<property name="color" value="黑色"/>
<property name="speed" value="200"/>
</bean>
<bean id="employee" class="com.xlb.bean.Employee">
<property name="name" value="sala"/>
<property name="gender" value="0"/>
<property name="salary" value="10000"/>
<property name="car" ref="car"/>
<property name="hobbyList">
<list>
<value>游泳</value>
<value>游泳</value>
<value>跑步</value>
<value>爬山</value>
</list>
</property>
<property name="hobbySet">
<set>
<value>游泳</value>
<value>游泳</value>
<value>跑步</value>
<value>爬山</value>
</set>
</property>
<property name="trafficSpend">
<map>
<entry key="步行" value="60min"/>
<entry key="开车" value="10min"/>
</map>
</property>
<property name="carList">
<list>
<ref bean="car"/>
<ref bean="car1"/>
<ref bean="car2"/>
</list>
</property>
</bean>
</beans>
测试结果
或者使用下面这种方式配置,即使用p
命名空间简化配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="car" class="com.xlb.bean.Car"
p:name="梅赛德斯-迈巴赫"
p:color="黑色"
p:speed="300"/>
<bean id="car1" class="com.xlb.bean.Car"
p:name="凯迪拉克"
p:color="黑色"
p:speed="200"/>
<bean id="car2" class="com.xlb.bean.Car"
p:name="奥迪A4L"
p:color="黑色"
p:speed="200"/>
<bean id="employee" class="com.xlb.bean.Employee"
p:name="sala"
p:gender="0"
p:salary="10000" p:car-ref="car">
<property name="hobbyList">
<list>
<value>游泳</value>
<value>游泳</value>
<value>跑步</value>
<value>爬山</value>
</list>
</property>
<property name="hobbySet">
<set>
<value>游泳</value>
<value>游泳</value>
<value>跑步</value>
<value>爬山</value>
</set>
</property>
<property name="trafficSpend">
<map>
<entry key="步行" value="60min"/>
<entry key="开车" value="10min"/>
</map>
</property>
<property name="carList">
<list>
<ref bean="car"/>
<ref bean="car1"/>
<ref bean="car2"/>
</list>
</property>
</bean>
</beans>
2.2 Bean类型
通过bean标签的scope属性控制:
scope=“singleton”::单例
scope=“prototype”:原型
scope=“request”:HTTP请求,来一个请求创建一个Bean
scope=“session”:HTTP会话级别
2.3 依赖其他Bean的创建
如果当前Bean的创建以来其他Bean的创建,则可以在Bean标签中使用depends-on属性 注明。