最新版spring教程------学习笔记一

1.spring

1.1、简介

  • Spring:春天------>给软件行业带来春天

  • 2002,首次推出Spring框架的雏形:interface21框架

  • Spring框架即以interface21框架为基础,经过重新设计,并不断丰富其内涵,于2004年3月24日,发布了1.0正式版

  • Rod Johnson:Spring Framework创始人,著名作者。很难想象Rod Johnson的学历,真的让好多人大吃一惊,他是悉尼大学的博士,然而他的专业不是计算机,而是音乐学。 Don’t Reinvent the Wheel

  • spring理念:使现有的技术更加容易使用,本身是一个大杂烩,整合了现有技术框架
  • SSH:Struct2 +Spring +Hibernate

  • SSM:SpringMvc+Spring + Mybatis

官网:https://spring.io/projects/spring-framework#overview

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

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

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

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.0.RELEASE</version>
</dependency>

1.2、优点

  • Spring是一个开源的免费的框架(容器)

  • Spring是一个轻量级的、非入侵式的框架

  • 控制反转(IOC),面向切面编程(AOP)
  • 支持事务的处理,对框架整合的支持

  • 总结:Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架

1.3、组成

Spring 框架是一个分层架构,由 7 个定义良好的模块组成。Spring 模块构建在核心容器之上,核心容器定义了创建、配置和管理 bean 的方式 .

1569934897811.png

组成 Spring 框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现。每个模块的功能如下:

  • 核心容器:核心容器提供 Spring 框架的基本功能。核心容器的主要组件是 BeanFactory,它是工厂模式的实现。BeanFactory 使用控制反转(IOC) 模式将应用程序的配置和依赖性规范与实际的应用程序代码分开。
  • Spring 上下文:Spring 上下文是一个配置文件,向 Spring 框架提供上下文信息。Spring 上下文包括企业服务,例如 JNDI、EJB、电子邮件、国际化、校验和调度功能。
  • Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向切面的编程功能 , 集成到了 Spring 框架中。所以,可以很容易地使 Spring 框架管理任何支持 AOP的对象。Spring AOP 模块为基于 Spring 的应用程序中的对象提供了事务管理服务。通过使用 Spring AOP,不用依赖组件,就可以将声明性事务管理集成到应用程序中。
  • Spring DAO:JDBC DAO 抽象层提供了有意义的异常层次结构,可用该结构来管理异常处理和不同数据库供应商抛出的错误消息。异常层次结构简化了错误处理,并且极大地降低了需要编写的异常代码数量(例如打开和关闭连接)。Spring DAO 的面向 JDBC 的异常遵从通用的 DAO 异常层次结构。
  • Spring ORM:Spring 框架插入了若干个 ORM 框架,从而提供了 ORM 的对象关系工具,其中包括 JDO、Hibernate 和 iBatis SQL Map。所有这些都遵从 Spring 的通用事务和 DAO 异常层次结构。
  • Spring Web 模块:Web 上下文模块建立在应用程序上下文模块之上,为基于 Web 的应用程序提供了上下文。所以,Spring 框架支持与 Jakarta Struts 的集成。Web 模块还简化了处理多部分请求以及将请求参数绑定到域对象的工作。
  • Spring MVC 框架:MVC 框架是一个全功能的构建 Web 应用程序的 MVC 实现。通过策略接口,MVC 框架变成为高度可配置的,MVC 容纳了大量视图技术,其中包括 JSP、Velocity、Tiles、iText 和 POI。

1569934858546.png

1.4、拓展

在Spring的官网有这个介绍:现代化的开发!说白了就是基于Spring的开发

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8WyAJ8B0-1584865088723)(https://blog.kuangstudy.com/usr/uploads/2019/10/2419223029.png)]

  • Spring Boot

    • 一个快速开发的脚手架
    • 基于Springboot可以快速的开发单个微服务
    • 约定大于配置
  • Spring Cloud

    • SpringCloud是基于SpringBoot实现的……

因为现在大多数公司都在使用Springboot进行快速开发,学习Springboot的前提是,需要完全掌握Spring及SpringMVC,承上启下的作用

因为现在的大多数公司都在使用Springboot进行快速开发,学习spring boot的前提,需要完全掌握spring及

springMvc! 承上启下的作用!

弊端:发展太久,配置十分繁琐,人称“配合地狱

2.IOC理论推导

1.UserDao接口

2.UserDaoImpl实现类

3.UserService业务接口

4.UserServiceImpl业务实现类

使用set方法注入

private UserDao userDao;
    //利用set动态实现值的注入
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

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

这种思想从本质上解决了问题,程序员不再去管理对象的创建,降低耦合,可以使程序员专注业务实现,这是IOC的原型9*

2.1IOC本质

控制反转IOC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现ioc的一种方法,也有人认为DI只是

IOC的另一种说法。没有IOC的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓的控制反转就是:获得依赖对象的方式反转了。

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

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

3.HelloSpring

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">

</beans>

bean对象添加:

<bean id="mysqlImpl" class="com.kuang.dao.UserDaoMysqlImpl"></bean>
<bean id="oracleImpl" class="com.kuang.dao.UserDaoOracleImpl"></bean>

<bean id="UserServiceImpl" class="com.kuang.service.UserServiceImpl">
    <!--	
        ref:引用Spring容器中已经创建好的对象
        value:具体的值,基本数据类型
        -->
    <property name="userDao" ref="mysqlImpl"></property>

Test:

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//getBean:参数即为spring配置文件中bean的id
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.toString());

思考问题

  • Hello对象是谁创建的?

    hello对象是由Spring创建的。

  • Hello对象的属性是怎么设置的?

    hello对象的属性是由Spring容器设置的。

这个过程就叫做控制反转:

控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的。

反转:程序本身不创建对象,而变成被动的接收对象。

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

IoC是一种编程思想,由主动的编程编程被动的接收。

可以通过new ClassPathXmlApplicationContext去浏览一下底层源码。

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

4.IOC创建对象的方式

1.使用无参构造创建对象,默认

2.假设我们需要使用有参构造:

<!-- 方式一:下标赋值-->
    <bean class="com.ioc.pojo.User" id="user">
       <constructor-arg index="0" value="Hello Spring "/>
    </bean>
<!--   方式二:通过类型创建,不建议使用过-->
    <bean id="user" class="com.ioc.pojo.User">
        <constructor-arg type="java.lang.String" value="小楠"/>
    </bean>
<!--    方式三:直接通过参数名来设置-->
    <bean id="user" class="com.ioc.pojo.User">
        <constructor-arg name="name" value="小楠同学"/>
    </bean>

spring 容器 就类似于婚姻介绍所

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

5.spring配置

5.1起别名

别名:如果添加了别名,我们也可以通过使用别名来获取到这个对象

 <bean id="user" class="com.ioc.pojo.User">
        <constructor-arg name="name" value="小楠同学"/>
    </bean>
    <alias name="user" alias="user2"/>

测试:

public class MynewTest {
    public static void main(String[] args) {

        ApplicationContext Context = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) Context.getBean("user2");
//        UserT userT2 = (UserT) Context.getBean("usert");
//       UserT userT = (UserT) Context.getBean("usert");
//       userT.show();
//        System.out.println(userT2 ==userT);
        user.show();

    }

}

5.2Bean的配置

id:

bean的唯一标识符,也就是相当于我们学的对象名

class:

bean 对象所对应的全限定名: 包名+类型

name:

也就是别名,而且name更高级,可以同时去多个别名

5.3import

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

6.DI依赖注入

6.1、构造器注入

6.2、Set方式注入【重点】

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

【环境搭建】

1.复杂类型

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

2.真实测试对象

package com.xiaonan.pojo;

import java.util.*;

/**
 * @author XIANS
 */
public class Student {
    private String name;
    private  Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private  String wife;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

测试环境是否成功:

<?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.xiaonan.pojo.Student">
            <property name="name" value="小楠"/>
        </bean>
</beans>

Test:

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.getName());

    }

}

在这里插入图片描述

正式测试:

3.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="address" class="com.xiaonan.pojo.Address">
            <property name="address" value="江西"/>
        </bean>
        <bean id="student" class="com.xiaonan.pojo.Student">
            <!--第一种,普通注入,value-->
            <property name="name" value="小楠"/>
            <!--第二种,bean注入,ref-->
            <property name="address" ref="address"/>
            <!--数组-->
            <property name="books">
                <array>
                    <value>西游记</value>
                    <value>红罗梦</value>
                    <value>小楠</value>
                </array>
            </property>
            <!--            list-->
            <property name="hobbies">
                <list>
                    <value>听歌</value>
                    <value>写代码</value>
                    <value>玩游戏</value>
                </list>
            </property>
            <!--             Map-->
            <property name="card">
                <map>
                    <entry key="ID card" value="121231313123"/>
                    <entry key="Bank card" value="1313516546465"/>
                </map>
            </property>
            <!--            set-->
            <property name="games">
                <set>
                    <value>cs go</value>
                    <value>BoB</value>
                </set>
            </property>
            <!--            null-->
            <property name="wife">
                <null></null>
            </property>
            <!--            properties-->
            <property name="info">
               <props>
                   <prop key="student ID">20184562</prop>
                   <prop key="gender">male</prop>
               </props>
            </property>
        </bean>
</beans>

4.测试类

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }

}
//Student{name='小楠', 
// address=Address{address='江西'}, 
// books=[西游记, 红罗梦, 小楠], 
// hobbies=[听歌, 写代码, 玩游戏], 
// card={ID card=121231313123, Bank card=1313516546465}, 
// games=[cs go, BoB],
// wife='null', 
// info={gender=male, student ID=20184562}}

在这里插入图片描述

6.3、拓展方式注入

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

官方解释:

实体类:

package com.xiaonan.pojo;

/**
 * @author XIANS
 */
public class User {
    private String name;
    private int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

userbeans.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--p命名空间注入 可以会直接注入属性 ==>property-->
        <bean id="user" class="com.xiaonan.pojo.User" p:name="小楠" p:age="19" />

        <!--c命名空间 通过构造器注入===>construct-args-->
        <bean id="user2" class="com.xiaonan.pojo.User" c:age="18" c:name="小楠"/>
</beans>

测试:

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

注意点:p命名和c命名空间不能直接而使用,需要导入xml约束

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值