SSM【Spring SpringMVC Mybatis】—— Spring(一)

目录

1、初识Spring

1.1 Spring简介

1.2 搭建Spring框架步骤

1.3 Spring特性

1.5 bean标签详解

2、SpringIOC底层实现

2.1 BeanFactory与ApplicationContexet

2.2 图解IOC类的结构

3、Spring依赖注入数值问题【重点】

3.1 字面量数值

3.2 CDATA区

3.3 外部已声明bean及级联属性赋值

3.4 内部bean

3.5 集合

4、Spring依赖注入方式【基于XML】

4.1 set注入

4.2 构造器注入

4.3 p名称空间注入

5、Spring管理第三方bean

5.1 Spring管理druid步骤

6、Spring中FactoryBean

6.1 Spring中两种bean

6.2 FactoryBean使用步骤


1、初识Spring

1.1 Spring简介

Spring是一个为简化企业级开发而生的开源框架。

Spring是一个IOC(DI)和AOP容器框架。

IOC全称:Inversion of Control【控制反转】

将对象【万物皆对象】控制权交个Spring

DI全称:(Dependency Injection):依赖注入

AOP全称:Aspect-Oriented Programming,面向切面编程

官网:https://spring.io/

1.2 搭建Spring框架步骤

导入jar包

<!--导入spring-context--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.1</version></dependency><!--导入junit4.12--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency>

编写核心配置文件

配置文件名称:applicationContext.xml【beans.xml或spring.xml】

配置文件路径:src/main/resources

示例代码

   

 <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 将对象装配到IOC容器中--><bean id="stuZhenzhong" class="com.atguigu.spring.pojo.Student"><property name="stuId" value="101"></property><property name="stuName" value="zhenzhong"></property></bean></beans>

使用核心类库

 

@Testpublic void testSpring(){//使用Spring之前//        Student student = new Student();//使用Spring之后//创建容器对象ApplicationContext iocObj =new ClassPathXmlApplicationContext("applicationContext.xml");//通过容器对象,获取需要对象Student stuZhenzhong = (Student)iocObj.getBean("stuZhenzhong");System.out.println("stuZhenzhong = " + stuZhenzhong);}

1.3 Spring特性

非侵入式:基于Spring开发的应用中的对象可以不依赖于Spring的API。

容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期。

组件化:Spring实现了使用简单的组件配置组合成一个复杂的应用。在 Spring 中可以使用XML和Java注解组合这些对象。

一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring 自身也提供了表述层的SpringMVC和持久层的JDBCTemplate)。

1.4 Spring中getBean()三种方式

getBean(String beanId):通过beanId获取对象

不足:需要强制类型转换,不灵活

getBean(Class clazz):通过Class方式获取对象

不足:容器中有多个相同类型bean的时候,会报如下错误:

expected single matching bean but found 2: stuZhenzhong,stuZhouxu

getBean(String beanId,Clazz clazz):通过beanId和Class获取对象

推荐使用

注意:框架默认都是通过无参构造器,帮助我们创建对象。

所以:如提供对象的构造器时,一定添加无参构造器

1.5 bean标签详解

属性

id:bean的唯一标识

class:定义bean的类型【class全类名】

子标签

property:为对象中属性赋值【set注入】

name属性:设置属性名称

value属性:设置属性数值

2、SpringIOC底层实现

IOC:将对象的控制器反转给Spring

2.1 BeanFactory与ApplicationContexet

BeanFactory:IOC容器的基本实现,是Spring内部的使用接口,是面向Spring本身的,不是提供给开发人员使用的。

ApplicationContext:BeanFactory的子接口,提供了更多高级特性。面向Spring的使用者,几乎所有场合都使用ApplicationContext而不是底层的BeanFactory。

2.2 图解IOC类的结构

BeanFactory:Spring底层IOC实现【面向Spring框架】

ApplicationContext:面向程序员

ConfigurableApplicationContext:提供关闭或刷新容器对象方法

ClassPathXmlApplicationContext:基于类路径检索xml文件

AnnotationConfigApplicationContext:基于注解创建容器对象

FileSystemXmlApplicationContext:基于文件系统检索xml文件

3、Spring依赖注入数值问题【重点】

3.1 字面量数值

数据类型:基本数据类型及包装类、String

语法:value属性或value标签

3.2 CDATA区

语法:\<![CDATA[]]>

作用:在xml中定义特殊字符时,使用CDATA区

3.3 外部已声明bean及级联属性赋值

语法:ref

注意:级联属性更改数值会影响外部声明bean【ref赋值的是引用】

示例代码

 

 <bean id="dept1" class="com.atguigu.pojo.Dept"><property name="deptId" value="1"></property><property name="deptName" value="研发部门"></property></bean><bean id="empChai" class="com.atguigu.pojo.Employee"><property name="id" value="101"></property><property name="lastName" value="chai"></property><property name="email" value="chai@163.com"></property><property name="salary" value="50.5"></property><property name="dept" ref="dept1"></property><property name="dept.deptName" value="财务部门"></property></bean>

3.4 内部bean

概述

内部类:在一个类中完整定义另一个类,当前类称之为内部类

内部bean:在一个bean中完整定义另一个bean,当前bean称之为内部bean

注意:内部bean不会直接装配到IOC容器中

示例代码

  <!--    测试内部bean--><bean id="empXin" class="com.atguigu.pojo.Employee"><property name="id" value="102"></property><property name="lastName" value="xx"></property><property name="email" value="xx@163.com"></property><property name="salary" value="51.5"></property><property name="dept"><bean class="com.atguigu.pojo.Dept"><property name="deptId" value="2"></property><property name="deptName" value="人事部门"></property></bean></property></bean>

3.5 集合

List

<!--    测试集合--><bean id="dept3" class="com.atguigu.pojo.Dept"><property name="deptId" value="3"></property><property name="deptName" value="程序员鼓励师"></property><property name="empList"><list><ref bean="empChai"></ref><ref bean="empXin"></ref><!--                <bean></bean>--></list></property></bean><!--    测试提取List--><util:list id="empList"><ref bean="empChai"></ref><ref bean="empXin"></ref></util:list><bean id="dept4" class="com.atguigu.pojo.Dept"><property name="deptId" value="4"></property><property name="deptName" value="运营部门"></property><property name="empList" ref="empList"></property></bean>

Map

 

 <!--    测试Map--><bean id="dept5" class="com.atguigu.pojo.Dept"><property name="deptId" value="5"></property><property name="deptName" value="采购部门"></property><property name="empMap"><map><entry key="101" value-ref="empChai"></entry><entry><key><value>103</value></key><ref bean="empChai"></ref></entry><entry><key><value>102</value></key><ref bean="empXin"></ref></entry></map></property></bean><util:map id="empMap"><entry key="101" value-ref="empChai"></entry><entry><key><value>103</value></key><ref bean="empChai"></ref></entry><entry><key><value>102</value></key><ref bean="empXin"></ref></entry></util:map><bean id="dept6" class="com.atguigu.pojo.Dept"><property name="deptId" value="106"></property><property name="deptName" value="后勤部门"></property><property name="empMap" ref="empMap"></property></bean>

4、Spring依赖注入方式【基于XML】

为属性赋值方式:

  1. 通过xxxset()方法:这是一种常见的方式,在类中提供了一系列的set方法,用于设置类的属性值。例如,如果有一个属性名为name,那么可能会有一个名为setName()的方法用于设置name属性的值。

  2. 通过构造器:另一种常见的方式是通过类的构造器来传递属性值。在构造对象时,通过构造器的参数列表将属性值传递给对象。这种方式可以在对象被创建时一次性地设置属性值,使得对象的状态在创建后就被确定下来。

  3. 反射:反射是一种高级的Java特性,允许在运行时检查类、获取类的信息以及动态调用类的方法和操作类的属性。通过反射,可以通过类的Field对象来设置对象的属性值,无论这些属性的可见性如何。

4.1 set注入

通过在XML配置文件中使用<property>标签来进行属性注入。在这种方式中,你可以指定属性的名称,并通过value属性或ref属性为属性赋值。如果是基本数据类型或字符串等简单类型,可以使用value属性直接赋值;如果是引用其他bean,可以使用ref属性指定引用的bean的id。

语法:\<property>

4.2 构造器注入

通过在XML配置文件中使用<constructor-arg>标签来进行构造器注入。与set注入类似,你可以在构造对象时指定构造器的参数,并通过value属性或ref属性为构造器参数赋值。这种方式适用于在创建对象时将属性值通过构造器传递给对象。

语法:\<constructor-arg>

4.3 p名称空间注入

导入名称空间:xmlns:p="http://www.springframework.org/schema/p"

语法:<bean p:xxx>

示例代码

  <bean id="stuZhouxu" class="com.atguigu.spring.pojo.Student"><property name="stuId" value="102"></property><property name="stuName"><value><![CDATA[<<zhouxu>>]]></value></property></bean><bean id="stuZhiFeng" class="com.atguigu.spring.pojo.Student"><constructor-arg name="stuId" value="103"></constructor-arg><constructor-arg name="stuName" value="zhifeng"></constructor-arg></bean><bean id="stuXiaoxi"class="com.atguigu.spring.pojo.Student"p:stuId="104"p:stuName="xiaoxi"></bean>

5、Spring管理第三方bean

5.1 Spring管理druid步骤

导入jar包

<!--导入druid的jar包--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency><!--导入mysql的jar包--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.37</version><!--            <version>8.0.26</version>--></dependency>

编写db.properties配置文件

properties

  #key=valuedb.driverClassName=com.mysql.jdbc.Driverdb.url=jdbc:mysql://localhost:3306/db220106db.username=rootdb.password=root

编写applicationContext.xml相关代码

  <!--    加载外部属性文件db.properties--><context:property-placeholder location="classpath:db.properties"></context:property-placeholder><!--    装配数据源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${db.driverClassName}"></property><property name="url" value="${db.url}"></property><property name="username" value="${db.username}"></property><property name="password" value="${db.password}"></property></bean>

测试

@Testpublic void testDruidDataSource() throws Exception{//获取容器对象ApplicationContext ioc =new ClassPathXmlApplicationContext("applicationContext_druid.xml");DruidDataSource dataSource = ioc.getBean("dataSource", DruidDataSource.class);System.out.println("dataSource = " + dataSource);DruidPooledConnection connection = dataSource.getConnection();System.out.println("connection = " + connection);}

6、Spring中FactoryBean

6.1 Spring中两种bean

一种是普通bean:

普通bean是指在Spring容器中以普通的方式配置和管理的bean。这些bean通常是通过在XML配置文件或Java配置类中定义并注册的,它们的创建和初始化由Spring容器负责。

另一种是工厂bean【FactoryBean】:

工厂bean是一种特殊的bean,它实现了org.springframework.beans.factory.FactoryBean接口。与普通bean不同,工厂bean负责创建其他bean实例,允许程序员在bean的创建过程中进行参数化或自定义。使用工厂bean可以更灵活地控制bean的创建逻辑和初始化过程。

作用:如需我们程序员参数到bean的创建时,使用FactoryBean

6.2 FactoryBean使用步骤

实现FactoryBean接口:创建一个类并实现FactoryBean接口,该接口要求实现getObject()方法来返回所创建的bean实例,并可选择实现getObjectType()方法来指定工厂bean所创建的对象类型。

重写方法【三个】:在实现FactoryBean接口的类中,需要重写getObject()方法来指定如何创建所需的bean实例。可选地,也可以重写getObjectType()方法来提供所创建的bean的类型。

装配工厂bean:将实现了FactoryBean接口的类配置到Spring容器中,可以通过XML配置文件或Java配置类进行装配。

测试

示例代码:

示例代码:

import org.springframework.beans.factory.FactoryBean;// 实现FactoryBean接口
public class MyBeanFactory implements FactoryBean<MyBean> {// 重写getObject()方法,指定创建bean的逻辑@Overridepublic MyBean getObject() throws Exception {// 这里可以根据需要进行一些自定义的逻辑,然后创建并返回所需的bean实例return new MyBean();}// 可选地重写getObjectType()方法,指定所创建的bean的类型@Overridepublic Class<?> getObjectType() {return MyBean.class;}
}

在Spring配置文件中装配工厂bean:

<bean id="myBeanFactory" class="com.example.MyBeanFactory"/>

在测试代码中获取并使用工厂bean:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {// 加载Spring配置文件ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");// 获取工厂bean实例MyBean myBean = context.getBean("myBeanFactory", MyBean.class);// 使用工厂创建的bean实例myBean.doSomething();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/10665.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

VUE使用饿了么的上传组件时实现图片预览

创作灵感 最近在写项目时&#xff0c;遇到了上传头像的需求&#xff0c;我使用的是element组件中的upload组件。但是在使用时&#xff0c;我需要实现预览、手动上传头像等功能。然而在使用饿了么组件时&#xff0c;这些功能还是需要我们自己去手动实现的&#xff0c;在手动实现…

Linux makefile进度条

语法 在依赖方法前面加上就不会显示这一行的命令 注意 1.make 会在当前目录下找名为“makefile” 或者 “Makefile” 的文件 2.为了生成第一依赖文件&#xff0c;如果依赖文件列表有文件不存在&#xff0c;则会到下面的依赖关系中查找 3..PHONY修饰的依赖文件总是被执行的 …

操作系统原理与系统——实验十三多道批处理作业调度(作业可移动)

关键代码 #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct data{int hour;//当前小时int min;//当前分钟 }time; struct node{char name[20];//进程名time arrive;//到达就绪队列时间int zx;//执行时间(预期时间)int size;int ta…

Polygon市值机器人

随着区块链技术的蓬勃发展和数字货币市场的日益繁荣&#xff0c;投资者们对于如何精准把握市场动态、实现资产稳健增长的需求愈发迫切。在这个背景下&#xff08;市值管理飞//机//aishutuyu&#xff09;&#xff0c;Polygon市值机器人应运而生&#xff0c;作为一款基于Polygon公…

timerfd加epoll封装定时器

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 1、用timerfd加epoll封装定时器的优点2、代码实现 1、用timerfd加epoll封装定时器的优点 定时器为什么需要timerfd 在设计定时器时&#xff0c;我们首先想到的就是…

【SpringBoot】Redis Lua脚本实战指南:简单高效的构建分布式多命令原子操作、分布式锁

文章目录 一.Lua脚本1.Lua特性2.Lua优势 二.Lua语法1.注释2.变量3.数据类型&#xff1a;3.1.基本类型3.2.对象类型&#xff1a;表&#xff08;table&#xff09; 4.控制结构&#xff1a;4.1.条件语句: 使用if、else和elseif来实现条件分支。4.2.循环结构&#xff1a;Lua支持for…

感知机和神经网络

引入 什么是神经网络&#xff1f; 我们今天学习的神经网络&#xff0c;不是人或动物的神经网络&#xff0c;但是又是模仿人和动物的神经网络而定制的神经系统&#xff0c;特别是大脑和神经中枢&#xff0c;定制的系统是一种数学模型或计算机模型&#xff0c;神经网络由大量的人…

图像处理:图像噪声添加

文章目录 前言一、高斯噪声二、椒盐噪声三、泊松噪声四、斑点噪声五、指数噪声六、均匀噪声总结 前言 本文主要介绍几种添加图像噪声的方法&#xff0c;用于数据增强等操作。 以下图为例。 一、高斯噪声 高斯噪声就是给图片添加一个服从高斯分布的噪声&#xff0c;可以通过调…

vLLM初探

vLLM是伯克利大学LMSYS组织开源的大语言模型高速推理框架&#xff0c;旨在极大地提升实时场景下的语言模型服务的吞吐与内存使用效率。vLLM是一个快速且易于使用的库&#xff0c;用于 LLM 推理和服务&#xff0c;可以和HuggingFace 无缝集成。vLLM利用了全新的注意力算法「Page…

Python+PySpark数据计算

1、map算子 对RDD内的元素进行逐个处理&#xff0c;并返回一个新的RDD&#xff0c;可以使用lambda以及链式编程&#xff0c;简化代码。 注意&#xff1a;再python中的lambda只能有行&#xff0c;如果有多行&#xff0c;要写成外部函数&#xff1b;&#xff08;T&#xff09;-&…

train_gpt2_fp32.cu - cudaCheck

源码 // CUDA error checking void cudaCheck(cudaError_t error, const char *file, int line) {if (error ! cudaSuccess) {printf("[CUDA ERROR] at file %s:%d:\n%s\n", file, line,cudaGetErrorString(error));exit(EXIT_FAILURE);} }; 解释 该函数用于检查CU…

无人机路径规划:基于鲸鱼优化算法WOA的复杂城市地形下无人机避障三维航迹规划,可以修改障碍物及起始点(Matlab代码)

一、部分代码 close all clear clc rng(default); %% 载入数据 data.S[50,950,12]; %起点位置 横坐标与纵坐标需为50的倍数 data.E[950,50,1]; %终点点位置 横坐标与纵坐标需为50的倍数 data.Obstaclexlsread(data1.xls); data.numObstacleslength(data.Obstacle(:,1)); …

TypeError: can only concatenate str (not “int“) to str

TypeError: can only concatenate str (not "int") to str a 窗前明月光&#xff0c;疑是地上霜。举头望明月&#xff0c;低头思故乡。 print(str_len len(str_text) : len(a)) 试图打印出字符串 a 的长度&#xff0c;但是在 Python 中拼接字符串和整数需要使用字符…

【微服务】spring aop实现接口参数变更前后对比和日志记录

目录 一、前言 二、spring aop概述 2.1 什么是spring aop 2.2 spring aop特点 2.3 spring aop应用场景 三、spring aop处理通用日志场景 3.1 系统日志类型 3.2 微服务场景下通用日志记录解决方案 3.2.1 手动记录 3.2.2 异步队列es 3.2.3 使用过滤器或拦截器 3.2.4 使…

triton编译学习

一 流程 Triton-MLIR: 从DSL到PTX - 知乎 (zhihu.com)https://zhuanlan.zhihu.com/p/671434808Superjomns blog | OpenAI/Triton MLIR 迁移工作简介https://superjom

基于STM32单片机的环境监测系统设计与实现

基于STM32单片机的环境监测系统设计与实现 摘要 随着环境污染和室内空气质量问题的日益严重&#xff0c;环境监测系统的应用变得尤为重要。本文设计并实现了一种基于STM32单片机的环境监测系统&#xff0c;该系统能够实时监测并显示室内环境的温湿度、甲醛浓度以及二氧化碳浓…

新iPadPro是怎样成为苹果史上最薄产品的|Meta发布AI广告工具全家桶| “碾碎一切”,苹果新广告片引争议|生成式AI,苹果倾巢出动

Remini走红背后&#xff1a;AI生图会是第一个超级应用吗&#xff1f;新iPadPro是怎样成为苹果史上最薄产品的生成式AI&#xff0c;苹果倾巢出动Meta发布AI广告工具全家桶&#xff0c;图像文本一键生成解放打工人苹果新iPadPro出货量或达500万台&#xff0c;成中尺寸OLED发展关键…

8、QT——QLabel使用小记2

前言&#xff1a;记录开发过程中QLabel的使用&#xff0c;持续更新ing... 开发平台&#xff1a;Win10 64位 开发环境&#xff1a;Qt Creator 13.0.0 构建环境&#xff1a;Qt 5.15.2 MSVC2019 64位 一、基本属性 技巧&#xff1a;对于Qlabel这类控件的属性有一些共同的特点&am…

QToolButton的特殊使用

QToolButton的特殊使用 介绍通过QSS取消点击时的凹陷效果点击时的凹陷效果通过QSS取消点击时的凹陷效果 介绍 该篇文章记录QToolButton使用过程中的特殊用法。 通过QSS取消点击时的凹陷效果 点击时的凹陷效果 通过QSS取消点击时的凹陷效果 #include <QToolButton> #i…

【深耕 Python】Quantum Computing 量子计算机(5)量子物理概念(二)

写在前面 往期量子计算机博客&#xff1a; 【深耕 Python】Quantum Computing 量子计算机&#xff08;1&#xff09;图像绘制基础 【深耕 Python】Quantum Computing 量子计算机&#xff08;2&#xff09;绘制电子运动平面波 【深耕 Python】Quantum Computing 量子计算机&…