Spring IOC容器XML配置示例

x33g5p2x  于2022-10-06 转载在 Spring  
字(3.8k)|赞(0)|评价(0)|浏览(393)

在之前的文章中,我们已经讨论了什么是Spring IOC容器以及它是如何工作的,现在在这篇文章中,我们将讨论一个简单的例子来展示Spring IOC容器与基于XML的配置元数据?

Spring IOC容器负责实例化、配置和组装Spring Bean。容器通过读取配置元数据来获得对哪些对象进行实例化、配置和组装的指示。配置元数据用XML、Java注释或Java代码表示。它可以让你表达构成你的应用程序的对象,以及这些对象之间丰富的相互依赖关系
我们可以通过三种方式向Spring IoC容器提供配置元数据

  1. 基于XML的配置
  2. 基于注解的配置
  3. **基于Java的配置 **

在这个例子中,我们将向Spring IoC容器提供基于XML的配置元数据。

Spring IOC容器XML配置实例

Spring应用程序的开发步骤

按照以下三个步骤来开发一个Spring应用程序。

  1. 创建一个简单的Maven项目
  2. 添加Maven依赖项
  3. 配置HellowWorld Spring Bean
  4. 创建一个Spring容器
  5. 从Spring容器中检索Bean

使用的工具和技术

  • Spring框架 - 5.1.0.RELEASE
  • JDK - 8或更高版本
  • Maven - 3.2以上
  • IDE - Eclipse Mars/STS

1. 创建一个简单的Maven项目

使用你喜欢的IDE创建一个简单的Maven项目,打包结构参考下图。如果你是maven新手,请阅读本文《如何创建一个简单的Maven项目》。

项目结构

下图显示了项目结构,供您参考。 

2. 添加Maven依赖项

<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.javaguides.spring</groupId>
    <artifactId>spring-ioc-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

3. 配置HelloWorld Spring Bean

什么是Spring Bean?

这是一个非常简单的问题,但往往被过度复杂化。通常情况下,Spring Bean是由Spring容器管理的Java对象。

下面是一个HelloWorld Spring Bean。

package net.javaguides.spring.ioc;

public class HelloWorld {
    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public void getMessage() {
        System.out.println("My Message : " + message);
    }
}

配置元数据 - 配置HelloWorld Spring Bean

<?xmlversion = "1.0"encoding = "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
   
 <bean id="helloWorld" class="net.javaguides.spring.ioc.HelloWorld">
  <property name="message" value="Hello World!" />
 </bean>
</beans>

4. 创建一个Spring容器

如果我们在一个独立的应用程序中拥有Spring Bean配置的XML文件,那么我们可以使用ClassPathXmlApplicationContext类来加载该文件并获得容器对象。

package net.javaguides.spring.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }
}

5. 从Spring容器中获取Bean

ApplicationContext接口提供getBean()方法来从Spring容器中检索Bean。

package net.javaguides.spring.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
    }
}

输出

My Message : Hello World!

这个例子的源代码可以在我的GitHub仓库https://github.com/RameshMF/spring-core-tutorial中找到。

相关文章