1. Bean 的创建和管理
- 实例化:BeanFactory 根据配置信息(如 XML 文件、Java 注解等)来创建 Bean 的实例。它支持多种实例化方式,包括默认构造函数、静态工厂方法和实例工厂方法。例如,对于一个简单的 Java 类 UserService,BeanFactory 可以通过调用其无参构造函数来创建实例。
public class UserService {
public UserService() {
System.out.println("UserService 实例化");
}
}
- 配置管理:可以对 Bean 的属性进行配置。比如在 XML 配置中,可以通过
标签为 Bean 的属性赋值,或者使用注解进行属性注入。
2. 依赖注入
- 构造函数注入:BeanFactory 可以通过构造函数将依赖的 Bean 注入到目标 Bean 中。例如,OrderService 依赖于 UserService,可以通过构造函数注入。
public class OrderService {
private UserService userService;
public OrderService(UserService userService) {
this.userService = userService;
}
}
xml
- 属性注入:通过设置 Bean 的属性来完成依赖注入。
public class PaymentService {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
}
xml
3. Bean 的生命周期管理
- 实例化:创建 Bean 的实例对象。
- 属性赋值:根据配置信息为 Bean 的属性赋值。
- 初始化:在 Bean 实例化和属性赋值完成后,会调用初始化方法。可以通过实现 InitializingBean 接口的 afterPropertiesSet() 方法或者在 XML 配置中指定 init-method 属性来定义初始化方法。
import org.springframework.beans.factory.InitializingBean;
public class ProductService implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("ProductService 初始化");
}
}
xml
- 销毁:在 Bean 不再使用时,会调用销毁方法。可以通过实现 DisposableBean 接口的 destroy() 方法或者在 XML 配置中指定 destroy-method 属性来定义销毁方法。
import org.springframework.beans.factory.DisposableBean;
public class CategoryService implements DisposableBean {
@Override
public void destroy() throws Exception {
System.out.println("CategoryService 销毁");
}
}
xml
4. 提供 Bean 的访问接口
- 通过 getBean() 方法可以根据 Bean 的名称或类型来获取 Bean 实例。
BeanFactory beanFactory = ...;
UserService userService = (UserService) beanFactory.getBean("userService");
主要子类及特点
1. ApplicationContext
- 功能扩展:它是 BeanFactory 的子接口,在 BeanFactory 的基础上扩展了许多功能。国际化支持:可以方便地实现多语言支持,通过 MessageSource 接口来管理国际化消息。资源加载:可以从不同的资源位置(如文件系统、类路径等)加载资源,通过 ResourceLoader 接口实现。事件发布:支持事件驱动编程,通过 ApplicationEventPublisher 接口发布和处理事件。
- 常用实现类:ClassPathXmlApplicationContext:从类路径下的 XML 配置文件中加载 Bean 定义。
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
- FileSystemXmlApplicationContext:从文件系统中的 XML 配置文件中加载 Bean 定义。
ApplicationContext context = new FileSystemXmlApplicationContext("C:/path/to/applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
- AnnotationConfigApplicationContext:基于 Java 注解配置的应用上下文,用于扫描带有注解的类来定义 Bean。
@Configuration
@ComponentScan("com.example")
public class AppConfig {
// 配置类
}
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = context.getBean(UserService.class);
2. XmlBeanFactory
- 功能特点:主要用于从 XML 配置文件中加载 Bean 定义并创建 Bean 实例。它是早期 Spring 版本中常用的 BeanFactory 实现类,但在 Spring 3.1 及以后的版本中已被标记为过时,建议使用 ApplicationContext 实现类。
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;
public class XmlBeanFactoryExample {
public static void main(String[] args) {
Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
UserService userService = (UserService) beanFactory.getBean("userService");
}
}
3. DefaultListableBeanFactory
- 功能特点:是 Spring 框架中最常用的 BeanFactory 实现类之一,它提供了对 Bean 定义的注册、管理和查询等功能,支持通过各种方式(如 XML、注解等)定义的 Bean。
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class DefaultListableBeanFactoryExample {
public static void main(String[] args) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
Resource resource = new ClassPathResource("applicationContext.xml");
reader.loadBeanDefinitions(resource);
UserService userService = (UserService) beanFactory.getBean("userService");
}
}
使用场景
1. 企业级应用开发
- 在大型企业级应用中,通常会有多个模块和组件,如用户管理模块、订单管理模块、支付模块等。BeanFactory 可以将这些模块中的组件以 Bean 的形式进行管理,通过依赖注入实现组件之间的解耦。例如,订单管理模块中的 OrderService 依赖于用户管理模块中的 UserService,可以通过 BeanFactory 进行依赖注入,使得两个模块可以独立开发和测试。
2. 分层架构中的应用
- 在典型的三层架构(表现层、业务逻辑层、数据访问层)中,各层之间存在依赖关系。BeanFactory 可以将各层的组件进行管理,实现层与层之间的解耦。例如,表现层的控制器依赖于业务逻辑层的服务,业务逻辑层的服务依赖于数据访问层的 DAO,通过 BeanFactory 进行依赖注入,使得各层可以独立变化,提高了系统的可维护性和可扩展性。
3. 插件化和模块化开发
- 在插件化或模块化开发中,每个插件或模块都有自己的功能和组件。BeanFactory 可以为每个插件或模块提供独立的 Bean 管理环境,方便实现插件和模块的动态加载和卸载。例如,一个电商系统中可以有营销插件、物流插件等,每个插件都有自己的 Bean 定义,通过 BeanFactory 可以对这些插件的 Bean 进行独立管理。
使用方法
1. XML 配置方式
- 定义 Bean 类:创建需要管理的 Java 类。
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- 编写 XML 配置文件:在 applicationContext.xml 中定义 Bean。
- 获取 Bean 实例:使用 ClassPathXmlApplicationContext 来加载配置文件并获取 Bean 实例。
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class XmlConfigExample {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
System.out.println(user.getName());
}
}
2. Java 注解方式
- 定义 Bean 类:使用 @Component 或其派生注解(如 @Service、@Repository、@Controller)来标记需要管理的类。
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void sayHello() {
System.out.println("Hello!");
}
}
- 配置类:使用 @Configuration 和 @ComponentScan 注解来配置应用上下文。
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example")
public class AppConfig {
// 配置类
}
- 获取 Bean 实例:使用 AnnotationConfigApplicationContext 来加载配置类并获取 Bean 实例。
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AnnotationConfigExample {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = context.getBean(UserService.class);
userService.sayHello();
}
}
3. 编程式配置方式
- 使用 DefaultListableBeanFactory 手动注册 Bean 定义。
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
public class ProgrammaticConfigExample {
public static void main(String[] args) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
// 定义 Bean 定义
BeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(User.class)
.addPropertyValue("name", "Alice")
.getBeanDefinition();
// 注册 Bean 定义
beanFactory.registerBeanDefinition("user", beanDefinition);
// 获取 Bean 实例
User user = (User) beanFactory.getBean("user");
System.out.println(user.getName());
}
}