2020-11-09 Spring

Spring

1.简介

春天,给软件行业带来了春天,mybatis简化了jdbc的操作,spring简化了new对象的操作。

Spring makes programming Java quicker, easier, and safer for everybody. Spring’s focus on speed, simplicity, and productivity has made it the world's most popular Java framework.

让软件开发更简单。Ioc和Aop

interface21框架是spring很古老的一个版本

spring于2004年发布的初始版本。

Rod Johnson Spring Framework的创始人。

  • 保持了强大的向后兼容性,用到了大量的设计模式,质量十分高。是为了解决企业应用开发的复杂性,使现有的技术更加容易使用。

  • servlet用分发器来实现,spring是一个大杂烩。整合了现有的技术框架

  • SSH: struct2 + spring + Hibernat(全自动的DAO框架)

  • SSM:SpringMVC + Spring + Mybatis(半自动的DAO框架)

官网源码:https://repo.spring.io/libs-release-local/org/springframework/spring/

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

maven依赖:

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

优点:1.开源的免费的容器。2. 轻量级的非入侵式的框架。3.Ioc 和 Aop 控制反转、面向切面编程

4.支持事务的处理,声明式事务,对事务的处理十分优秀。5.支持对框架的整合。

组成:

  • 拓展

现代化的java开发

 

 

  • SpringBoot

    • 一个快速开发的脚手架。

    • 基于springboot可以快速开发单个微服务。

  • SpringCloud

    • SpringCloud是基于SpringBoot实现的。

现在大多数公司都在使用SpringBoot进行快速开发,学习springCloud的前提,需要完全掌握Spring SpringMVC

弊端:发展了太久之后,违背了原有的理念,到后期配置将会十分繁琐,人称"配置地狱"。

 

2.Ioc理论推导

思想的学习和代码的学习是不同的。

 

根据用户需求的不同,我们必须一直修改接口,十分繁琐。

接口引用指向实现类。

public static void setUserDao(UserDao userDao) {
        UserServiceImpl.userDao = userDao;
    }
利用set注入后,程序不再具有主动性,而是变成了被动的接收对象。
这就叫做控制反转,这种现象从本质上解决了问题,我们程序员不用再自动创建对象了。
    

1.没有Ioc

这里用UserService和UserServicImpl是一样的,因为最终重载或者执行Impl类的getDao();

 

 

如果想要用Oracle

就要把UserDaoMysqlImpl();换成UserDaoOracleImpl();

  • 2.Ioc

这里就不需要接口引用指向实现类了,因为要用到实现类的Set注入。

 

 

 

本来由程序控制生成对象,private UserDao userDao = new UserDaoMysqlImpl();

变成了我们自己去输入 userService.setUserDao(new UserDaoMysqlImpl();

而现在是由我们自行控制创建对象 , 把主动权交给了调用者 . 程序不用去管怎么创建,怎么实现了 . 它只负责提供一个接口 .

这种思想 , 从本质上解决了问题 , 我们程序员不再去管理对象的创建了 , 更多的去关注业务的实现 . 耦合性大大降低 . 这也就是IOC的原型 !

 

3.Ioc本质

 

 

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

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

 

 

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

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

 

4.HelloSpring

1.

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">
    <bean id="..." class="...">  
        <!-- collaborators and configuration for this bean go here -->
    </bean>
    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>
    <!-- more bean definitions go here -->
    <bean id="hello" class="com.kyz.pojo.Hello">
        <property name="str" value="spring"></property>
    </bean>
</beans>

 2.实例化容器

//固定的用xml加载的话
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        // String applicationName = context.getApplicationName();
        //取出来即可
        Hello hello = context.getBean("hello", Hello.class);
        System.out.println(hello.toString());

 

  • Hello 对象是谁创建的 ? hello 对象是由Spring创建的

  • Hello 对象的属性是怎么设置的 ? hello 对象的属性是由Spring容器设置的这个过程就叫控制反转 :

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

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

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

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

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

思考

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

 

ClassPathXmlApplicationContext和ApplicationContext的关系。

经过N层继承,通过ConfigurableApplicationContext才继承了ApplicationContext

1.写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="mysqlimpl" class="com.kyz.dao.UserDaoMysqlImpl"></bean>
    <bean id="oracleimpl" class="com.kyz.dao.UserDaoOracleImpl"></bean>

    <bean id="UserServiceMysqlImpl" class="com.kyz.Service.UserServiceImpl">
<!--
      ref 引用spring中已经创建好的对象
      value就是个具体的值 基本数据类型去用
      -->
        <property name="userDao" ref="mysqlimpl"></property>
    </bean>

    <bean id="UserServiceOracleImpl" class="com.kyz.Service.UserServiceImpl">
        <!--
              ref 引用spring中已经创建好的对象
              value就是个具体的值 基本数据类型去用
              -->
        <property name="userDao" ref="oracleimpl"></property>
    </bean>


</beans>

 2.直接@Test

package com.kyz.test;
import com.kyz.Service.UserService;
import com.kyz.Service.UserServiceImpl;
import com.kyz.dao.UserDaoOracleImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MVCtest {
    @Test
    public void MVCtest1(){
        //原来的方式,通过Serviece层面的接口引用指向接口实现
        UserService userService = new UserServiceImpl();
        //调用UserService中重写的getDao()方法中的UserData();来调用Dao层中与数据库交互的方法。
        userService.getDao();
    }
    @Test
    public void MVCtest2(){
        //实例化UserServiceImpl实现类
        UserServiceImpl userService = new UserServiceImpl();
        userService.setUserDao(new UserDaoOracleImpl());
        userService.getDao();
    }
    @Test
    public void Springtest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");

        
        UserServiceImpl  userService = (UserServiceImpl) context.getBean("UserServiceMysqlImpl");
        userService.getDao();

        //UserServiceImpl userService = new UserServiceImpl();
        //userService.setUserDao(new UserDaoOracleImpl());
        UserServiceImpl userService1 = (UserServiceImpl) context.getBean("UserServiceOracleImpl");
        userService1.getDao();

    }
}

 

 我们现在彻底不用修改代码了,所谓的Ioc:对象的创建,由Spring来创建、管理和装配。

选择不同的饮料由我们手动装箱,变成了Spring(误)来帮我们选择pipeline来输出。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
整理自尚硅谷视频教程springboot高级篇,并增加部分springboot2.x的内容 一、Spring Boot与缓存 一、JSR107 Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry。 • CachingProvider定义了创建、配置、获取、管理和控制多个CacheManager。一个应用可 以在运行 期访问多个CachingProvider。 • CacheManager定义了创建、配置、获取、管理和控制多个唯一命名 的Cache,这些Cache 存在于CacheManager的上下文中。一个CacheManager仅被一个 CachingProvider所拥有。 • Cache是一个类似Map的数据结构并临时存储以Key为索引的值。一个 Cache仅被一个 CacheManager所拥有。 • Entry是一个存储在Cache中的key-value对。 • Expiry 每一 个存储在Cache中的条目有一个定义的有效期。一旦超过这个时间,条目为过期 的状态。一旦过期,条 目将不可访问、更新和删除。缓存有效期可以通过ExpiryPolicy设置。 二、Spring缓存抽象 Spring从3.1开始定义了org.springframework.cache.Cache 和 org.springframework.cache.CacheManager接口来统一不同的缓存技术; 并支持使用JCache(JSR- 107)注解简化我们开发; • Cache接口为缓存的组件规范定义,包含缓存的各种操作集合; • Cache接 口下Spring提供了各种xxxCache的实现;如RedisCache,EhCacheCache , ConcurrentMapCache 等; • 每次调用需要缓存功能的方法时,Spring会检查检查指定参数的指定的目标方法是否 已经被调用 过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法 并缓存结果后返回给用户。下 次调用直接从缓存中获取。 • 使用Spring缓存抽象时我们需要关注以下两点; 1、确定方法需要被缓存 以及他们的缓存策略 2、从缓存中读取之前缓存存储的数据 Cache 缓存接口,定义缓存操作。实现有:RedisCache、EhCacheCache、 ConcurrentMapCache等 CacheManager 缓存管理器,管理各种缓存(Cache)组件 @Cacheable 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 @CacheEvict 清空缓存 @CachePut 保证方法被调用,又希望结果被缓存。 @EnableCaching 开启基于注解的缓存 keyGenerator 缓存数据时key生成策略 serialize 缓存数据时value序列化策略 @CacheConfig 抽取缓存的公共配置 三、几个重要概念&缓存注解 1、常用注解 2、常用参数 名字 位置 描述 示例 methodName root object 当前被调用的方法名 #root.methodName method root object 当前被调用的方法 #root.method.name target root object 当前被调用的目标对象 #root.target targetClass root object 当前被调用的目标对象类 #root.targetClass args root object 当前被调用的方法的参数列表 #root.args[0] 3、常用参数SPEL说明 名字 位置 描述 示例 caches root object 当前方法调用使用的缓存列表(如 @Cacheable(value= {"cache1","cache2"}) ), 则有两 个cache #root.caches[0].name argument name evaluation context 方法参数的名字. 可以直接 #参数 名 ,也可以使用 #p0或#a0 的形 式,0代表参数的索引; #iban 、 #a0 、 #p0 result evaluation context 方法执行后的返回值(仅当方法执 行之后的判断有效,如‘unless’ , ’cache put’的表达式 ’cache evict’的表达式 beforeInvocation=false ) #result 四、代码中使用缓存 1、搭建基本环境 1、导入数据库文件 创建出department和employee表 2、创建javaBean封装数据 3、整合MyBatis操作数据库 1.配置数据源信息 2.使用注解版的MyBatis; 1)、@MapperScan指定需要扫描的mapper接口所在的包

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值