美文网首页
Spring Hello World

Spring Hello World

作者: 码农UP2U | 来源:发表于2020-07-15 04:44 被阅读0次

今天开始学习 Java 的 Spring 框架,从头开始学,一点点的记录所学的内容,一点点点入门。

Spring 是 Java 的一个框架,Spring 是提供了 IoC 和 AOP 的容器,当然了它并不仅仅是这些内容,只是刚开始入门,可能这两点比较重要和关键吧。

使用 Spring Framework 进行开发,需要使用的核心的几个 Jar 包分别如下:

  • spring-beans
  • spring-context
  • spring-core
  • spring-expression

使用 Spring Framework 这几个 Jar 包需要依赖 common-logging 这个 Jar 包的支持。

现在都是用 Maven 来引入相应的 Jar 包了,但是通过手动引入 Jar 的方式还是需要了解一下。而且,现在的 Spring Boot 已经基本不再需要 xml 的配置,都是直接使用注解进行完成,但是,基本的配置还是需要进行了解的。

下载 Spring Jar 包的地址如下:

Spring Jar 下载地址

下载列表中可以看到有三种类型的压缩文件,分别是:

spring-framework-5.1.7.RELEASE-dist.zip     09-May-2019 14:55  78.92 MB
spring-framework-5.1.7.RELEASE-docs.zip     09-May-2019 14:55  38.62 MB
spring-framework-5.1.7.RELEASE-schema.zip   09-May-2019 14:55  60.81 KB

我们下载 dist.zip 文件即可。将该 zip 文件解压后有很多的文件,我们只需要使用前面提到的几个核心的 Jar 包即可。

common-logging.jar 可以通过 Maven 仓库中的地址去直接下载。

在 IDEA 中新建一个 Java 的项目,然后新建一个 libs 目录,将下载解压后的几个核心 Jar 放入该目录中。

然后打开 Project Settings 窗口,在 Libraries 中将下载的 Jar 添加进入即可。

在 IDEA 创建项目后,会有一个 src 目录,在该目录下创建包 com.test,接着再次在 src 目录下创建一个 applicationContext.xml 文件。

定义一个 Person 类,类定义如下:

package com.test;

public class Person {
    private String name;
    private Integer age;
    private String sex;

    public Person() {
    }

    public Person(String name, Integer age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

然后,将该类配置到 applicationContext.xml 文件中,该文件中的配置如下:

<?xml version="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.xsd">

    <bean id="person" class="com.test.Person"></bean>

</beans>

然后在定一个测试的类,测试类的代码如下:

package com.test;

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

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person)applicationContext.getBean("person");
        System.out.println(person);
    }
}

然后运行该测试类,发现可以打印出 person 的信息。

在整个过程中,我们没有去 new Person 对象,但是我们却得到了 Person 对象,这些工作是由 Spring 框架为我们完成的。

刚开始学习 Spring 框架,慢慢记录!有错误请指正。



微信中搜索 “码农UP2U” 关注我的公众号吧!!!

相关文章

  • 易百教程

    https://www.yiibai.com/spring/spring-3-hello-world-exampl...

  • Spring Hello World

    今天开始学习 Java 的 Spring 框架,从头开始学,一点点的记录所学的内容,一点点点入门。 Spring ...

  • Spring:Hello World

    一、Spring 与 Spring Boot Spring 是什么?简单来说,Spring 就是让 Java 更好...

  • Spring 3 MVC hello world example

    In this tutorial, we show you a Spring 3 MVC hello world ...

  • Spring @cacheable

    1. Hello cache 1.1. spring cache XML配置 1.2. hello world 调...

  • Spring Boot 学习笔记

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spr...

  • 常用markdown语法

    Hello World! Hello World! Hello World! Hello World! Hello...

  • hello

    hello, world hello, world hello, world hello, world

  • Markdown

    标题: hello world hello world hello world hello world hello...

  • 2018-06-11

    markdown hello world hello world hello world hello world ...

网友评论

      本文标题:Spring Hello World

      本文链接:https://www.haomeiwen.com/subject/gexqhktx.html