bean的创建

文章目录
  1. 1. 普通的bean创建
  2. 2. FactoryBean创建Bean
  3. 3. 其它方式创建Bean

spring在xml驱动的BeanFactory中创建bean的方式有很多种方式。

普通的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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置一个简单的bean -->
<bean id="mycar" class="com.study.pack.xml.factorybean.Car"></bean>

<!-- 配置一个简单的bean,并为其加入name和price属性 -->
<bean id="mycarB" class="com.study.pack.xml.factorybean.Car">
<property name="name" value="宝马"/>
<property name="price" value="2000"/>
</bean>
</beans>
public class Test {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("factory-bean.xml"));
Car carA = (Car)factory.getBean("mycarA");
Car carB = (Car)factory.getBean("mycarB");
System.out.println(carA);
System.out.println(carB);
}
// 输出结果
// null--0.0
// 宝马--2000.0
}

@Data
public class Car {
private String name;
private double price;

@Override
public String toString() {
return this.name + "--" + this.price;
}
}
FactoryBean创建Bean

FactoryBean创建bean的不同之处在于增加了一个FactoryBean实现类,该类负责相关bean的创建细节,而在xml中配置的也是该FactoryBean的信息。

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 通过指定的FactoryBean创建Bean -->
<bean id="car" class="com.study.pack.xml.factorybean.CarFactoryBean">
<property name="message" value="比亚迪,20000"/>
</bean>
</beans>
public class FactoryBeanTest {

public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("factory-bean.xml"));
Car car = (Car) factory.getBean("car");
// 加上&是只返回该FactoryBean实例
CarFactoryBean carFactoryBean = (CarFactoryBean) factory.getBean("&car");
System.out.println(car);
System.out.println(carFactoryBean);
}
// 输出信息
// 比亚迪--20000.0
// 比亚迪,20000
}

public class CarFactoryBean implements FactoryBean<Car> {
private String message;

@Override
public Car getObject() throws Exception {
Car car = new Car();
String[] messageArray = message.split(",");
car.setName(messageArray[0]);
car.setPrice(Double.parseDouble(messageArray[1]));
return car;
}

@Override
public Class<?> getObjectType() {
return Car.class;
}

@Override
public boolean isSingleton() {
return true;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

@Override
public String toString() {
return this.message;
}
}
其它方式创建Bean

除了以上两种创建方式之外,spring还可以通过工厂方法来创建实例,工厂方法创建bean实例分两种情况,一种是通过静态方法创建,另一种是通过非静态方法创建。

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!-- 静态方法 -->
<bean id="secondCar" class="com.study.pack.xml.factorybean.SimpleCarFactory" factory-method="getCar"/>

<!-- 非静态方法 -->
<bean id="firstCar" factory-bean="simpleCarFactory" factory-method="createCar"/>
<bean id="simpleCarFactory" class="com.study.pack.xml.factorybean.SimpleCarFactory"/>

</beans>
public class Test {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("factory-bean.xml"));
Car firstCar = (Car)factory.getBean("firstCar");
Car secondCar = (Car)factory.getBean("secondCar");
System.out.println(firstCar);
System.out.println(secondCar);
}
// 输出
//吉利--200.0
//保时捷--150000.0
}

public class SimpleCarFactory {

// 静态方法
public static Car getCar() {
Car car = new Car();
car.setName("保时捷");
car.setPrice(150000);
return car;
}

// 非静态方法
public Car createCar() {
Car car = new Car();
car.setName("吉利");
car.setPrice(200);
return car;
}
}