在Spring框架里,工厂模式的运用十分广泛,它主要帮助我们创建和管理对象,让对象的创建和使用分离,提高代码的可维护性和可扩展性。下面为你详细介绍Spring框架中工厂模式的具体体现和示例:
1. BeanFactory
作为工厂模式的基础实现
BeanFactory
是Spring框架中最基础的工厂接口,它定义了获取Bean的方法,Spring通过它来创建和管理各种Bean实例。BeanFactory
采用了延迟加载策略,即只有在真正需要某个Bean时才会去创建它。
示例代码
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;// 定义一个简单的Java类作为Bean
class MyBean {public void sayHello() {System.out.println("Hello from MyBean!");}
}public class BeanFactoryExample {public static void main(String[] args) {// 加载Spring配置文件Resource resource = new ClassPathResource("applicationContext.xml");// 创建BeanFactory实例BeanFactory factory = new XmlBeanFactory(resource);// 从工厂中获取Bean实例MyBean myBean = (MyBean) factory.getBean("myBean");// 调用Bean的方法myBean.sayHello();}
}
配置文件 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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 定义一个Bean --><bean id="myBean" class="MyBean"/>
</beans>
代码解释
- 在上述代码中,
BeanFactory
就像一个工厂,根据配置文件applicationContext.xml
中的定义来创建MyBean
实例。我们通过factory.getBean("myBean")
方法从工厂中获取MyBean
实例,而不需要手动去创建它。
2. ApplicationContext
作为高级工厂
ApplicationContext
是 BeanFactory
的子接口,它在 BeanFactory
的基础上提供了更多的功能,如国际化支持、事件发布等。ApplicationContext
采用了预加载策略,即在容器启动时就会创建所有的单例Bean。
示例代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;// 定义一个简单的Java类作为Bean
class MyAnotherBean {public void doSomething() {System.out.println("Doing something in MyAnotherBean!");}
}public class ApplicationContextExample {public static void main(String[] args) {// 加载Spring配置文件并创建ApplicationContext实例ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");// 从ApplicationContext中获取Bean实例MyAnotherBean myAnotherBean = context.getBean("myAnotherBean", MyAnotherBean.class);// 调用Bean的方法myAnotherBean.doSomething();}
}
配置文件 applicationContext2.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 定义一个Bean --><bean id="myAnotherBean" class="MyAnotherBean"/>
</beans>
代码解释
- 这里的
ApplicationContext
同样扮演着工厂的角色,它根据配置文件applicationContext2.xml
创建MyAnotherBean
实例。我们使用context.getBean("myAnotherBean", MyAnotherBean.class)
方法从ApplicationContext
中获取指定类型的MyAnotherBean
实例,然后调用其方法。
3. 工厂方法模式在Spring中的应用
Spring还支持工厂方法模式,允许我们通过自定义的工厂类和工厂方法来创建Bean。
示例代码
// 定义一个产品类
class Product {public void use() {System.out.println("Using the product.");}
}// 定义一个工厂类
class ProductFactory {public Product createProduct() {return new Product();}
}import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class FactoryMethodExample {public static void main(String[] args) {// 加载Spring配置文件并创建ApplicationContext实例ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext3.xml");// 从ApplicationContext中获取Bean实例Product product = context.getBean("product", Product.class);// 调用Bean的方法product.use();}
}
配置文件 applicationContext3.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 定义工厂类 --><bean id="productFactory" class="ProductFactory"/><!-- 使用工厂方法创建Bean --><bean id="product" factory-bean="productFactory" factory-method="createProduct"/>
</beans>
代码解释
- 在这个例子中,
ProductFactory
是一个自定义的工厂类,其中的createProduct
方法是工厂方法,用于创建Product
实例。在Spring配置文件中,我们通过factory-bean
和factory-method
属性指定使用ProductFactory
的createProduct
方法来创建product
Bean。最后,我们从ApplicationContext
中获取product
Bean 并调用其方法。