Spring深入浅出(一),基本概念,HelloWorld实例

一、假设你已经搭建好Spring开发环境,包括:

1. Java 开发工具包,参考:Java SE Downloads 
2. Apache Commons Logging API,参考: http://commons.apache.org/logging/
3. MyEclipse2020,参考:https://www.myeclipsecn.com/download/
4. Spring 5.2.3版本,参考:https://repo.spring.io/libs-release-local/org/springframework/spring/5.2.3.RELEASE/spring-5.2.3.RELEASE-dist.zip

并且,至少将下列文件引用到类库(MyEclipse中,Build Path -> Configure Build Path):

commons-logging-1.2.jar
spring-aop-5.2.3.RELEASE.jar
spring-beans-5.2.3.RELEASE.jar
spring-context-5.2.3.RELEASE.jar
spring-core-5.2.3.RELEASE.jar
spring-expression-5.2.3.RELEASE.jar

二、创建HelloWorld程序

1. 创建实体Bean

package com.clzhang.spring.demo;

public class HelloWorld {
    private String message;

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

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

2. 创建调用程序

package com.clzhang.spring.demo;

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

public class MainApp {
    public static void main(String[] args) {
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

        HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
        objA.getMessage();
    }
}

3. 创建配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="helloWorld" class="com.clzhang.spring.demo.HelloWorld" scope="prototype">
        <property name="message" value="Hello World!" />
    </bean>
</beans>

说明:上面配置文件中,scope属性值可以忽略;另外,xmlns:context的注释也非必要。

4. 运行程序MainApp

The Message is:Hello World!

三、解读

IoC 容器是 Spring 的核心,也可以称为 Spring 容器。Spring 提供 2 种不同类型的 IoC 容器,即 BeanFactory 和 ApplicationContext 容器。BeanFactory 和 ApplicationContext 都是通过 XML 配置文件加载 Bean 的。

ApplicationContext 继承了 BeanFactory 接口,由 org.springframework.context.ApplicationContext 接口定义,对象在启动容器时加载。

ApplicationContext 接口有两个常用的实现类,具体如下:

1)ClassPathXmlApplicationContext

该类从类路径 ClassPath 中寻找指定的 XML 配置文件,并完成 ApplicationContext 的实例化工作,具体如下所示:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(String configLocation);

2)FileSystemXmlApplicationContext

该类从指定的文件系统路径中寻找指定的 XML 配置文件,并完成 ApplicationContext 的实例化工作,具体如下所示:

ApplicationContext applicationContext = new FileSystemXmlApplicationContext(String configLocation);

它与 ClassPathXmlApplicationContext 的区别是:在读取 Spring 的配置文件时,FileSystemXmlApplicationContext 不会从类路径中读取配置文件,而是通过参数指定配置文件的位置。即 FileSystemXmlApplicationContext 可以获取类路径之外的资源,如“F:/workspaces/Beans.xml”。

本文参考:

http://c.biancheng.net/spring/ioc.html

https://www.w3cschool.cn/wkspring/dgte1ica.html

原文地址:https://www.cnblogs.com/nayitian/p/14994779.html