Spring,搭建Spring环境

2023-05-16

控制反转:控制了对象的创建,反转:反转的是获取对象的方式,从自己创建对象变为由Spring工厂推送

1. 搭建Spring环境,导入依赖

  • spring-aop:开发AOP特性时需要的JAR 
  • spring-beans:处理Bean的jar
  • spring-context:处理spring上下文的jar
  • spring-core.jar:spring核心jar
  • spring-expression:spring表达式

还要导入:

  • mysql-connection-java
  • druid
  • mybatis
  • mybatis-spring
  • log4j

 2. application-Context.xml配置文件

先导入模板

<?xml version="1.0" encoding="utf8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
                           http://www.springframework.org/schema/p
                           http://www.springframework.org/schema/p/spring-p.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd "
>


</beans>

3. 详解Spring原理

新建Student类

public class Student {
    private Integer stuno;

    private String stuname;

    private Integer stuage;

    public Integer getStuno() {
        return stuno;
    }

    public void setStuno(Integer stuno) {
        this.stuno = stuno;
    }

    public String getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname == null ? null : stuname.trim();
    }

    public Integer getStuage() {
        return stuage;
    }

    public void setStuage(Integer stuage) {
        this.stuage = stuage;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuno=" + stuno +
                ", stuname='" + stuname + '\'' +
                ", stuage=" + stuage +
                '}';
    }

}

写个测试类test.java

来看以前的做法:先新建对象,在通过set()属性赋值

再来用IOC方法

                1. 在application-context.xml文件中写bean,通过property标签设置属性

                

                2. 回到test测试类

现在怎么创建student对象?

                第一步:获取Spring上下文对象context

                第二步:通过 context.getBean()获取刚刚在Spring配置文件application-context.xml文件中的bean(id="Student")

         

对象的创建和对象的赋值全部交给IOC容器 

 下面的代码,执行从springIOC容器中获取一个id为student的bean对象,该对象的类型为对应bean标签class的值

Student student = (Student)context.getBean("student");

理解工厂的概念:

创建一个ICourse接口:课程

public interface ICourse {
    public void learn();
}

不同课程例如JavaCourse、MybatisCourse实现这个接口

public class JavaCourse implements ICourse {
    @Override
    public void learn() {
        System.out.println("learning Java...");
    }
}
public class MybatisCourse implements ICourse {
    @Override
    public void learn() {
        System.out.println("learning Mybatis...");
    }
}

学生可以学习课程:为Student类添加方法 learnJava()、learnMybatis()

   public void learnJava(){
        ICourse iCourse = new JavaCourse();
        iCourse.learn();
    }
    public void learnMybatis(){
        ICourse iCourse = new MybatisCourse();
        iCourse.learn();
    }

测试一下:

 上面是我们原始的开发方式

每次需要课程都要在Student类里面写一个learnXxx(),

如果要学习20个课程,就要20个方法,且每个方法里面都要创建一个课程的实现类对象

 这种方式创建对象new非常零散,造成后期维护较为麻烦

创建工厂,由工厂统一的创建对象

 根据名称创建课程对象

public class CourseFactory {
    public static ICourse getCourse(String name){
        if(name.equalsIgnoreCase("java")){
            return new JavaCourse();
        }else {
            return new MybatisCourse();
        }
    }
}

把Student中的learnJava()、learnMybatis()方法统一写在learn方法中,并给形参String name,并把name交给工厂CourseFactory来创建对象 

 测试:

 现在要学习不同课程只需要改参数值就可以了,通过简单的工厂,可以将创建课程new 集中起来操作,方便后期维护

所以SpringIOC容器就是一个工厂,不需要我们去写CourseFactory,由SpringIOC容器为我们创建

我们只需要通过调用

为了更方便理解控制反转,我们也可以把这种方式叫做依赖注入

在spring配置文件中把Course的两个实现类设置bean

 如下代码:在需要创建JavaCourse对象的地方通过spring上下文context调用getBean()方法拿对象,不需要自己创建

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

public class CourseFactory {
    public static ICourse getCourse(String name) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        if (name.equalsIgnoreCase("java")) {
            return (ICourse) context.getBean("javaCourse");
        } else {
            return (ICourse) context.getBean("mybatisCourse");
        }
    }
}

对比之前自己new对象

public class CourseFactory {
    public static ICourse getCourse(String name){
        if(name.equalsIgnoreCase("java")){
            return new JavaCourse();
        }else {
            return new MybatisCourse();
        }
    }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring,搭建Spring环境 的相关文章

随机推荐

  • 背包九讲PDF

    本资料仅限个人学习交流使用 xff0c 不得用于商业用途 背包九讲PDF xff1a https pan baidu com s 17rTxMwCo9iSTOW77yucdXQ 提取码 xff1a xbqa 转载于 https www cn
  • electron在Windows、Linux和KYLIN操作系统下的不同表现

    一 electron简介 Electron 是一个由 Github 开发 用 HTML xff0c CSS 和 JavaScript 来构建跨平台桌面应用程序的框架 xff0c 然后这些应用程序可以打包在macOS Windows和Linu
  • vscode 终端美化

    1 进入网站 Base16 Terminal Colors for Visual Studio Code 2 选择自己喜欢的主题 点击Copy to clipboard 3 打开vscode 设置 输入setting 在 settings
  • kali美化与配置

    kali linux简单美化 前言 xff1a kali linux是一个神奇的系统 xff0c 里面含有大量的工具 xff08 虽然python很容易就可以做出来 xff09 xff0c 但是像msf这样的大作还是很有参考价值的 好不容易
  • ubuntu开机进入循环登录状态的解决方案

    ubuntu进入循环登陆的解决方案 1 出现问题的原因2 解决问题的方法2 1 登陆进入CLI2 2 检查 etc environment环境变量 1 出现问题的原因 我出现的这个问题的原因是由于修改系统的环境变量 etc profile
  • Tomcat 7.X安装教程(简单易懂)

    Tomcat 7 X安装教程 简单易懂 步骤1 下载Tomcat7 x版本 官网7 x下载地址 https tomcat apache org download 70 cgi https aoian lanzous com iCWyymcn
  • 社区医疗管理系统(JDBC+eclipse)

    提示 xff1a 文章写完后 xff0c 目录可以自动生成 xff0c 如何生成可参考右边的帮助文档 文章目录 前言一 项目预览1 登陆界面2 首页3 修改密码和查看个人信息4 用户管理5 居民信息 二 导入步骤1 下载文件2 数据库导入3
  • linux shell 常用命令

    linux shell 终端操作命令 shutdown 默认1分钟内关机 43 n表示n分钟后关机 输入后可以打shutdown c 进行取消 shutdown h now表示立即关机sudo 在命令前书写 xff0c 表示已管理员的权限运
  • Shell脚本——kafka集群启停

    部署kafka集群的服务器名称为 xff1a node101 node102 node103 批量启动 停止zookeeper xff0c 查看zookeeper开启状态 在 opt module zookeeper bin 目录中 xff
  • Shell脚本——批量关闭服务器

    服务器名称为 xff1a node101 node105 node110 node113 bin bash array 61 102 103 104 105 110 113 101 xff09 for i 61 0 i lt array i
  • 设置文件夹共享

    本文使用共享文件夹名称为 xff1a test 1 右键文件夹 xff0c 点击 属性 xff0c 菜单栏选择 共享 xff0c 点击下面的 共享 S 按钮 2 选择要共享的用户 xff08 此处选择 Everyone xff0c 可以根据
  • Ubuntu16.04的图形化界面无法启动问题

    昨晚在 Ubuntu 下试图安装笔记本触控板的驱动的时候 xff0c 突然 Ubuntu 的图形化界面不见了 xff0c 尝试了 Ctrl 43 Alt 43 F1 F2 F3 无果 xff0c 又在一些博客的指导下尝试在命令行使用 sta
  • Shell脚本——设置ssh免密

    功能 xff1a 实现多台linux主机之间root用户的免密设置 主机名 xff1a node101 node106 在node101上切换至root用户 xff0c 编写以下脚本内容 xff1a bin bash 使用root用户 在n
  • 解析ET6接入ILRuntime实现热更

    1 介绍 ILRuntime项目为基于C 的平台 xff08 例如Unity xff09 提供了一个纯C 实现 xff0c 快速 方便且可靠的IL运行时 xff0c 使得能够在不支持JIT的硬件环境 xff08 如iOS xff09 能够实
  • Python 实现用网页展示多个结果表数据

    Python 实现用网页展示多个表格的数据 前言 一 效果图 二 代码 1 引入库 2 函数定义 3 主程序 前言 实现方法是利用pandas to html 与表格展示的美化相结合 使数据展示更美观 一 效果图 示例 二 代码 本文将使用
  • error while loading shared libraries

    问题描述 我在调试配置一个Linux计算环境的程序时候 xff0c 安装配置好相关的库 xff0c 但是在执行运行程序命令时候报错如下 xff1a error span class token keyword while span load
  • 要求在数组中间删除一个数字

    span class token comment 要求在数组中间删除一个数字 span span class token keyword var span arr span class token operator 61 span span
  • Ubuntu常见问题及解决办法

    在刚开始使用Ubuntu系统时 xff0c 总会遇到各种各样的小问题 xff0c 这里整理了一些遇到的问题及解决办法 xff0c 不断更新中 xff01 xff01 xff01 目录 一 创建文件夹权限不够 1 1 问题描述 1 2 解决办
  • 计算机网络考试题库

    第1章 计算机网络概论 1 xff0e 在20世纪50年代 xff0c 和 xff08 xff09 技术的互相结合 xff0c 为计算机网络的产生奠定了理论基础 2 xff0e 从传输范围的角度来划分计算机网络 xff0c 计算机网络可以分
  • Spring,搭建Spring环境

    控制反转 xff1a 控制了对象的创建 xff0c 反转 xff1a 反转的是获取对象的方式 xff0c 从自己创建对象变为由Spring工厂推送 1 搭建Spring环境 xff0c 导入依赖 spring aop xff1a 开发AOP