原文: https://howtodoinjava.com/spring5/core/spring-bean-xml-config/

在此 spring bean XML 配置示例中,学习创建定义和创建 spring bean 以及在任何 spring 应用程序中填充应用程序上下文。 本示例使用 xml 配置定义 bean 。 我们将使用 maven 管理 Spring 依赖项,并使用 Eclipse 构建和运行代码。

1. Spring Maven 依赖

要创建能够创建和管理 bean 的 spring 应用程序上下文,我们至少需要三个 maven 依赖项,即spring-corespring-beansspring-context

  1. spring-core模块具有与其他 Spring 模块一起使用所需的最基本的类。
  2. spring-beans模块提供了与 Spring bean 一起使用所需的org.springframework.beans.factory.BeanFactory接口。
  3. spring-context模块提供org.springframework.context.ApplicationContext接口,该接口可启用其他功能,例如消息资源AOP 功能,特定类型的应用程序上下文和 bean 生命周期事件监听器。

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd;
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.howtodoinjava.spring.webmvc</groupId>
  5. <artifactId>SpringDemos</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>SpringDemos</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <spring.version>5.2.0.RELEASE</spring.version>
  13. </properties>
  14. <dependencies>
  15. <!-- Spring Dependencies -->
  16. <dependency>
  17. <groupId>org.springframework</groupId>
  18. <artifactId>spring-core</artifactId>
  19. <version>${spring.version}</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework</groupId>
  23. <artifactId>spring-beans</artifactId>
  24. <version>${spring.version}</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework</groupId>
  28. <artifactId>spring-context</artifactId>
  29. <version>${spring.version}</version>
  30. </dependency>
  31. </dependencies>
  32. </project>

2. xml 配置中的 Spring bean 定义

2.1. 具有 bean 定义的单个配置文件

您可以在单个 xml 文件中定义所有 SpringBean 及其传递依赖项。 此 xml 文件可用于创建应用程序上下文

beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context.xsd">
  8. <bean id="operations" class="com.howtodoinjava.spring.beans.Operations"></bean>
  9. <bean id="employee" class="com.howtodoinjava.spring.beans.Employee"></bean>
  10. <bean id="department" class="com.howtodoinjava.spring.beans.Department"></bean>
  11. </beans>

2.2. 在多个配置文件中定义 bean 并导入到主文件中

该方法在编写模块化代码时更有用。 您可以在单独的 xml 文件中定义 bean,然后将文件导入主 xml 文件。

employee.xml

  1. <beans>
  2. <bean id="employee" class="com.howtodoinjava.spring.beans.Employee"></bean>
  3. </beans>

department.xml

  1. <beans>
  2. <bean id="department" class="com.howtodoinjava.spring.beans.Department"></bean>
  3. </beans>

beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans>
  3. <import resource="employee.xml"/>
  4. <import resource="department.xml"/>
  5. <bean id="operations" class="com.howtodoinjava.spring.beans.Operations"></bean>
  6. </beans>

3. Spring bean 示例

创建ApplicationContext,我们可以使用它作为可用实现的一种特定实现,例如 ClassPathXmlApplicationContextFileSystemXmlApplicationContextStaticApplicationContextXmlWebApplicationContext等。

我们将需要将 bean 配置文件名称作为使用类的构造函数参数传递。 不要忘记将文件归档在classpath资源文件夹中。

Main.java

  1. import org.springframework.context.ApplicationContext;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. public class XmlConfigExample
  4. {
  5. public static void main( String[] args )
  6. {
  7. @SuppressWarnings("resource")
  8. ApplicationContext ctx = new
  9. ClassPathXmlApplicationContext( "com/howtodoinjava/core/demo/beans/beans.xml" );
  10. Employee employee = ctx.getBean(Employee.class);
  11. Department department = ctx.getBean(Department.class);
  12. Operations operations = ctx.getBean(Operations.class);
  13. System.out.println(department);
  14. System.out.println(employee);
  15. operations.helloWorld();
  16. }
  17. }

程序输出:

Console

  1. Jan 02, 2018 3:10:27 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  2. INFO: Loading XML bean definitions from class path resource [beans.xml]
  3. Jan 02, 2018 3:10:27 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  4. INFO: Loading XML bean definitions from class path resource [employee.xml]
  5. Jan 02, 2018 3:10:27 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  6. INFO: Loading XML bean definitions from class path resource [department.xml]
  7. Employee [id=0, name=null]
  8. Department [id=0, name=null]
  9. Hello World !!

4. 示例中使用的项目结构和其他类

4.1. 项目结构

Spring bean – XML 配置 - 图1

Spring XML Config Project Structure

4.2. Bean

Employee.java

  1. @Getter
  2. @Setter
  3. @ToString
  4. public class Employee
  5. {
  6. private long id;
  7. private String name;
  8. }

Department.java

  1. @Getter
  2. @Setter
  3. @ToString
  4. public class Department
  5. {
  6. private long id;
  7. private String name;
  8. }

Operations.java

  1. public class Operations
  2. {
  3. public void helloWorld(){
  4. System.out.println("Hello World !!");
  5. }
  6. }

在评论部分中,将您与基于 Spring XML 配置基于 spring 容器有关的问题交给我。

学习愉快!

有关:

Spring Java 配置示例
Spring Boot2 配置示例

下载源码