IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Spring之(一)初识Spring -> 正文阅读

[Java知识库]Spring之(一)初识Spring

一、初识Spring

1.1 Spring简介

  • Spring:Spring框架由Rod Johnson开发。Spring是一个从实际开发中抽取出来的框架,因此它完成了大量开发中的通用步骤,留给开发者的仅仅是与特定应用相关的部分,从而大大提高了企业应用的开发效率。
  • Spring的优点:
    • 低侵入式设计,代码的污染极低。
    • 独立于各种应用服务器,基于Spring框架的应用,可以真正实现Write Once,Run Anywhere的承诺。
    • Spring的IoC容器降低了业务对象替换的复杂性,提高了组件之间的解耦。
    • Spring的AOP支持允许将一些通用任务如安全、事务、日志等进行集中式管理,从而提供了更好的复用。
    • Spring的ORM和DAO提供了与第三方持久层框架的良好整合,并简化了底层的数据库访问。
    • Spring的高度开放性,并不强制应用完全依赖于Spring,开发者可自由选用Spring框架的部分或全部。

1.2 Spring框架的组成结构

在这里插入图片描述

1.3 Spring的核心机制

程序中主要是用过Spring容器来访问容器中的Bean,ApplicationContext是Spring容器中最常用的接口,该接口有两个实现类:

  • ClassPathXmlApplicationContext:从类加载路径下搜索配置文件,并根据文件来创建Spring容器;

  • FileSystemXmlApplicationContext:从文件系统的相对路径或绝对路径下去搜索配置文件,并根据配置文件来创建Spring容器。

    public class test {
        public static void main(String[] args) {
        	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("src/applicationContext.xml");
            Human human = (Human) applicationContext.getBean("am");
            human.eat();
            human.speak();
    
            ApplicationContext applicationContex1t = new FileSystemXmlApplicationContext("src/applicationContext.xml");
            Human human1 = (Human) applicationContext1.getBean("ch");
            human1.eat();
            human1.speak();
        }
    }
    

也可以通过Bean工厂的方式来访问容器中的Bean:

public class test2 {
    public static void main(String[] args) {
        BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("src/applicationContext.xml"));
        Human human = (Human) beanFactory.getBean("am");
        human.eat();
        human.speak();
    }
}

1.4 为什么要使用Spring?

一直有一个接口Human,在此接口中定义两个方法:void eat();和void walk();,再利用对其的实例化来实现接口中的方法:

//Human接口:
public interface Human {
    void eat();
    void walk();
}


//中国人类实现Human接口
package cn.spring.iface;
import cn.spring.face.Human;
public class Chinese implements Human {
    @Override
    public void eat() {
        System.out.println("舌尖上的中国,中国人很会吃...");
    }
    @Override
    public void walk() {
        System.out.println("中国人喜欢散步...");
}
//美国人类实现Human接口
package cn.spring.iface;
import cn.spring.face.Human;
public class American implements Human {
    @Override
    public void eat() {
        System.out.println("美国人吃西餐...");
    }
    @Override
    public void walk() {
        System.out.println("美国人喜欢坐车...");
}


//test测试类
package cn.spring.test;
import cn.spring.face.Human;
import cn.spring.factory.HumanFactory;
import cn.spring.iface.American;
import cn.spring.iface.Chinese;
public class test1 {
    public static void main(String[] args) {
        /**
         * 传统方式:直接创建
         * 特点:耦合,不合理可扩展性,灵活性不足
         */
        Human chinese = new Chinese();
        chinese.eat();
        chinese.walk();

        Human american = new American();
        american.eat();
        american.walk();
    }
}

很明显,当有100个或者更多的中国人(美国人)时,我们需要一个一个创建其响应的实例,这样式很不合理的,所以我们加入“工厂类”,也就是将共同的特性提取出来,在使用时直接调用即可:

//加入工厂模式
package cn.spring.factory;
import cn.spring.face.Human;
import cn.spring.iface.American;
import cn.spring.iface.Chinese;
public class HumanFactory {
    public Human getHuman(String name){
        if ("ch".equals(name)){
            return new Chinese();
        }else if ("am".equals(name)){
            return new American();
        }else {
            throw new IllegalArgumentException("参数输入不正确!");
        }
    }
}
//测试类
package cn.spring.test;
import cn.spring.face.Human;
import cn.spring.factory.HumanFactory;
import cn.spring.iface.American;
import cn.spring.iface.Chinese;
public class test {
    public static void main(String[] args) {
        /**
         * 工厂模式:解耦合,提高灵活性和可扩展性
         */
        HumanFactory humanFactory = new HumanFactory();
        Human human = humanFactory.getHuman("ch");
        human.eat();
    }
}
  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-07-14 10:44:42  更:2021-07-14 10:44:51 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/2 13:48:36-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码