Spring的核心容器由哪些模块组成?

目录

一、Spring-core模块

二、Spring-beans模块

三、Spring-context模块

四、Spring-context-support模块

五、Spring-expression模块


Spring的核心容器由哪些模块组成?_第1张图片

 

一、Spring-core模块

Spring-core是Spring框架中的核心模块,提供了许多基础支持和工具类,包括资源管理、依赖注入、AOP、事件处理等功能。在SSM框架中使用Spring-core可以更加方便地实现这些功能。

下面是一个样例代码,演示了如何使用Spring-core进行依赖注入:

// 定义一个服务接口
public interface GreetingService {
    String sayHello();
}

// 实现服务接口
public class GreetingServiceImpl implements GreetingService {
    private String greeting;

    // 注入属性
    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }

    @Override
    public String sayHello() {
        return greeting + ", world!";
    }
}

// 在Spring配置文件中定义bean

    


// 在应用程序中获取bean并使用
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
GreetingService greetingService = (GreetingService) context.getBean("greetingService");
System.out.println(greetingService.sayHello());    // 输出结果:Hello, world!

 

在上面的代码中,我们定义了一个GreetingService接口和一个实现类GreetingServiceImpl,并使用Spring-core提供的依赖注入机制将属性greeting注入到GreetingServiceImpl中。然后,我们在Spring配置文件中定义了一个"greetingService"的bean,并设置了它的greeting属性为"Hello"。最后,在应用程序中获取"greetingService" bean并调用它的sayHello方法输出结果。

Spring的核心容器由哪些模块组成?_第2张图片

二、Spring-beans模块

Spring-beans模块是Spring框架的核心模块之一,它提供了IoC容器的基本实现,即BeanFactory和ApplicationContext。在Spring中,所有的对象都被称为bean,并且由IoC容器来管理和创建它们。

BeanFactory是一个根据配置文件或者注解配置来创建并管理bean的工厂。它主要负责bean的实例化、属性赋值、依赖注入等任务,但是它只有在被调用时才会进行bean的初始化和创建,因此它具有延迟加载的特点。

而ApplicationContext则是BeanFactory的子接口,它在BeanFactory的基础上增加了更多的功能,如国际化支持、事件机制、AOP等。与BeanFactory不同,ApplicationContext在容器启动时就完成了bean的初始化、依赖注入等操作,因此它没有延迟加载的特点。

下面是一个使用Spring-beans模块的样例代码:

// 定义一个JavaBean
public class User {
    private String username;
    private String password;

    // getter和setter方法省略
}

// 创建IoC容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

// 获取User对象
User user = (User)context.getBean("user");

// 调用User对象的方法
user.setUsername("张三");
user.setPassword("123456");

在这个例子中,我们首先定义了一个名为User的JavaBean,然后通过ClassPathXmlApplicationContext创建了一个ApplicationContext实例。接着,我们通过context.getBean方法获取到了名为"uer"的bean,并将其强制类型转换为User对象。最后,我们可以使用User对象的setter方法对它进行初始化和操作。

Spring的核心容器由哪些模块组成?_第3张图片

 

三、Spring-context模块

Spring-context模块是Spring框架的核心模块之一,提供了 Spring 框架必需的基本功能和面向切面编程、IoC 容器、依赖注入等高级特性。它主要包含以下四个方面的功能:

  1. Spring IoC 容器:它是 Spring 框架的核心组件,负责创建对象并维护对象之间的依赖关系。

  2. Spring AOP 框架:面向切面编程(AOP)是 Spring 框架的一个重要特性,Spring-context 中提供了 AOP 相关的支持和实现。

  3. Spring 事件框架:Spring-context 还提供了事件机制,能够让系统中的各个组件之间进行消息传递和通信。

  4. Spring SPI 支持:SPI 即 Service Provider Interface,是一种 Java 标准化的服务发现机制,Spring-context 提供了对 SPI 的支持。

下面是一个简单的 Spring-context 配置文件示例,该文件命名为applicationContext.xml:



    
    
    
    




    




    

这个配置文件主要配置了数据源、MyBatis SqlSessionFactory和MapperScannerConfigurer等,这些都是在 Spring-context 模块中提供的功能。其中,id为dataSource的bean定义了一个基于Apache Commons DBCP的数据库连接池;id为sqlSessionFactory的bean定义了一个MyBatis的SqlSessionFactory实例,它使用了上面定义的dataSource bean;最后,id为mapperScannerConfigurer的bean定义了一个MyBatis Mapper扫描器,指定了扫描的包路径。

四、Spring-context-support模块

Spring-context-support模块是Spring框架的一个支持模块,它提供了一些Context相关的实用工具类和bean定义读取器。这些工具类可以帮助我们更方便地配置和管理Spring应用程序中的bean。

在这个模块中,包括了以下工具类:

  1. PropertySourcesPlaceholderConfigurer:可以用来处理properties属性文件,并将其转化为Spring中的Properties对象。
  2. ReloadableResourceBundleMessageSource:用于国际化处理,能够自动检测实现热加载。
  3. FileSystemXmlApplicationContext:可以用来通过文件系统中的XML文件来装载ApplicationContext。
  4. AnnotationConfigWebApplicationContext:可以用来基于注解的Spring Web应用程序上下文,支持MVC、RESTful和WebSocket应用程序。
  5. MBeanExporter:可以将Spring管理的Bean暴露给JMX服务器进行监控等操作。

下面是一个使用Spring-context-support模块的样例代码:

// 加载Spring的配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");

// 获取PropertySourcesPlaceholderConfigurer Bean
PropertySourcesPlaceholderConfigurer configurer = (PropertySourcesPlaceholderConfigurer)context.getBean("propertySourcesPlaceholderConfigurer");

// 从配置文件中获取db.url和db.username
String url = configurer.getProperty("db.url");
String username = configurer.getProperty("db.username");

// 输出结果
System.out.println("url: " + url);
System.out.println("username: " + username);

在这个例子中,我们首先通过ClassPathXmlApplicationContext加载了Spring的配置文件,然后通过context.getBean方法获取了名为"propertySourcesPlaceholderConfigurer"的bean,并将其强制类型转换为PropertySourcesPlaceholderConfigurer对象。接着,我们使用configurer.getProperty方法从配置文件中获取了db.url和db.username的值,并输出结果。

五、Spring-expression模块

Spring-expression是Spring框架中的一个模块,它提供了一种表达式语言(Expression Language, EL),可以在Spring配置文件中使用类似脚本的语法编写表达式,用于动态地设置bean属性值、调用方法等。

下面是一个示例代码,演示了如何在Spring配置文件中使用Spring-expression模块:



    
    
    




    
    
    
        
        
            
        
    

在上面的代码中,我们使用Spring-expression为一个"person" bean的"name"和"age"属性设置值。通过使用#{...}包裹表达式语言,在表达式中可以使用Java的语法,如字符串字面量、整数字面量、调用方法、访问对象属性等。

另外,在定义一个"userService" bean时,我们使用Spring-expression调用了一个方法,并将它的返回值设置为属性"permissionEvaluator"的值。这里使用了#{...}语法创建一个匿名的DefaultMethodSecurityExpressionHandler bean,并调用它的setPermissionEvaluator方法。Spring-expression还提供了许多其他高级特性,详细介绍可以参考Spring文档。

你可能感兴趣的:(Java学习,spring,java,mybatis)