当前位置: 首页 > news >正文

Spring 框架中哪些接口可以创建对象

Spring 框架中哪些接口可以创建对象

在 Spring 框架中,向 IOC 容器中添加 Bean 主要有以下几种接口和方式。Spring 提供了不同的手段来实现对象的创建和管理,涵盖了不同的需求和场景。以下是几种常用的接口和方式:

1. BeanFactory 接口

BeanFactory 是 Spring 框架中最基本的容器接口,它用于管理和提供 Bean 的实例。在早期版本的 Spring 中,BeanFactory 是容器的核心接口。通过 BeanFactory,你可以获取已注册的 Bean 实例。

常用的实现类:

  • DefaultListableBeanFactory(用于注册 Bean 定义)
示例:
// 基于 XML 配置的 BeanFactory 示例
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class BeanFactoryExample {public static void main(String[] args) {// 加载 Spring 配置文件BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");// 获取并使用 BeanMyBean myBean = (MyBean) factory.getBean("myBean");myBean.sayHello();}
}

xml文件:

<beans><bean id="myBean" class="com.demo.MyBean" />
</beans>

2. ApplicationContext 接口

ApplicationContextBeanFactory 的子接口,除了继承了 BeanFactory 的功能外,还提供了更多的功能,如事件传播、国际化支持、AOP 支持等。ApplicationContext 是实际应用中更常用的接口。

常用的实现类:

  • ClassPathXmlApplicationContext(基于 XML 配置文件的上下文)
  • AnnotationConfigApplicationContext(基于注解配置的上下文)
  • GenericWebApplicationContext(Web 环境的上下文)

ApplicationContext 提供了更多的功能,通常用于大多数 Spring 应用中。

示例:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class ApplicationContextExample {public static void main(String[] args) {// 加载 Spring 配置文件ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");// 获取并使用 BeanMyBean myBean = (MyBean) context.getBean("myBean");myBean.sayHello();}
}

xml文件:

<beans><bean id="myBean" class="com.demo.MyBean" />
</beans>

3. ConfigurableApplicationContext 接口

ConfigurableApplicationContextApplicationContext 的子接口,它提供了更强大的功能,包括关闭容器、刷新容器、启动和停止等操作。在大多数情况下,ApplicationContext 的实现类如 ClassPathXmlApplicationContextAnnotationConfigApplicationContext 都实现了这个接口。

常用的实现类:

  • GenericWebApplicationContext(Web 环境)
  • AnnotationConfigApplicationContext(基于注解配置)
示例:
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class ConfigurableApplicationContextExample {public static void main(String[] args) {// 加载 Spring 配置文件ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");// 获取并使用 BeanMyBean myBean = (MyBean) context.getBean("myBean");myBean.sayHello();// 关闭容器context.close();}
}

4. BeanDefinitionRegistry 接口

BeanDefinitionRegistry 是一个注册 Bean 定义的接口。它用于向 Spring 容器动态注册 Bean 定义。当你希望在运行时动态地向容器添加 Bean 时,BeanDefinitionRegistry 提供了注册功能。

常用的实现类:

  • DefaultListableBeanFactory(通常与 ApplicationContext 一起使用)
  • GenericWebApplicationContext(Web 环境)
示例:
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class BeanDefinitionRegistryExample {public static void main(String[] args) {// 创建一个 BeanFactoryDefaultListableBeanFactory factory = new DefaultListableBeanFactory();// 动态注册 Bean 定义factory.registerBeanDefinition("myBean", BeanDefinitionBuilder.genericBeanDefinition(MyBean.class).getBeanDefinition());// 创建应用上下文AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();context.setBeanFactory(factory);// 获取并使用 BeanMyBean myBean = (MyBean) context.getBean("myBean");myBean.sayHello();}
}

5. BeanFactoryPostProcessor 接口

BeanFactoryPostProcessor 是 Spring 容器启动时,容器创建所有 Bean 实例之前,用来修改 Bean 定义的接口。通过这个接口,你可以访问和修改容器中所有的 Bean 定义,并且可以动态地向容器中添加 Bean 定义。

示例:
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {@Overridepublic void postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory) {// 动态注册一个新的 Bean 定义if (beanFactory instanceof BeanDefinitionRegistry) {BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;registry.registerBeanDefinition("myBean", new org.springframework.beans.factory.support.GenericBeanDefinition(MyBean.class));}}public static void main(String[] args) {// 创建应用上下文并添加 BeanFactoryPostProcessorAnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();context.addBeanFactoryPostProcessor(new CustomBeanFactoryPostProcessor());context.refresh();  // 刷新容器// 获取并使用 BeanMyBean myBean = (MyBean) context.getBean("myBean");myBean.sayHello();// 关闭容器context.close();}
}

6. ApplicationContextAware 接口

ApplicationContextAware 接口允许类访问 ApplicationContext,可以在对象初始化时获取当前的容器上下文。对于需要动态注册 Bean 或者根据应用上下文执行某些操作的场景非常有用。

示例:
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class MyBean implements ApplicationContextAware {private ApplicationContext context;@Overridepublic void setApplicationContext(ApplicationContext context) {this.context = context;}public void sayHello() {System.out.println("Hello from MyBean!");// 你可以在这里动态访问容器,或者做其他操作}public static void main(String[] args) {// 创建应用上下文AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();context.register(MyBean.class);context.refresh();// 获取并使用 BeanMyBean myBean = (MyBean) context.getBean(MyBean.class);myBean.sayHello();// 关闭容器context.close();}
}
http://www.lryc.cn/news/488287.html

相关文章:

  • 豆瓣书摘 | 爬虫 | Python
  • Oracle数据库物理存储结构管理
  • java——Map接口
  • 量子计算机全面解析:技术、应用与未来
  • IDEA相关(包括但不限于快捷键,使用技巧)成长笔记
  • 【再谈设计模式】适配器模式 ~接口兼容的桥梁
  • 使用Cursor和Claude AI打造你的第一个App
  • 粗读Apache Paimon 的基本概念及其组成结构
  • c++调用 c# dll 通过 P/Invoke (详细避坑)
  • 李春葆《数据结构》——图相关代码
  • Linux驱动开发第2步_“物理内存”和“虚拟内存”的映射
  • 告别多品牌乱战,吉利开始觉醒
  • Target-absent Human Attention
  • <QNAP 453D QTS-5.x> 日志记录:在 Docker 中运行的 Flask 应用安装 自签名 SSL 证书 解决 Chrome 等浏览器证书安全
  • 通过huggingface-cli下载Hugging Face上的公开数据集或模型至本地
  • 论文阅读——Intrusion detection systems using longshort‑term memory (LSTM)
  • SparkSQL的执行过程:从源码角度解析逻辑计划、优化计划和物理计划
  • Leetcode打卡:新增道路查询后的最短距离II
  • Spring Web入门练习
  • 计算机毕业设计 | SpringBoot+vue汽车资讯网站 汽车购买咨询管理系统(附源码+论文)
  • stm32下的ADC转换(江科协 HAL版)
  • 解决IntelliJ IDEA的Plugins无法访问Marketplace去下载插件
  • react 如何修改弹出的modal的标题
  • C#中的二维数组的应用:探索物理含义与数据结构的奇妙融合
  • HTML5拖拽API学习 托拽排序和可托拽课程表
  • 内容补充页(相关公式解释)
  • vue中动态渲染静态图片资源
  • 管伊佳ERP,原名华夏ERP,一个简约易上手的国产ERP系统
  • 学习虚幻C++开发日志——委托(持续更新中)
  • 开窗函数 - first_value/last_value