spring ApplicationContext 继承的这些接口作用,示例,表格对比
各接口作用及代码示例
1. EnvironmentCapable
作用:提供对运行环境的访问,如系统属性、配置属性等。
// 配置类
@Configuration
public class AppConfig {
@Value("${app.message:Hello World}") // 默认值为Hello World
private String message;
}
// 使用示例
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Environment environment = context.getEnvironment();
String appMessage = environment.getProperty("app.message"); // 获取配置属性
System.out.println("App Message: " " + appMessage); // 输出:Hello World
2. ListableBeanFactory
作用:通过名称或类型列举/查找Bean。
// 配置类
@Configuration
public class AppConfig {
@Bean
public Service service1() { return new Service("Service1"); }
@Bean
public Service service2() { return new Service("Service2"); }
}
// 使用示例
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
Map<String, Service> services = context.getBeansOfType(Service.class);
System.out.println("Services: " + services.size()); // 输出:2
3. HierarchicalBeanFactory
作用:支持分层配置,允许子工厂覆盖父级属性。
// 配置父工厂
GenericApplicationContext parent = new GenericApplicationContext();
parent.refresh();
// 配置子工厂并设置父级
GenericApplicationContext child = new GenericApplicationContext(parent);
child.getBeanFactory().registerSingleton("overrideBean", new OverrideBean());
child.refresh();
// 获取子工厂中的Bean(优先使用子级配置)
OverrideBean bean = child.getBean(OverrideBean.class); // 使用子级的配置
4. MessageSource
作用:国际化资源管理,获取本地化消息。
// 配置类
@Configuration
public class AppConfig {
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasename("messages"); // 国际化文件名
return source;
}
}
// 使用示例
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
String greeting = context.getMessage("greeting", null, Locale.US); // 从messages.properties获取
System.out.println("Greeting: " + greeting); // 输出:Hello!
5. ApplicationEventPublisher
作用:发布应用事件,支持事件监听机制。
// 自定义事件
public class CustomEvent extends ApplicationEvent {
public CustomEvent(Object source, String data) {
super(source);
this.data = data;
}
private String data;
}
// 监听器
@Component
public class EventListener implements ApplicationListener<CustomEvent> {
@Override
public void onApplicationEvent(CustomEvent event) {
System.out.println("Received event: " + event.getData());
}
}
// 发布事件
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
context.publishEvent(new CustomEvent(this, "Event occurred!")); // 触发监听器
6. ResourcePatternResolver
作用:通过通配符解析资源路径。
// 使用示例
ApplicationContext context = new ClassPathXmlApplicationContext();
Resource[] resources = context.getResources("classpath*:/*.properties");
for (Resource resource : resources) {
System.out.println("Found resource: " + resource.getFilename()); // 输出所有匹配的文件名
}
接口功能对比表格
接口名称 | 核心功能 | 关键方法 | 典型使用场景 |
---|---|---|---|
EnvironmentCapable | 访问系统/配置环境属性 | getEnvironment() | 读取配置文件或系统属性 |
ListableBeanFactory | 按名称/类型查找Bean | getBeansOfType() | 动态获取所有实现类Bean |
HierarchicalBeanFactory | 支持分层配置覆盖 | setParentBeanFactory() | 多环境配置继承与覆盖 |
MessageSource | 国际化消息解析 | getMessage() | 多语言提示信息管理 |
ApplicationEventPublisher | 发布应用事件 | publishEvent() | 实现事件驱动架构 |
ResourcePatternResolver | 通配符资源路径解析 | getResources() | 批量加载配置文件或静态资源 |
总结
- EnvironmentCapable:环境属性访问,如读取
application.properties
。 - ListableBeanFactory:动态查找Bean,适合需要按类型聚合的场景。
- HierarchicalBeanFactory:分层配置,适用于多环境部署。
- MessageSource:国际化支持,如多语言应用。
- ApplicationEventPublisher:事件驱动,解耦组件间通信。
- ResourcePatternResolver:批量加载资源,如扫描所有配置文件。