STM8 ADC连续模式通道切换干扰分析与解决方案
2026/7/19 21:35:32
ApplicationContext 是 Spring 框架的核心接口,它是 BeanFactory 的子接口,提供了更丰富的功能。下面从作用、使用方法、注意事项等方面详细介绍。
ApplicationContext 主要负责:
| 实现类 | 适用场景 | 配置方式 |
|---|---|---|
ClassPathXmlApplicationContext | 独立应用,classpath 下的 XML 配置 | XML |
FileSystemXmlApplicationContext | 独立应用,文件系统路径的 XML | XML |
AnnotationConfigApplicationContext | 纯注解配置的独立应用 | Java Config |
GenericApplicationContext | 通用实现,可自定义后处理器 | 多种 |
GenericGroovyApplicationContext | Groovy 脚本配置 | Groovy |
GenericWebApplicationContext | Web 应用(非 Spring Boot) | 多种 |
// 加载 classpath 下的 XML 配置ApplicationContextcontext=newClassPathXmlApplicationContext("applicationContext.xml");// 加载文件系统路径的 XMLApplicationContextcontext=newFileSystemXmlApplicationContext("D:/config/applicationContext.xml");// 获取 BeanUserServiceuserService=context.getBean("userService",UserService.class);// 配置类@Configuration@ComponentScan(basePackages="com.example")@PropertySource("classpath:app.properties")publicclassAppConfig{@BeanpublicDataSourcedataSource(){// 配置数据源returnnewDriverManagerDataSource();}}// 启动容器ApplicationContextcontext=newAnnotationConfigApplicationContext(AppConfig.class);UserServiceuserService=context.getBean(UserService.class);@SpringBootApplicationpublicclassMyApplication{publicstaticvoidmain(String[]args){// 自动创建 ApplicationContextConfigurableApplicationContextcontext=SpringApplication.run(MyApplication.class,args);// 获取 BeanMyServiceservice=context.getBean(MyService.class);}}web.xml 方式(传统):
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>Servlet 3.0+ 注解方式:
publicclassMyWebAppInitializerimplementsWebApplicationInitializer{@OverridepublicvoidonStartup(ServletContextcontainer){AnnotationConfigWebApplicationContextcontext=newAnnotationConfigWebApplicationContext();context.register(AppConfig.class);container.addListener(newContextLoaderListener(context));}}// 获取 classpath 资源Resourceresource=context.getResource("classpath:config.properties");// 获取 URL 资源ResourceurlResource=context.getResource("https://example.com/data.json");// 获取文件系统资源ResourcefileResource=context.getResource("file:/opt/app/config.xml");// 获取消息Stringmessage=context.getMessage("user.welcome",newObject[]{"张三"},Locale.CHINA);// 需要在配置中定义 MessageSource Bean@BeanpublicMessageSourcemessageSource(){ResourceBundleMessageSourcesource=newResourceBundleMessageSource();source.setBasename("messages");returnsource;}// 定义事件publicclassUserRegisterEventextendsApplicationEvent{publicUserRegisterEvent(Objectsource){super(source);}}// 发布事件context.publishEvent(newUserRegisterEvent(user));// 监听事件@ComponentpublicclassUserRegisterListenerimplementsApplicationListener<UserRegisterEvent>{@OverridepublicvoidonApplicationEvent(UserRegisterEventevent){// 处理注册逻辑}}// 错误:多次创建容器publicclassSomeClass{publicvoidmethod1(){newClassPathXmlApplicationContext("beans.xml");// 浪费资源}publicvoidmethod2(){newClassPathXmlApplicationContext("beans.xml");// 又创建一个}}// 正确:单例模式持有容器publicclassApplicationContextHolder{privatestaticfinalApplicationContextcontext=newClassPathXmlApplicationContext("beans.xml");publicstaticApplicationContextgetContext(){returncontext;}}ContextLoaderListener创建根容器// 推荐:使用 ClassPathXmlApplicationContext// 配置文件放在 src/main/resources 下newClassPathXmlApplicationContext("classpath:applicationContext.xml");// 避免硬编码绝对路径// new FileSystemXmlApplicationContext("D:/project/config/beans.xml");// 注意 Bean 的默认作用域是 singleton// 对于 Web 应用,可以使用 request/session 作用域@Scope("prototype")// 多例@Scope("request")// Web 请求级别@Scope("session")// Web 会话级别// 在 Web 应用中,确保容器正确关闭// Spring Boot 会自动处理// 独立应用需要注册关闭钩子((ConfigurableApplicationContext)context).registerShutdownHook();// 大型项目建议拆分配置ApplicationContextcontext=newClassPathXmlApplicationContext("classpath:spring-dao.xml","classpath:spring-service.xml","classpath:spring-mvc.xml");// 或使用 import 标签// <import resource="spring-dao.xml"/>// 单元测试中使用 @ContextConfiguration@RunWith(SpringRunner.class)@ContextConfiguration(classes=AppConfig.class)publicclassMyServiceTest{@AutowiredprivateMyServicemyService;// 测试方法}lazy-init="true"加速启动@ComponentScan指定精确包路径,避免扫描整个 classpath// 使用 try-with-resources 确保关闭try(ConfigurableApplicationContextcontext=newClassPathXmlApplicationContext("beans.xml")){// 使用容器MyServiceservice=context.getBean(MyService.class);service.doSomething();}// 自动关闭@PostConstruct:初始化逻辑不要过于复杂希望这些内容能帮助你全面理解 ApplicationContext!