[spring]spring详细总结

spring

1.spring简介

  • Spring框架是一个开源的应用程序框架,是针对bean的生命周期进行管理的轻量级容器。
  • Spring解决了开发者在J2EE开发中遇到的许多常见的问题,提供了功能强大IOC、AOP及Web MVC等功能。
  • Spring可以单独应用于构筑应用程序,也可以和Struts、Webwork、Tapestry等众多Web框架组合使用,并且可以与 Swing等桌面应用程序AP组合。
  • Spring不仅仅能应用于J2EE应用程序之中,也可以应用于桌面应用程序以及小应用程序之中。
  • Spring框架主要由七部分组成,分别是 Spring Core、 Spring AOP、 Spring ORM、 Spring DAO、Spring Context、 Spring Web和 Spring Web MVC。

官方文档地址:

https://docs.spring.io/spring-framework/docs/4.3.9.RELEASE/spring-framework-reference/

https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#spring-core

中文

https://www.docs4dev.com/docs/zh/spring-framework/5.1.3.RELEASE/reference/

优点:

  • 开源免费
  • 轻量级的非入侵式的
  • 控制反转(IOC),面向切面编程(aop)
  • 支持事务处理

使用spring的jar包支持:


    org.springframework
    spring-web
    5.3.22

七大模块:

[spring]spring详细总结

弊端:发展了太久后,配置越来越多,人称”配置地狱”

2.IOC理论推导

在我们之前的业务中,用户的需求可能会影响程序的代码,可能需要修改代码,如果程序的代码量十分大,修改一次的成本十分的昂贵!

原来的方式:

private UserMapper usermapper=new UserMapperImpl();

现在将对象的传递由new变成set动态注入

private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper){
    this.userMapper=userMapper;
}

原来是程序控制的,现在变成用户控制了。

3.一个spring项目的快速搭建

(1)写一个实体类

package com.pojo;

/**
 * @author panglili
 * @create 2022-07-23-21:40
 */
public class HelloSpring {
    private String name;

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

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return name;
    }
}

(2)将实体类配置在spring容器


(3)测试

import com.pojo.HelloSpring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author panglili
 * @create 2022-07-23-21:43
 */
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext( "application.xml");
        HelloSpring hello =(HelloSpring) context.getBean("helloSpring");
        System.out.println(hello.toString());

    }
}

4.IOC创建对象的过程

  • 使用无参构造创造

[spring]spring详细总结

[spring]spring详细总结

设定为有参后,就会报错!

对象在被注册进去的时候,就被实例化了,直接使用就好。

5.IO注入

(1)前面的构造器注入

(2)set注入


构造注入对象之间的关系为组合

set注入的对象之间的关系为聚合

(3)p命名空间注入

  • 使用set方式注入

(4)c命名空间注入

  • 使用构造器方式注入,开启构造器才能用
HelloSpring(String name){
    this.name=name;
}
HelloSpring(){

}

注意导入头文件

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

6.作用域

ScopeDescription

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.

  • 单例

**

  • 原型

7.bean的自动装配

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

spring的装配方式:

(1)手动装配

  • 在people类中依赖了cat和dog对象,所以属性中手动装配他们的属性

(2)自动装配

通过byName自动装配

  • spring会自动去找people中的set后面相对应的cat和dog与bean中id对应

通过byType自动装配

  • spring会自动去找people中的对象依赖和bean中class类相同的对应

(3)使用注解实现自动装配

使用之前导入注解依赖的配置和支持


不再使用显示引用对象依赖的其他属性


@Autowired

直接在对象上面使用@Autowired注解

private String name;
@Autowired
private Dog dog;
@Autowired
private Cat cat;

如果装配环境复杂的话,可以通过@Qualifier(value = “cat”)指定bean注入

例如多个cat对象bean,属性值不同的时候


就需要

@Qualifier

否则,只会spring会走第一个bean

@Autowired
@Qualifier(value = "cat11")
private Cat cat;

Resource

  • 这个注解跟上面的@Autowired功能相似,但是它可以通过名字再通过类型装配,都没有才会报错,要比@Autowired智能一点,但使用较少。
@Resource(name="cat")
private Cat cat;

8.使用注解开发

1.bean

spring4以后,注解依赖于aop包,确保你的lib中有它

[spring]spring详细总结

确保开启了使用注解


2.组件代替bean实现自动注入

在配置文件中自动扫描包下的所有类为bean


在类对象上加上注解@Component可以被扫描

  • @component (把普通pojo实例化到spring容器中,相当于配置文件中的 )
@Component

使用注解给属性注入值

package pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author panglili
 * @create 2022-07-24-9:52
 */
@Component
public class People {
    private String name;
    @Value("123")
   public int age;
    private Dog dog;
    private Cat cat;
 }
}
  • 但是复杂的属性配置还是建议使用xml统一管理注入

3.component衍生的注解

dao:@repository

service:@service

controller:@controller

跟component功能相同只是能够使得分工更加的明确

小结:

xml与注解:

  • xml更加万能,适用于各种场合!维护简单方便
  • 注解 不是自己类使用不了,维护相对复杂

最佳实践:

  • xml用来管理bean
  • 注解只负责属性的注入

9.使用javaconfig实现代替xml配置

The central artifacts in Spring’s new Java-configuration support are @Configuration -annotated classes and @Bean -annotated methods.

根据官方文档的说明,创建一个java—configuration的配置类,使用@configuration注解,再使用@Bean在方法上面就可以实现xml中的功能。

(1)创建一个java—configuration

package config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import pojo.User;

/**
 * @author panglili
 * @create 2022-07-24-11:24
 */
@Configuration
public class Java_config {

    @Bean
    public User getUser(){
        return new User();
    }
}

(2)实体类

package pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author panglili
 * @create 2022-07-24-11:24
 */
@Component
public class User {
    @Value("tata")
    private String name;

    public String getName() {
        return name;
    }

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

(3)测试

现在也可以拿到值

import config.Java_config;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import pojo.User;

/**
 * @author panglili
 * @create 2022-07-24-11:28
 */
public class MyTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Java_config.class);
        User user =(User) context.getBean("getUser");

        System.out.println(user.getName());
    }
}

[spring]spring详细总结

这种java代替xml的实现方式,在springboot中大量使用。

10.代理模式

代理模式的分类:

  • 静态代理
  • 动态代理

关系分析

[spring]spring详细总结

抽象角色:一般会使用接口或者抽象类

真实角色:被代理的角色

代理角色:代理真实的角色,做一些附属的操作

客户:访问代理对象的人

静态代理

步骤:

抽象角色:

/**
 * @author panglili
 * @create 2022-07-24-15:00
 */
public interface Rent {
    public void rent();
}

真实角色:

/**
 * @author panglili
 * @create 2022-07-24-15:01
 */
public class Host implements Rent {
    public void rent() {
        System.out.println("host would rent house");

    }
}

代理角色:

/**
 * @author panglili
 * @create 2022-07-24-15:02
 */
public class Proxy implements Rent {

    private Host host;
    public Proxy(){

    }

    public Proxy(Host host){
        this.host=host;
    }

    public void rent() {
        host.rent();
        seeHouse();
    }

    //look house
    public void seeHouse(){
        System.out.println("look House");
    }
}

客户:

/**
 * @author panglili
 * @create 2022-07-24-15:02
 */
public class Client  {
    public static void main(String[] args) {
        Host host = new Host();
        Proxy proxy = new Proxy(host);
        proxy .rent();
    }
}

动态代理

  • 和静态代理的角色一样
  • 动态生成,不是我们直接写好了的
  • 动态代理分为两大类:基于接口的动态代理和基于类的动态代理
  • 基于接口——jdbc
  • 基于类——cglib
  • 基于字节码

11.AOP切面编程

使用aop植入,需要导入一个依赖包


    org.aspectj
    aspectjweaver
    1.9.7

  • 横切关注点
  • 切面
  • 通知
  • 目标
  • 代理
  • 切入点
  • 连接点

在执行的方法前动态的添加一个日志输出

方法一使用spring类实现

(1)接口

package service;

/**
 * @author panglili
 * @create 2022-07-24-16:09
 */
public interface Service {
    public void add();
    public void delete();
    public void update();
    public void select();
}

(2)要代理的实现类

package service;

/**
 * @author panglili
 * @create 2022-07-24-16:09
 */
public class ServiceImpl implements Service {
    public void add() {
        System.out.println("add");
    }

    public void delete() {
        System.out.println("delete");
    }

    public void update() {
        System.out.println("update");
    }

    public void select() {
        System.out.println("select");
    }
}

(3)代理商

package log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * @author panglili
 * @create 2022-07-24-16:11
 */
public class Log implements MethodBeforeAdvice {
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("excutor"+method.getName()+target);
    }
}

(4)客户调用

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

/**
 * @author panglili
 * @create 2022-07-24-16:28
 */
public class MyTest  {
    public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        //注意,代理的是接口类型
        Service ser = (Service) context.getBean("userSer");
        ser.add();

    }
}

方法二 使用自定义类实现

(1)自定义一个类,是被横切进去的内容

package dir;

/**
 * @author panglili
 * @create 2022-07-24-17:00
 */
public class PointMethod {
    public void after(){
        System.out.println("执行方法之后切入......");
    }
    public void before(){
        System.out.println("执行方法之前切入......");
    }
}

(2)在配置文件中使用aop切入


(3)测试

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

/**
 * @author panglili
 * @create 2022-07-24-16:28
 */
public class MyTest  {
    public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        //注意,代理的是接口类型
        Service ser = (Service) context.getBean("userSer");
        ser.add();

    }
}

[spring]spring详细总结

方式三 使用注解实现aop

(1)在要被切入的地方使用@Aspect和@After等注解标识切面和切点等

package dir;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * @author panglili
 * @create 2022-07-24-17:00
 */
@Aspect
public class PointMethod {
    @After("execution(* service.ServiceImpl.*(..))")
    public void after(){
        System.out.println("执行方法之后切入......");
    }
    @Before("execution(* service.ServiceImpl.*(..))")
    public void before(){
        System.out.println("执行方法之前切入......");
    }
}

(2)将此类配置到spring中,别忘了开启注解支持


(3)测试

同上面一样的测试,不再赘述

12.spring和mybatis整合

1.导包


        junit
        junit
        4.13.1

        mysql
        mysql-connector-java
        8.0.29

        org.mybatis
        mybatis
        3.5.3

        org.springframework
        spring-webmvc
        5.3.19

        org.springframework
        spring-jdbc
        5.3.18

        org.aspectj
        aspectjweaver
        1.9.7

        org.mybatis
        mybatis-spring
        2.0.2

2.将mybatis中的配置到spring中

mybatis被spring接管后,他自己的配置文件只需要写别名和日志,事务和数据库连接交给spring



3.接口实现类

package mapper;

import org.mybatis.spring.SqlSessionTemplate;
import pojo.User;

import java.util.List;

/**
 * @author panglili
 * @create 2022-07-24-18:42
 */
public class UserMapperImpl implements UserMapper {
    private SqlSessionTemplate sqlSession;

    public void setSession(SqlSessionTemplate session) {
        this.sqlSession = session;
    }

    public List user() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        return mapper.user();
    }
}

测试

import mapper.UserMapper;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.User;
import java.util.List;

/**
 * @author panglili
 * @create 2022-07-24-18:01
 */
public class MyTest {
  @Test
    public void Test(){

       ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        UserMapper user1 = context.getBean("user", UserMapper.class);
        List list = user1.user();
        for(User u :list){
            System.out.println(u);
        }

    }
}

13.事务支持

只需要在spring中简单的配置两点就可以实现


注意代理的是接口不是类!!!


Original: https://www.cnblogs.com/lumanmanqixiuyuanxi/p/16544483.html
Author: 路漫漫qixiuyuanxi
Title: [spring]spring详细总结

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/574461/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 32位Win7下安装与配置PHP环境(一)

    运行PHP网站,主要需要安装、配置三个软件,Apache、PHP和MySQL。如果需要编辑调试PHP程序,还要安装一个编辑调试软件。一、 ApacheApache是和IIS类似的一…

    Java 2023年5月29日
    071
  • Hadoop Archives(HDFS文件归档)

    Hadoop Archives Guide (HDFS文件归档)一.简介:Hadoop Archives 是特殊的归档格式,一个 Hadoop archives对应一个文件系统目录…

    Java 2023年6月9日
    084
  • TypeScript(7)泛型

    泛型 指在定义函数、接口或类的时候,不预先指定具体的类型,而在使用的时候再指定具体类型的一种特性。 引入 下面创建一个函数, 实现功能: 根据指定的数量 count 和数据 val…

    Java 2023年6月9日
    089
  • 解决 No IDEA annotations attached to the JDK 1.8和xml文件没有代码提示

    Android studio3.3 用着用着突然xml里没有代码联想了,忙着做其他的就没管,写xml的时候就硬写… 然后今天用着突然在class文件上方提示 No ID…

    Java 2023年5月30日
    073
  • 关系数据库元数据处理类(三) 创建查询元数据公共类

    1 public abstract class BaseMetadata : IMetadata 2 { 3 protected IDbUtility DbUtility; 4 5…

    Java 2023年6月5日
    063
  • 怎么判断一个对象已经被回收—虚引用

    虚引用是使用PhantomReference创建的引用,虚引用也称为幽灵引用或者幻影引用,是所有引用类型中最弱的一个。一个对象是否有虚引用的存在,完全不会对其生命周期构成影响,也无…

    Java 2023年6月6日
    071
  • Java面向对象

    面向对象 1.面向对象的概念 面向对象和面向过程的区别 面向过程: 将问题分解成步骤,然后按照步骤实现函数,执行时一次调用函数。 数据和对数据的操作是分离的。 面向对象: 将问题分…

    Java 2023年6月9日
    085
  • PoweJob高级特性-MapReduce完整示例

    由于网上搜索 PowerJob MapReduce 都是设计原理,demo也展示个空壳子,没有演示Map到Reduce结果怎么传递,对于没有MR开发经验的人来说并没有什么帮助,所以…

    Java 2023年6月6日
    082
  • rocketmq 精华

    介绍 rocket mq 翻译成中文就是火箭消息队列,从名字就可以看出来,它是一个很快的消息队列… rocket mq 是 阿里巴巴研制的后面贡献给 apache 基金…

    Java 2023年6月8日
    078
  • spring cloud alibaba (一)

    Spring Cloud Alibaba 官网:https://spring.io/projects/spring-cloud-alibabagitHub:https://gith…

    Java 2023年6月5日
    069
  • 【米哈游2023届秋招】开启【校招唯一专属内推码EYTUC】

    爆个猛料 很猛的 米哈游7月 又有新岗位啦!!!!不打卡弹性工作。抓紧金7银8面试机会,年中岗位多、急、需求量大,offer流程快。现招:技术(算法、前端、后台、大数据、安卓ios…

    Java 2023年6月8日
    0108
  • uni模板

    html;gutter:true; export default { components:{ }, data () { return {} }, onLoad () {}, on…

    Java 2023年5月29日
    083
  • SpringBoot扩展配置

    一、配置随机数 RandomValuePropertySource可以用于注入随机数,它支持integer、long、uuid、string。 my.secret=${random…

    Java 2023年5月30日
    067
  • Spring Framework

    1、容器及其管理的组件所提供的最重要的好处就是可拔插的体系结构。 2、控制反转:在组件类中,不需要使用新的操作符(一般为new)来实例化依赖组件, 而是在运行时由容器实例将依赖组件…

    Java 2023年6月6日
    075
  • Dubbo入门到精通学习笔记(动力节点王鹤)

    Dubbo学习笔记 *这篇笔记的学习视频来自b站动力节点 https://space.bilibili.com/76542346/video 第一章 初识 Dubbo 1.1 架构…

    Java 2023年6月9日
    0113
  • 这些OAuth2客户端的认证方式你未必了解

    OAuth2客户端按照它们与授权服务器进行安全认证的能力可以分为机密类型( Confidential)和公共类型( Public)。 机密类型的自身会有个密码凭据,比如Web服务器…

    Java 2023年6月8日
    0104
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球