【狂神】Spring5笔记(1-9)
目录
首页:
1.Spring
1.1 简介
1.2 优点
2.IOC理论推导
3.IOC本质
4.HelloSpring
ERROR
5.IOC创建对象方式
5.1、无参构造 这个是默认的
5.2、有参构造
6.Spring配置说明
6.1、别名
6.2、Bean的配置
6.3、import
7.DL依赖注入环境
7.1 构造器注入
7.2 Set方式注入
7.3 案例(代码)
7.3.1.Student类
7.3.2 Address类
7.3.3 beans.xml
7.3.4 Mytest4类
首页:
我是跟着狂神老师的视频内容来整理的笔记,不得不说,真的收获颇丰,希望这篇笔记能够帮到你。
..... (¯`v´¯)♥ .......•.¸.•´ ....¸.•´ ... ( ☻/ /▌♥♥ / \ ♥♥
1.Spring
1.1 简介
由Rod Johnson创建,雏形是interface21框架。理念是:使现有的技术更加容易使用,本身是一个大杂烩,整合了现有的技术框架!
- SSH: Struct2+Sppring+Hibernate
- SSM:SpringMVC+Spring+Mybatis
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.0.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.11</version>
</dependency>
1.2 优点

2.IOC理论推导
1.UserDao接口

2.UserDaolmpl实现类

3.UserService业务接口

4.UserServicelmpl业务实现类

上面的四个类是我们写项目时的传统的写法。主要就是在实现类中实现功能,最后在业务实现类中最终实现。
通过在Servicelmpl中创建一个新的的UserDao对象,是可以实现方法的调用的,但是当后面所调用的类变得越来越多以后,这种方法就不太适合了。比如说,多了很多类似于UserDaolmpl的实现类,但是想要调用他们的话,就必须在其对应的Service中进行更改,太过于麻烦,耦合性太强。

解决方法:
public class UserServicelmpl implements UserService{private UserDao userDao;//利用set进行动态实现值的注入public void setUserDao(UserDao userDao){this.userDao=userDao;}public void getUser() {userDao.getUser();}
}
实现类:

3.IOC本质


简言之,就是把控制权交给了用户而不是程序员,我们可以通过所选择的来呈现不同的页面或者说是表现方式。用户的选择变多了。
4.HelloSpring
这是一个视频里的小案例,旨在加深对bean的理解。beans.xml的正规名叫做applicationContext.xml,到后面可以用Import进行导入。
代码:
//1.Hello
package org.example;
public class Hello {private String str;public String getStr() {return str;}public void setStr(String str) {this.str = str;}@Overridepublic String toString() {return "Hello{"+"str="+str+'\''+'}';}
}
//2.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 这里的name的值就是类中变量名 --><bean id="hello" class="org.example.Hello"><property name="str" value="spring"></property></bean>
</beans>//3.实现测试类MyTest
import org.example.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {//获取Spring的上下文对象ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");Hello hello = (Hello) context.getBean("hello"); //这里的hello就是创建对象的变量名System.out.println(hello.toString());}
}


idea中自动生成返回对象的快捷键
ctr+alt+v
ERROR
1.

原因:JDK版本过低造成,要大于1.8,我用的2.0
5.IOC创建对象方式
5.1、无参构造 这个是默认的
<bean id="user" class="org.example.pojo.User"> <property name="name" value="张总"></property> </bean>
5.2、有参构造
- 通过下标获得
<bean id="user" class="org.example.pojo.User"> <constructor-arg index="0" value="王总"/> </bean>
- 通过变量的类型获得,但不建议用,因为当变量名有很多时便不适用了
<bean id="user" class="org.example.pojo.User"> <constructor-arg type="java.lang.String" value="赵总"/> </bean>
- 通过变量名来获得
<bean id="user" class="org.example.pojo.User"> <constructor-arg name="name" value="李总"/> </bean>
6.Spring配置说明
6.1、别名
起别名,并不是覆盖原有的变量名

6.2、Bean的配置

6.3、import

7.DL依赖注入环境
7.1 构造器注入
前面已经说过了。
7.2 Set方式注入
- 依赖注入:Set注入!
1.依赖:bean对象的创建依赖于容器spring
2.注入:bean对象中的所有属性,由容器来注入
7.3 案例(代码)
一个比较全的案例,包括了String,类,数组,list集合,Map,Set,Null,Properties。
代码如下:
7.3.1.Student类
//1.Student
package org.example;import java.util.*;public class Student {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; //不是很理解这个的意思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> getHobbys() {return hobbys;}public void setHobbys(List<String> hobbys) {this.hobbys = hobbys;}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;}@Overridepublic String toString() {return "Student{"+"name="+name+'\''+",address="+address.toString()+",books="+ Arrays.toString(books)+",hobbys="+hobbys+",card="+card+",games="+games+",wife="+wife+'\''+",info="+info+'}';}
}
7.3.2 Address类
//2.Address类
package org.example;public class Address {private String address;public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return address;}
}
7.3.3 beans.xml
//3.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" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"><bean id="address" class="org.example.Address"><property name="address"><value>西安</value></property></bean><bean id="student" class="org.example.Student"><property name="name" value="秦三"/><property name="address" ref="address"/><property name="books"><array><value>语文</value><value>数学</value><value>英语</value><value>化学</value></array></property><property name="hobbys"><list><value>篮球</value><value>足球</value><value>台球</value></list></property><property name="card"><map><entry key="身份证" value="1111111111111"/><entry key="银行卡" value="2222222222222"/></map></property><property name="games"><set><value>LOL</value><value>COC</value></set></property><property name="wife"><null/></property><property name="info"><props><prop key="学号">12345</prop><prop key="性别">男</prop><prop key="姓名">张三</prop></props></property></bean><!-- more bean definitions go here --></beans>
7.3.4 Mytest4类
//4.MyTest测试类
import org.example.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest4 {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");Student student = (Student) context.getBean("student");System.out.println(student.toString());}
}
最后,祝大家身体健康,学习快乐,天天向上!
相关文章:
【狂神】Spring5笔记(1-9)
目录 首页: 1.Spring 1.1 简介 1.2 优点 2.IOC理论推导 3.IOC本质 4.HelloSpring ERROR 5.IOC创建对象方式 5.1、无参构造 这个是默认的 5.2、有参构造 6.Spring配置说明 6.1、别名 6.2、Bean的配置 6.3、import 7.DL依赖注入环境 7.1 构造器注入 …...
Redis——急速安装并设置自启(CentOS)
现状 对于开发人员来说,部署服务器环境并不是一个高频操作。所以就导致绝大部分开发人员不会花太多时间去学习记忆,而是直接百度(有一些同学可能连链接都懒得收藏)。所以到了部署环境的时候就头疼,甚至是抗拒。除了每次…...
C++中使用while循环
C中使用while循环 C关键字 while 可帮助您完成程序中 goto 语句完成的工作,但更优雅。 while 循环的语法如下: while(expression) {// Expression evaluates to trueStatementBlock; }只要 expression 为 true, 就将反复执行该语句块。因此…...
视频融合平台EasyCVR视频汇聚平台关于小区高空坠物安全实施应用方案设计
近年来,随着我国城市化建设的推进,高楼大厦越来越多,高空坠物导致的伤害也屡见不鲜,严重的影响到人们的生命安全。像在日常生活中一些不起眼的小东西如烟头、鸡蛋、果核、易拉罐,看似伤害不大,但只要降落的…...
IBM安全发布《2023年数据泄露成本报告》,数据泄露成本创新高
近日,IBM安全发布了《2023年数据泄露成本报告》,该报告针对全球553个组织所经历的数据泄露事件进行深入分析研究,探讨数据泄露的根本原因,以及能够减少数据泄露的技术手段。 根据报告显示,2023年数据泄露的全球平均成…...
python爬虫—requests
一、安装 pip install requests 二、基本使用 1、基本使用 类型 : models.Response r.text : 获取网站源码 r.encoding :访问或定制编码方式 r.url :获取请求的 url r.content :响应的字节类型 r.status_code :响应…...
应用案例 | 3D视觉引导解决方案汽车零部件上下料
Part.1 行业背景 三维视觉引导技术在国内外汽车零部件领域得到了广泛应用。随着汽车制造业的不断发展和创新,对于零部件的加工和装配要求越来越高,而三维视觉引导技术能够帮助企业实现更精确、更高效的零部件上下料过程。 纵览国外,部分汽车…...
const {}解构赋值
定义:ES6允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring)。 解构赋值的基本规则:只要等号右边不是对象或数组,就先将其转换为对象。由于undefi…...
一篇文章带你了解-selenium工作原理详解
前言 Selenium是一个用于Web应用程序自动化测试工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,…...
H5 + C3基础(八)(3d转换 位移 旋转)
3d转换 位移 & 旋转 定义位移透视 perspective透视和Z轴使用场景 旋转子元素开启3d视图示例 小结 定义 3d转换在2d转换中增加了一个z轴,垂直于屏幕,向外为正,向内为负。 位移 在2d位移的基础上增加了 translateZ(z); 在Z轴上的位移 t…...
PyQt6 GUI界面设计和Nuitka包生成exe程序(全笔记)
PyQt6 GUI界面设计和Nuitka包,生成exe程序全笔记 目录一、PyQt6包安装1.1 进行环境配置和安装1.2 检查包是否安装成功。1.3 运行desinger.exe二、GUI界面设计,写程序,并能运行成功。三、Nuitka打包生成exe程序3.1 做Nuitka安装准备工作(1)安装C编译器,设置环境变量3.2 配…...
学习网络编程No.5【TCP套接字通信】
引言: 北京时间:2023/8/25/15:52,昨天刚把耗时3天左右的文章更新,充分说明我们这几天并不是在摆烂中度过,而是在为了更文不懈奋斗,历时这么多天主要是因为该部分知识比较陌生,所以需要我们花费…...
常用的时间段的时间戳
获取 昨天这个时间的时间戳 Calendar calendar Calendar.getInstance(); //当前时间calendar.add(Calendar.DAY_OF_YEAR,-1); Long dd calendar.getTime().getTime()/1000;System.out.println(dd);计算今天0点的时间戳 Long time System.currentTimeMillis(); //当前…...
博客系统后台控制层接口编写
BlogColumnCon CrossOrigin RequestMapping("/back/blogColumn") RestController public class BlogColumnCon {Autowiredprivate BlogColumnService blogColumnService;/*** 新增** param blogColumn* return*/PostMapping("/add")public BaseResult add…...
生成 MySQL 删除索引、创建索引、分析表的 SQL 语句
目录 1. 生成删除索引 SQL 语句 2. 生成创建索引的 SQL 语句 3. 生成分析表的 SQL 语句 1. 生成删除索引 SQL 语句 mysql -uwxy -p12345 -S /data/18253/mysqldata/mysql.sock -e " select concat(alter table \,table_schema,\.\,table_name,\ ,drop_index,;)from ( …...
mongodb建用户
玛德折腾了2个小时,、mongodb 建用户。艹 [rootk8-master mongodb]# cat docker-compose.yaml version: 2 services: mongodb: container_name: mongodb_2.0 image: mongo:4.4 restart: always environment: TZ: Asia/Shanghai MONGO_INITDB_ROOT_USERNAME: admin M…...
无门槛访问ChatGPT升级版-数据指北AI
大家好,我是脚丫先生 (o^^o) 给小伙伴们介绍ChatGPT升级版不需要任何门槛,不需要单独搞账号,只要邮箱登录的方式,即可访问平台,以用户体验为首要,让所有人都能无门槛的使用目前市面上最强大的AI智能聊天&a…...
前端需要学习哪些技术?
前端工程师岗位缺口一直很大,符合岗位要求的人越来越少,所以学习前端的同学要注意,一定要把技能学到扎实,做有含金量的项目,这样在找工作的时候展现更大的优势。 缺人才,又薪资高,那么怎样才能…...
详解排序算法(附带Java/Python/Js源码)
冒泡算法 依次比较两个相邻的子元素,如果他们的顺序错误就把他们交换过来,重复地进行此过程直到没有相邻元素需要交换,即完成整个冒泡,时间复杂度。 比较相邻的元素。如果第一个比第二个大,就交换它们两个;…...
手写Mybatis:第8章-把反射用到出神入化
文章目录 一、目标:元对象反射类二、设计:元对象反射类三、实现:元对象反射类3.1 工程结构3.2 元对象反射类关系图3.3 反射调用者3.3.1 统一调用者接口3.3.2 方法调用者3.3.3 getter 调用者3.3.4 setter 调用者 3.4 属性命名和分解标记3.4.1 …...
AI-调查研究-01-正念冥想有用吗?对健康的影响及科学指南
点一下关注吧!!!非常感谢!!持续更新!!! 🚀 AI篇持续更新中!(长期更新) 目前2025年06月05日更新到: AI炼丹日志-28 - Aud…...
设计模式和设计原则回顾
设计模式和设计原则回顾 23种设计模式是设计原则的完美体现,设计原则设计原则是设计模式的理论基石, 设计模式 在经典的设计模式分类中(如《设计模式:可复用面向对象软件的基础》一书中),总共有23种设计模式,分为三大类: 一、创建型模式(5种) 1. 单例模式(Sing…...
突破不可导策略的训练难题:零阶优化与强化学习的深度嵌合
强化学习(Reinforcement Learning, RL)是工业领域智能控制的重要方法。它的基本原理是将最优控制问题建模为马尔可夫决策过程,然后使用强化学习的Actor-Critic机制(中文译作“知行互动”机制),逐步迭代求解…...
【JavaEE】-- HTTP
1. HTTP是什么? HTTP(全称为"超文本传输协议")是一种应用非常广泛的应用层协议,HTTP是基于TCP协议的一种应用层协议。 应用层协议:是计算机网络协议栈中最高层的协议,它定义了运行在不同主机上…...
盘古信息PCB行业解决方案:以全域场景重构,激活智造新未来
一、破局:PCB行业的时代之问 在数字经济蓬勃发展的浪潮中,PCB(印制电路板)作为 “电子产品之母”,其重要性愈发凸显。随着 5G、人工智能等新兴技术的加速渗透,PCB行业面临着前所未有的挑战与机遇。产品迭代…...
el-switch文字内置
el-switch文字内置 效果 vue <div style"color:#ffffff;font-size:14px;float:left;margin-bottom:5px;margin-right:5px;">自动加载</div> <el-switch v-model"value" active-color"#3E99FB" inactive-color"#DCDFE6"…...
剑指offer20_链表中环的入口节点
链表中环的入口节点 给定一个链表,若其中包含环,则输出环的入口节点。 若其中不包含环,则输出null。 数据范围 节点 val 值取值范围 [ 1 , 1000 ] [1,1000] [1,1000]。 节点 val 值各不相同。 链表长度 [ 0 , 500 ] [0,500] [0,500]。 …...
【决胜公务员考试】求职OMG——见面课测验1
2025最新版!!!6.8截至答题,大家注意呀! 博主码字不易点个关注吧,祝期末顺利~~ 1.单选题(2分) 下列说法错误的是:( B ) A.选调生属于公务员系统 B.公务员属于事业编 C.选调生有基层锻炼的要求 D…...
Java入门学习详细版(一)
大家好,Java 学习是一个系统学习的过程,核心原则就是“理论 实践 坚持”,并且需循序渐进,不可过于着急,本篇文章推出的这份详细入门学习资料将带大家从零基础开始,逐步掌握 Java 的核心概念和编程技能。 …...
JUC笔记(上)-复习 涉及死锁 volatile synchronized CAS 原子操作
一、上下文切换 即使单核CPU也可以进行多线程执行代码,CPU会给每个线程分配CPU时间片来实现这个机制。时间片非常短,所以CPU会不断地切换线程执行,从而让我们感觉多个线程是同时执行的。时间片一般是十几毫秒(ms)。通过时间片分配算法执行。…...
