快速入门Spring

目录

为什么要学Spring:

Spring框架诞生的背景:

Spring是什么:

接下来我们就要解决Spring怎么用这个问题

[BeanFactory快速入门 IOC思想的体现](#BeanFactory快速入门 IOC思想的体现)

[BeanFactory快速入门 DI思想的体现](#BeanFactory快速入门 DI思想的体现)


开始学Spring时我们要了解以下几个问题

为什么要学习Spring?

Spring是什么?

Spring怎么用?

Spring在什么时候用?

针对这几个问题我们开始学习Spring

为什么要学Spring:

众所周知Spring是一个框架,那什么是框架呢?

框架是一种结构体,它具备通用性,且能快速实现产品的功能。

例子:建筑框架、写作文的文章结构框架等

Spring框架诞生的背景:

多年前web开发过程中遇到的重大问题。基于三层模型开发时,在业务逻辑的代码中能看到很多的手动new对象的代码,这样会造成高度耦合。

为了解决这个问题,我们就需要学习Spring

Spring是什么:

简单的来讲就是如下图:

我们可以把Spring看成一个工厂,程序在工厂中获取Bean对象。

通过上面的案例,很明显的可以看出来,我们引入了一个很重要的环节,那就是工厂;但是这样就没有问题了吗?

大家仔细思考一下几个问题:

1、工厂怎么知道我要哪个对象呢?或者说我如何告诉工厂我要哪个对象呢?

2、Bean对象之间的关系如何维护呢?比如Abean对象需要用C Bean对象,那么我找工厂要了A Bean对象还要去设置C Bean对象吗?

..............................................................

Spring框架中的几种核心思想出来了!!!!

记住,不管学什么框架,存在即合理,一款框架的出现一定是为了解决某一类问题而产生。所以学某项框架或者技术前一定请熟悉它的技术背景。不要为了写代码而写代码。

接下来我们就要解决Spring怎么用这个问题

第一步:

配置pom.xml:

特别提醒:spring 6.0版本要求jdk的版本为17或以上;如果你的jdk版本是低于17的,请降低spring的版本,以免后面报错(或者升级jdk的版本到17及以上)。我的jdk是1.8的,所以选择降级spring的版本。

复制代码
dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

第二步:编写bean.xml文件

在resource文件中配置bean.xml文件如图:

第三步:编写bean类

复制代码
package com.frank;

public class HelloWorld {
    String message;

    public String getMessage() {
        return message;
    }

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

第四步:编写测试代码来说明spring的使用。

复制代码
package com.frank;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        // 从类路径下加载bean配置文件,得到一个容器对象
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("bean.xml");
        // 使用容器对象里的getBean方法,通过配置文件里面的唯一标识id得到该bean对象
        HelloWorld helloWorld = (HelloWorld) app.getBean("helloWorld");
        // 使用得到的bean对象调用方法,如果能得到值说明对象的确由spring创建好了
        String message = helloWorld.getMessage();
        System.out.println(message);
    }
}

运行程序后,我们会发现,在测试的代码中我们并没有在手动的去通过new创建对象了,而是直接管spring容器对象要;从控制台打印出来的结果来看,的确拿到了message的值,这就说明helloworld对象的确创建成功了,而且把HelloWorld类的message属性给赋了值。

BeanFactory快速入门 IOC思想的体现

IOC思想也叫"控制反转"

讲spring技术背景的时候,利用工厂模式来解决了解耦问题,但是没有用代码来使用说明,这个工厂到底长什么样子,我们还不知道,接下来,我们就一起来使用BeanFacory工厂来实现管理Bean对象的例子。话不多说,上代码。

前面的步骤和第一个spring demo一样,我们一起来回顾一下

第一步:引入spring的jar包(还记得怎么引入的吗?)

第二步:编写bean配置文件(bean.xml还记得里面写什么吗?)

第三步:编写测试类找容器要对象(第三步有点不一样了,我们不找容器要,而是找工厂要)

前面两步的代码都一样,直接看第三步的代码

复制代码
package com.frank;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;

public class MainApp {
    public static void main(String[] args) {
        // 创建bean工厂对象
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        // 创建一个读取配置bean文件的对象
        XmlBeanDefinitionReader definitionReader = new XmlBeanDefinitionReader(beanFactory);
        // 加载配置bean文件并交给bean工厂对象
        definitionReader.loadBeanDefinitions("bean.xml");
        // 根据bean配置文件里面的唯一标识id获取bean对象
        HelloWorld helloWorld = (HelloWorld)beanFactory.getBean("helloWorld");
        String message = helloWorld.getMessage();
        System.out.println(message);
    }
}

BeanFactory快速入门 DI思想的体现

DI也叫依赖注入,这是咋回事呢?

首先我们来还原一个大家都很熟悉的场景,三层模型中service的实现类调用dao层接口里面的方法

原来的做法代码要这么写:

话不多说,上代码展示说明:

复制代码
package service;
import dao.StudentDao;
public class StudentServiceImp {

    StudentDao sd;

    public void updateStudent(){
            sd.update();
    }

    public void setSd(StudentDao sd) {
        this.sd = sd;
    }
}

package dao;
public class StudentDao {
    public void  update(){
        // 操作数据库的代码
    }
}

bean.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="studentService" class="service.StudentServiceImp">
        <property name="sd" ref="studentDao"/>
    </bean>

    <bean id="studentDao" class="dao.StudentDao">
    </bean>

</beans>
相关推荐
weixin_478689769 分钟前
操作系统【2】【内存管理】【虚拟内存】【参考小林code】
数据库·nosql
九皇叔叔1 小时前
【7】PostgreSQL 事务
数据库·postgresql
kk在加油1 小时前
Mysql锁机制与优化实践以及MVCC底层原理剖析
数据库·sql·mysql
合作小小程序员小小店1 小时前
web网页开发,在线%ctf管理%系统,基于html,css,webform,asp.net mvc, sqlserver, mysql
mysql·sqlserver·性能优化·asp.net·mvc
Kookoos1 小时前
ABP VNext + Cosmos DB Change Feed:搭建实时数据变更流服务
数据库·分布式·后端·abp vnext·azure cosmos
JosieBook1 小时前
【Java编程动手学】Java常用工具类
java·python·mysql
hello 早上好2 小时前
MsSql 其他(2)
数据库·mysql
高压锅_12202 小时前
SQLAlchemy数据库连接密码特殊字符处理完全指南
数据库·mysql·django·sqlalchemy
Hello.Reader7 小时前
Redis 延迟监控深度指南
数据库·redis·缓存
ybq195133454317 小时前
Redis-主从复制-分布式系统
java·数据库·redis