学习Spring有一段时间了,这里会把一些知识点给梳理一遍。
这里要说到的是-Spring创建bean的四种方式:
整个工程的目录结构,用maven进行了管理:
配置文件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" 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.2.xsd">
<!--方式一:用class属性直接指定-->
<bean id="createCarBean" class="com.sg.bean.Car">
<property name="brand" value="BMW"/>
<property name="price" value="12000.0"></property>
</bean>
<!--方式二:
静态工厂创建bean
class为静态工厂全类名
factory-method为静态方法,这里必须为静态方法
-->
<bean id="staticFactory" class="com.sg.factory.StaticFactory" factory-method="carFactory"/>
<!--方式三:实例工厂创建bean -->
<bean id="instanceFactory" class="com.sg.factory.InstanceFactory"/>
<!--
class为要创建的类型
factory-bean之前实例工厂的id
factory-method为实例方法
-->
<bean id="createInstanceFactory" class="com.sg.bean.Car" factory-bean="instanceFactory" factory-method="carFactory" />
<!--
方式四:实现FactoryBean
class直接指向实现FactoryBean的类
-->
<bean id="factoryCar" class="com.sg.factory.FactoryCar"></bean>
</beans>
要创建实例bean的Car类:
package com.sg.bean;
public class Car {
//汽车的品牌
private String brand;
//汽车的价格
private Double price;
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;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
静态工厂创建bean:
package com.sg.factory;
import com.sg.bean.Car;
/**
* 静态工厂
* @author sg
*
*/
public class StaticFactory {
public static Car carFactory(){
Car car=new Car();
car.setBrand("BYD");
car.setPrice(20000.0);
return car;
}
}
实例工厂创建bean:
package com.sg.factory;
import com.sg.bean.Car;
/**
* 实例工厂
* @author sg
*
*/
public class InstanceFactory {
public Car carFactory(){
Car car=new Car();
car.setBrand("QQ");
car.setPrice(120000.0);
return car;
}
}
继承于FactoryBean创建bean:
package com.sg.factory;
import org.springframework.beans.factory.FactoryBean;
import com.sg.bean.Car;
public class FactoryCar implements FactoryBean<Car>{
public Car getObject() throws Exception {
Car car=new Car();
car.setBrand("Benz");
car.setPrice(200000.0);
return car;
}
public Class<?> getObjectType() {
return Car.class;
}
/*bean的作用域*/
public boolean isSingleton() {
return false;
}
}
测试类:
package com.sg.beanTest;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sg.bean.Car;
/**
* 创建bean测试
* @author sg
* Bean的创建方式,这里会演示四种方式
* 分别为:
* 1-直接class 指定
* 2-使用静态工厂的方法
* 3-使用实例工厂的方法
* 4-继承于BeanFactory
*/
public class CreateBean {
/***
* 直接的指定class
*/
@Test
public void create001(){
ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = act.getBean("createCarBean",Car.class);
System.out.println(car.toString());
}
/**
* 使用静态工厂方法
*/
@Test
public void create002(){
ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = act.getBean("staticFactory",Car.class);
System.out.println(car.toString());
}
/**
* 创建实例工厂
*/
@Test
public void create003(){
ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = act.getBean("createInstanceFactory",Car.class);
System.out.println(car.toString());
}
/**
* 实现FactoryBean的接口
*/
@Test
public void create004(){
ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = act.getBean("factoryCar",Car.class);
System.out.println(car.toString());
}
}