Spring中的Bean配置 (1)——内容提要
Spring中的Bean配置 (2)—— IOC和DI
Spring中的Bean配置 (3)——在Spring的IOC容器里配置Bean(通过全类名(反射))——基于XML文件的方式
Spring中的Bean配置 (4)——依赖注入方式
Spring中的Bean配置 (5)——字面值
Spring中的Bean配置 (6)——引用其他Bean
Spring中的Bean配置 (7)——注入参数详解:null值和级联属性
Spring中的Bean配置 (8)—— 集合属性
Spring中的Bean配置 (9)—— XML 配置里的 Bean自动装配
Spring中的Bean配置 (10)—— 继承 Bean 配置和依赖 Bean 配置
Spring中的Bean配置 (11)——Bean的作用域
Spring中的Bean配置 (12)——使用外部属性文件
Spring中的Bean配置 (13)—— Spring表达式语言:SpEl
Spring中的Bean配置 (14)——IOC容器中Bean的生命周期
Spring中的Bean配置 (15)——在Spring的IOC容器里配置Bean(通过工厂方法创建Bean)——基于XML文件的方式
Spring中的Bean配置 (16)——在Spring的IOC容器里配置Bean(通过FactoryBean)——基于XML文件的方式
Spring中的Bean配置 (17)——在Spring的IOC容器里配置Bean——基于注解的方式来配置Bean
工厂方法有两种:1;静态工厂方法 2;实例工厂方法
如下一一介绍
1:通过调用静态工厂方法创建 Bean
- 调用静态工厂方法创建 Bean是将对象创建的过程封装到静态方法中. 当客户端需要对象时, 只需要简单地调用静态方法, 而不同关心创建对象的细节.
- 要声明通过静态方法创建的 Bean, 需要在 Bean 的 class 属性里指定拥有该工厂的方法的类, 同时在 factory-method 属性里指定工厂方法的名称. 最后, 使用 元素为该方法传递方法参数.
代码示例:
Car_StaticFactory.java
package com.atguigu.spring.bean;
import java.util.HashMap;
import java.util.Map;
/**
* .静态工厂方法:直接调用某一个类的静态方法就可以返回bean的实例
*/
public class Car_StaticFactory {
private static Map<String, Car> cars =new HashMap<String,Car>();
static {
cars.put("benchi", new Car("benchi",80000));
cars.put("baoma", new Car("baoma",80000));
}
//静态工厂方法
public static Car getCar(String name) {
return cars.get(name);
}
}
Car.java
package com.atguigu.spring.bean;
public class Car {
private String brand;
private double price;
public void setBrand(String brand) {
this.brand = brand;
}
public void setPrice(Double price) {
this.price = price;
}
public Car(String brand, double price) {
super();
this.brand = brand;
this.price = price;
System.out.println("调用构造函数");
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
application.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,注意而不是配置静态工厂方法实例,而是配置bean实例 -->
<!--
class属性:指向静态工厂方法全类名
factory-method:指向静态工厂方法的名字
constructor-arg:如果工厂方法需要传入参数,则使用constructor-arg来配置参数
-->
<bean id="car1" class="com.atguigu.spring.bean.Car_StaticFactory"
factory-method="getCar" >
<constructor-arg value="benchi"></constructor-arg>
</bean>
</beans>
Main.java
package com.atguigu.spring.bean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ConfigurableApplicationContext configurableApplicationContext=new ClassPathXmlApplicationContext("application.xml");
Car car1=(Car) configurableApplicationContext.getBean("car1");
System.out.println(car1);
configurableApplicationContext.close();
}
}
2:通过调用实例工厂方法创建 Bean
- 实例工厂方法: 将对象的创建过程封装到另外一个对象实例的方法里. 当客户端需要请求对象时, 只需要简单的调用该实例方法而不需要关心对象的创建细节.
- 要声明通过实例工厂方法创建的 Bean
—在 bean 的 factory-bean 属性里指定拥有该工厂方法的 Bean
—在 factory-method 属性里指定该工厂方法的名称
—使用 construtor-arg 元素为工厂方法传递方法参数
代码演示
Car_Instance
package com.atguigu.spring.bean;
import java.util.HashMap;
import java.util.Map;
/**
* .实例工厂方法,即需要创建工厂本身,在调用工厂的实例方法返回bean的实例
*/
public class Car_Instance {
private static Map<String, Car> cars =new HashMap<String,Car>();
public Car_Instance() {
cars.put("benchi", new Car("benchi", 8000000));
cars.put("baoma", new Car("baoam", 4000000));
}
public Car getCar(String brand) {
return cars.get(brand);
}
}
Car.java
package com.atguigu.spring.bean;
public class Car {
private String brand;
private double price;
public void setBrand(String brand) {
this.brand = brand;
}
public void setPrice(Double price) {
this.price = price;
}
public Car(String brand, double price) {
super();
this.brand = brand;
this.price = price;
System.out.println("调用构造函数");
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
application.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="instancefactory" class="com.atguigu.spring.bean.Car_Instance"></bean>
<!-- 通过实例工厂方法来配置bean -->
<!--
factory-bean属性:指向实例工厂方法的bean
factory-method:指向实例工厂方法的名字
constructor-arg:如果工厂方法需要传入参数,则使用它来配置
-->
<bean id="car2" factory-bean="instancefactory" factory-method="getCar" >
<constructor-arg value="benchi"></constructor-arg>
</bean>
</beans>
Main.java
package com.atguigu.spring.bean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ConfigurableApplicationContext configurableApplicationContext=new ClassPathXmlApplicationContext("application.xml");
Car car2=(Car) configurableApplicationContext.getBean("car2");
System.out.println(car2);
configurableApplicationContext.close();
}
}