Spring ApplicationContext

1.概述

详细探索 Spring ApplicationContext接口

2.ApplicationContext接口

Spring 框架的主要特性之一是 IoC(控制反转)容器。Spring IoC 容器负责管理应用程序的对象。它使用依赖注入来实现控制反转。

接口*BeanFactory* 和*ApplicationContext* 代表 Spring IoC 容器。这里,BeanFactory是访问 Spring 容器的根接口。它提供了管理 bean 的基本功能。

另一方面,ApplicationContextBeanFactory的子接口。因此,它提供了BeanFactory 的所有功能。

此外,它还提供了 更多企业特定的功能ApplicationContext的重要特性是解析消息、支持国际化、发布事件和应用层特定的上下文。这就是我们使用它作为默认 Spring 容器的原因。

3.什么是 Spring Bean

在了解ApplicationContext容器之前,了解 Spring bean 很重要。在 Spring 中,beanSpring 容器实例化、组装和管理的对象

那么应该将应用程序的所有对象都配置为 Spring bean 吗?作为最佳实践,不应该这样做。

按照一般的Spring 文档 ,应该为服务层对象、数据访问对象 (DAO)、表示对象、基础设施对象(如 Hibernate SessionFactories、 JMS 队列等)定义 bean。

4.在容器中配置Bean

众所周知,ApplicationContext的主要工作是管理 bean。

因此,应用程序必须向ApplicationContext容器提供 bean 配置。Spring bean 配置由一个或多个 bean 定义组成。此外,Spring 支持不同的配置 bean 的方式。

4.1.基于 Java 的配置

首先,从基于 Java 的配置开始,因为它是最新且最受青睐的 bean 配置方式。它从 Spring 3.0 开始可用。

Java 配置通常使用***@Configuration类中的****@Bean 注释方法**。方法上的*@Bean*注解表明该方法创建了一个 Spring bean。*此外,使用@Configuration注解的类表明它包含 Spring bean 配置。

现在创建一个配置类来将AccountService类定义为 Spring bean:

@Configuration
public class AccountConfig {

  @Bean
  public AccountService accountService() {
    return new AccountService(accountRepository());
  }

  @Bean
  public AccountRepository accountRepository() {
    return new AccountRepository();
  }
}

4.2. 基于注解的配置

Spring 2.5 引入了基于注解的配置作为在 Java 中启用 bean 配置的第一步。

在这种方法中,首先通过 XML 配置启用基于注释的配置。然后在 Java 类、方法、构造函数或字段上使用一组注解来配置 bean。这些注释的一些示例是*@Component*、@Controller@Service@Repository@Autowired和*@Qualifier*

4.3. 基于 XML 的配置

这是在 Spring 中配置 bean 的传统方式。

5.ApplicationContext的类型

Spring 提供了适合不同需求的不同类型的ApplicationContext容器。这些是ApplicationContext接口的实现。那么让我们来看看ApplicationContext的一些常见类型。

5.1.AnnotationConfigApplicationContext

首先,来看一下Spring 3.0 中引入的AnnotationConfigApplicationContext类。 它可以将带有@Configuration、@Component和JSR-330 元数据注释的类作为输入。

使用AnnotationConfigApplicationContext容器和基于 Java 的配置的简单示例:

ApplicationContext context = new AnnotationConfigApplicationContext(AccountConfig.class);
AccountService accountService = context.getBean(AccountService.class);

5.2.AnnotationConfigWebApplicationContext

AnnotationConfigWebApplicationContext 是AnnotationConfigApplicationContext的基于 Web 的变体。

web.xml配置 Spring 的ContextLoaderListener servlet 侦听器或 Spring MVC DispatcherServlet时,可能会使用这个类。

此外,从 Spring 3.0 开始,还可以通过编程方式配置此应用程序上下文容器。需要做的就是实现WebApplicationInitializer接口:

public class MyWebApplicationInitializer implements WebApplicationInitializer {

  public void onStartup(ServletContext container) throws ServletException {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(AccountConfig.class);
    context.setServletContext(container);

    // servlet configuration
  }
}

5.3. XmlWebApplicationContext

在 Web 应用程序中使用基于 XML 的配置,可以使用XmlWebApplicationContext类。

事实上,配置这个容器就像只AnnotationConfigWebApplicationContext类,也就是说可以在web.xml 中配置, 或者实现WebApplicationInitializer接口:

public class MyXmlWebApplicationInitializer implements WebApplicationInitializer {

  public void onStartup(ServletContext container) throws ServletException {
    XmlWebApplicationContext context = new XmlWebApplicationContext();
    context.setConfigLocation("/WEB-INF/spring/applicationContext.xml");
    context.setServletContext(container);

    // Servlet configuration
  }
}

5.4.FileSystemXMLApplicationContext

使用FileSystemXMLApplicationContext 从文件系统或 URL 加载基于 XML 的 Spring 配置文件。当需要以编程方式加载ApplicationContext时,此类很有用。通常,测试工具和独立应用程序是一些可能的用例。

例如 创建这个 Spring 容器并为基于 XML 的配置加载 bean:

String path = "C:/myProject/src/main/resources/applicationcontext/account-bean-config.xml";

ApplicationContext context = new FileSystemXmlApplicationContext(path);
AccountService accountService = context.getBean("accountService", AccountService.class);

5.5.ClassPathXmlApplicationContext

如果想从 classpath 加载 XML 配置文件,可以使用ClassPathXmlApplicationContext类。与*FileSystemXMLApplicationContext 类似,*它对于测试工具以及嵌入在 JAR 中的应用程序上下文很有用。

下面是一个使用这个类的例子:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext/account-bean-config.xml");
AccountService accountService = context.getBean("accountService", AccountService.class);

6.ApplicationContext的附加功能

6.1.消息解析

ApplicationContext接口通过扩展MessageSource接口来支持消息解析**和国际化。此外,Spring 提供了两个*MessageSource实现,ResourceBundleMessageSource和StaticMessageSource

使用StaticMessageSource以编程方式将消息添加到源;但是,它支持基本的国际化,并且更适合测试而不是生产使用。

另一方面,ResourceBundleMessageSource是MessageSource最常见的实现。它依赖于底层 JDK 的ResouceBundle实现。它还使用MessageFormat提供的 JDK 标准消息解析。

下面演示如何使用MessageSource从属性文件中读取消息。

首先,在类路径中创建messages.properties文件

account.name=TestAccount

其次,在AccountConfig类中添加一个 bean 定义:

@Bean
public MessageSource messageSource() {
  ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
  messageSource.setBasename("config/messages");
  return messageSource;
}

第三,将MessageSource注入AccountService

@Autowired
private MessageSource messageSource;

最后,可以在AccountService的任何地方使用getMessage方法来读取消息:

messageSource.getMessage("account.name", null, Locale.ENGLISH);

Spring 还提供了ReloadableResourceBundleMessageSource 类,它允许从任何 Spring 资源位置读取文件,并支持 bundle 属性文件的热重载。

6.2. 事件处理

ApplicationContext在ApplicationEvent类和ApplicationListener接口的帮助下支持事件处理。它支持内置事件,如ContextStartedEventContextStoppedEventContextClosedEvent和RequestHandledEvent。此外,它还支持业务用例的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值