Spring轻量级开发框架(一)

简介: Spring轻量级开发框架

Spring

1.Spring

1、1spring简介

核心宗旨:简化服务器开发

  • Spring:春天---->可以理解为给软件行业带来了春天
  • 2002,Rod Jahnson首次推出了spring框架的雏形
  • 2004,三月二十四号诞生
  • spring框架是以interface21框架为基础,经过重新设计,并且不断丰富其内涵,于2004年3月24日发布了1.0的正式版本
  • Spring框架的创始人,同时也是SpringSource的联合创始人。Spring是面向切面编程(AOP)和控制反转(IoC)的容器框架。Rod的畅销书Expert One-on-One J2EE Design and Development(2002年出版)是迄今为止J2EE领域最具影响力的书之一。
  • spring理念,使用现有技术更加容易使用,本身是一个大杂烩,整合了现有技术的框架
  • SSH:Stuct2+spring+hibernate
  • SSM:SpringMvc+Spring+Mybatis

官网:https://spring.io/

官方下载地址:https://repo.spring.io/release/org/springframework/spring/

Github:https://github.com/spring-projects/spring-framework


<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.7</version>
</dependency>

1.2、优点

  • Spring是一个开源免费的框架(容器)!
  • Spring是一个轻量级的,非入侵式的框架!
  • 控制反转(ioc),面向切面变成(AOP)
  • 支持事务的处理,对框架整合的支持

总结一句话,Spring就是一个轻量级的控制反转(ioc)和面向切面(AOP)变成的框架

1.3、组成

每个模块的作用如下:

核心容器(Spring Core):核心容器提供 Spring 框架的基本功能。核心容器的主要组件是 BeanFactory,它是工厂模式的实现。BeanFactory 使用控制反转 (IOC) 模式将应用程序的配置和依赖性规范与实际的应用程序代码分开。

Spring 上下文(Spring Context):Spring 上下文是一个配置文件,向 Spring 框架提供上下文信息。Spring 上下文包括企业服务,例如 JNDI(Java命名和目录接口)、EJB(Enterprise Java Beans称为Java 企业Bean)、电子邮件、国际化、校验和调度功能。

Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向方面的编程功能集成到了 Spring 框架中。所以,可以很容易地使 Spring 框架管理的任何对象支持 AOP。Spring AOP 模块为基于 Spring 的应用程序中的对象提供了事务管理服务。通过使用 Spring AOP,不用依赖 EJB 组件,就可以将声明性事务管理集成到应用程序中。

Spring DAO:JDBC DAO 抽象层提供了有意义的异常层次结构,可用该结构来管理异常处理和不同数据库供应商抛出的错误消息。异常层次结构简化了错误处理,并且极大地降低了需要编写 的异常代码数量(例如打开和关闭连接)。Spring DAO 的面向 JDBC 的异常遵从通用的 DAO 异常层次结构。

Spring ORM:Spring 框架插入了若干个 ORM 框架,从而提供了 ORM 的对象关系工具,其中包括 JDO、Hibernate 和 iBatis SQL Map。所有这些都遵从 Spring 的通用事务和 DAO 异常层次结构。

Spring Web 模块:Web 上下文模块建立在应用程序上下文模块之上,为基于 Web 的应用程序提供了上下文。Web 模块还简化了处理多部分请求以及将请求参数绑定到域对象的工作。

Spring MVC 框架:MVC 框架是一个全功能的构建 Web 应用程序的 MVC 实现。通过策略接口,MVC 框架变成为高度可配置的,MVC 容纳了大量视图技术,其中包括 JSP。

1.4、拓展

在Spring官网有这样一个介绍:现代化的java开发!现在的开发说白了就是spring开发

  • SpringBoot
  • 一个快速开发的脚手架。
  • 可以基于SrpingBoot可快速开发单个微服务。
  • 约定大于配置!
  • SpringCloud
  • SpringCloud是基于 SpringBoot实现的

应为大多数公司都在使用SpringBoot进行快速开发,学习SpringBoot的前提是需要完全掌握Spring及SpringMvc,承上启下的作用
弊端:发展了太久之后违背了原来的理念,配置十分繁琐,人称配置地狱

2、IOC理论推导

2.1主要是转变思维:只要是学习这个想法

曾经的开发步骤:

  1. UserDAO接口
  2. UserDAOimpl实现类
  3. UserService业务接口
  4. UserServiceimpl业务实现类

ioc是一种思想,在我们之前的业务中,用户的需求,可能会影响我们的源代码,我们需要根据用户的需求去修改代码,如果程序代码量十分大,修改一次的成本代价十分昂贵

我们用一个Set接口实现,

public class UserServiceImpl implements UserService {
    private UserDAO userDAO;
     public void setUserDAO(UserDAO userDAO){
        this.userDAO = userDAO;
   }
    public void getUser() {
        userDAO.getUser();
    }
}

革命性质的变化,

  • 之前,程序是主动创建对象!控制权在程序员手上
  • 修改使用Set注入后,程序不在具有主动性,而是变成了被动接受对象

控制反转。就是这个思想,从主动变成被动的反转,这种思想从本质上解决了问题,我们程序员不用在去管理程序对象的创建了。系统的耦合性大大降低,可以更加专注的在业务的实现上

2.2ioc本质

控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法,也有人认为DI只是IoC的另一种说法。没有IoC的程序中 , 我们使用面向对象编程 , 对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了。


IoC是Spring框架的核心内容,使用多种方式完美的实现了IoC,可以使用XML配置,也可以使用注解,新版本的Spring也可以零配置实现IoC。

Spring容器在初始化时先读取配置文件,根据配置文件或元数据创建与组织对象存入容器中,程序使用时再从Ioc容器中取出需要的对象。

采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式可以把两者合为一体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。

控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)。

3、helloSpring

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cF6lKXIb-1648917346642)(https://gitee.com/cold-abyss_admin/my-image-host/raw/master/ img /未命名文件 (1)].png)

3.1、导入Jar包

注 : spring 需要导入commons-logging进行日志记录 . 我们利用maven , 他会自动下载对应的依赖项 .

<dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>5.3.7</version>
     </dependency>

3.2、编写代码

  1. 编写一个Hello实体类
package com.hyc.pojo;
public class test {
    @Override
    public String toString() {
        return "test{" +
                "str='" + str + '\'' +
                '}';
    }
    public String getStr() {
        return str;
    }
    public void setStr(String str) {
        this.str = str;
    }
    private  String str;
}


  1. 编写我们的spring文件 , 这里我们命名为beans.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用Spring来创建对象,在Spring这些都成为Bean
平时 类型 变量名 = new 类型()
    test ts = new test();
Spring中
id = 变量名
class = new 对象
bean = 对象  相当于 new test();
property 相当于给对象中的属性设置一个值
以来set注入
-->
    <bean id="hello" class="com.hyc.pojo.test">
        <property name="str" value="Spring"></property>
    </bean>
</beans>


  1. 我们可以去进行测试了 .
import com.hyc.pojo.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MuTest {
  public static void main(String[] args) {
    //获取Spring的上下文对象
      ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
      //我们的对象现在都在Spring中管理,要使用直接取出来就可以了
      test hello = (test)context.getBean("hello");
    System.out.println(hello.toString());
  }
}


3.3、思考

  • test对象是谁创建的 ? 【 hello 对象是由Spring创建的 】
  • test对象的属性是怎么设置的 ? 【hello 对象的属性是由Spring容器设置的 】

这个过程就叫控制反转 :

  • 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
  • 反转 : 程序本身不创建对象 , 而变成被动的接收对象 .

依赖注入 : 就是利用set方法来进行注入的.

IOC是一种编程思想,由主动的编程变成被动的接收

可以通过newClassPathXmlApplicationContext去浏览一下底层源码 .

3.4、修改案例一

我们在案例一中, 新增一个Spring配置文件beans.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用Spring来创建对象,在Spring这些都成为Bean
平时 类型 变量名 = new 类型()
    test ts = new test();
Spring中
id = 变量名
class = new 对象
bean = 对象  相当于 new test();
property 相当于给对象中的属性设置一个值
依赖set注入
-->
<bean id="mysqlimpl" class="com.hyc.dao.UserMysqlIMPL"></bean>
<bean id="hi" class="com.hyc.dao.UserDaoImpl"></bean>
<bean id="usService" class="com.hyc.Service.UserServiceImpl">
<!--    ref 引用Spring容器中创建好的对象
        value: 具体的值,基本类型使用
-->
<property name="userDAO"  ref="mysqlimpl"></property>
</bean>
</beans>

测试!

public class MyTest {
  public static void main(String[] args) {
      //获取ApplicationContext拿到Spring的容器
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    //有容器,天下我有,老子要什么就有什么
      UserServiceImpl usService =(UserServiceImpl) context.getBean("usService");
      usService.getUser();
  }
}

OK , 到了现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改 , 所谓的IoC,一句话搞定 : 对象由Spring 来创建 , 管理 , 装配 !

4.IOC创建对象的方式

  1. 使用无参构造创建对象,默认
  2. 使用有参的方法,假设我们要用有参构造创建对象
  1. 下标赋值
<!--   第一种 下标赋值-->
<bean id="user" class="com.hyc.pojo.User">
 <constructor-arg index="0" value="胡宇辰"></constructor-arg>
</bean>


  1. 类型赋值(不推荐)
<!--通过类型创建-->
    <bean id="user" class="com.hyc.pojo.User">
        <!-- <constructor-arg index="0" value="胡宇辰"></constructor-arg>-->
        <constructor-arg type="java.lang.String" value="胡宇辰"></constructor-arg>
    </bean>


  1. 通过变量名来赋值
<!--第三种,通过参数名来赋值-->
    <bean id="user" class="com.hyc.pojo.User">
        <constructor-arg name="name" value="胡宇辰"></constructor-arg>
    </bean>


总结:在配置文件加载的时候,容器中管理的对象就已经初始化了!

5、Spring配置

5.1、别名

<!--    别名:如果添加了别名我们也可以使用别名去获取类-->
    <alias name="user" alias="usersss"></alias>

5.2、Bean的配置、

<!--
id:bean的唯一标识符,相当于我们的对象名
class:bean对象所对应的全限定名:包名+类名
name:也是别名
-->
    <bean  id="UserT" class="com.hyc.pojo.User2" name="lz"></bean>

5.3import

这个import,一般用于团队开发使用,可以将多个配置文件导入合并为一个

假设,有多个人开发一个项目,这三个人负责不同的类开发,不同的类需要注册在不同的bean中偶们可以import将所有人的beans,xml合并成一个总的

  • 张三
  • 李四
  • 王五
  • 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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <import resource="beans.xml"/>
    <import resource="beans2.xml"/>
</beans>

6.DI依赖注入、

6.1构造器注入

前面已经讲了

6.2、Set方式注入

  • 依赖注入:Set注入
  • 依赖:bean对象的创建依赖于容器
  • 注入:bean对象中的所有属性由容器来注入

【环境搭建】

  1. 复杂类型
private String address;
    public Address() {
    }
    public Address(String address) {
        this.address = address;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address =address;
    }


  1. 真实测试对象
private String name;
    private Address address;
    private String[]books;
    private List<String>hobbys;
    private Map<String,String> card;   
  private Set<String> games;
    private String wife;
    private Properties info;


  1. 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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.hyc.pojo.Student">
    <property name="name" value="hyc"></property>
</bean>
</beans>


  1. 测试类
import com.hyc.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
  public static void main(String[] args) {
    //
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      Student student = (Student) context.getBean("student");
    System.out.println(student.getName());
  }
}


6.3拓展方式注入

我们可以使用p命名和c命名空间来进行注入

官方解释:

<!--p命名空间注入,可以直接注入属性的,本质是setter-->
    <bean id="user" class="com.hyc.pojo.User" p:name="qing" p:age="18">
    </bean>
    <!--c命名空间注入,通过构造器注入 本质是构造器-->
    <bean id="user2"  class="com.hyc.pojo.User" c:name="胡宇辰" c:age="19"></bean>

测试:

@Test
    public void test2(){
     ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml");
      User user = context.getBean("user", User.class);
    System.out.println(user);
  }

注意点:

p和c命名空间不能直接使用,需要导入xml约束,才能使用

xmlns:p="http://www.springframework.org/schema/p"

6.4、bean的作用于

Scope

Description

singleton

(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.

prototype

Scopes a single bean definition to any number of object instances.

request

Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext

.

session

Scopes a single bean definition to the lifecycle of an HTTP Session

. Only valid in the context of a web-aware Spring ApplicationContext

.

application

Scopes a single bean definition to the lifecycle of a ServletContext

. Only valid in the context of a web-aware Spring ApplicationContext

.

websocket

Scopes a single bean definition to the lifecycle of a WebSocket

. Only valid in the context of a web-aware Spring ApplicationContext

  1. 代理模式(Spring默认机制)
<bean id="ServiceImpl" class="cn.csdn.service.ServiceImpl" scope="singleton">


  1. 原型模式:每次从容器中get的时候,都会产生一个新的对象!他们的hashcode是不相等的
  2. 其余的只在web开发中使用到

7.Bean的自动装配

  • 自动装配是Spring满足Bean依赖的一种方式
  • Spring会在上下文中自动寻找,并且自动给bean装配属性

在spring中有三种自动装配的方式

  1. 在xml显示的配置
  2. 在java中的显示配置
  3. 隐式的自动装配bean【重要】

7.1、测试

环境搭建一个人有两个宠物

7.2、自动装配

Byname:byName:会自动在容器上下文中查找,和自己对象set的对应的值beanid

<bean id="perple" class="com.hyc.pojo.Perple" autowire="byName">
    <property name="name" value="Hyc"></property>
    
</bean>

ByType:byType:会自动在容器上下文中查找,和自己对象同属性的相同的bean

<bean id="perple" class="com.hyc.pojo.Perple" autowire="byType">
    <property name="name" value="Hyc"></property>
    
</bean>

小结:

  • byname的时候需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法一致
  • bytype的时候需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致


Spring轻量级开发框架(二)https://developer.aliyun.com/article/1469576

目录
相关文章
|
30天前
|
XML 开发框架 Java
Spring轻量级开发框架(二)
Spring轻量级开发框架
47 0
|
1月前
|
开发框架 安全 Java
Spring 框架:企业级应用开发的强大工具
在当今数字化时代,企业级应用开发的需求日益增长。为了满足这一需求,开发者们需要一款功能强大、易于使用的开发框架。Spring 框架作为 Java 领域的领先者,为企业级应用开发提供了全面的解决方案。本文将深入探讨 Spring 框架的各个方面,包括其历史、核心模块、优势以及应用场景。
24 0
|
1月前
|
XML Java 数据格式
spring基础之常用组件
spring基础之常用组件
|
19天前
|
开发框架 安全 Java
探索 Spring 框架:企业级应用开发的强大工具
探索 Spring 框架:企业级应用开发的强大工具
28 2
|
30天前
|
SQL 开发框架 Java
Spring轻量级开发框架(三)
Spring轻量级开发框架
24 0
|
8天前
|
监控 Java 测试技术
使用 Spring Boot 简化企业级应用开发
【4月更文挑战第19天】Spring Boot简化企业级应用开发,加速项目初始化与配置,轻松集成多种技术,降低依赖管理复杂性。它提高开发效率,具备灵活扩展性,便于监控管理,且高度可测试。强大的社区支持解决问题迅速,助力企业快速推出高质量应用,提升市场竞争力。开发者掌握Spring Boot能增强自身价值。
|
8月前
|
监控 Java 测试技术
探索轻量级Java开发:Spring Boot 介绍与特性解析
在现代的软件开发领域,快速、高效地构建可靠的应用程序变得至关重要。Spring Boot,作为Spring Framework家族中的一员,正是为了满足这一需求而诞生的。本文将为您介绍 Spring Boot 的基本概念、特性以及为什么它在Java开发中如此受欢迎。
98 0
|
9月前
|
安全 Java 持续交付
Spring 框架有什么优点?
Spring 框架有什么优点?
35 0
|
9月前
|
设计模式 SQL 前端开发
spring的原生技术
Spring是一个非常流行的开发框架,它提供了许多原生技术和功能,帮助开发人员构建高效、可扩展和安全的应用程序。
55 0