Spring的注入
目录
一、Spring的概念
二、各种数据类型的注入
(1)studentService
(2)applicationContext.xml(Sring核心配置文件)
(3)测试
三、注入null或者empty类型的数据
(1)UserService
(2)applicationContext.xml(Spring核心配置文件)
(3)测试
一、Spring的概念
Spring是一个开源框架,为简化企业级开发而生。它以IOC(控制反转)和AOP(面向切面)为思想内核,提供了控制层SpringMVC、数据层SpringData、服务层事务管理等众多技术,并可以整合众多第三方框架。
Spring将很多复杂的代码变得优雅简洁,有效的降低代码的耦合度,极大的方便项目的后期维护、升级和扩展。
Spring官网地址:Spring | Home
这里的IOC控制反转的意思是:本来我们创建对象是需要自己创建的,使用new,但是这有很大的缺点的,如下:
(1)浪费资源:StudentService调用方法时即会创建一个对象,如果不断调用方法则会创建大量StudentDao对象。
(2)代码耦合度高:假设随着开发,我们创建了StudentDao另一个更加完善的实现类StudentDaoImpl2,如果在StudentService中想使用StudentDaoImpl2,则必须修改源码。
所以IOC其实就是我们将创建对象的权利交给框架,让框架帮我们创建对象
二、各种数据类型的注入
什么事注入呢,这里我们讲的注入其实就是在你创建一个对象后,你是不是要给他赋值呢,所以你可以简单地理解成,注入就是给创建的对象赋值。
(1)studentService
package com.gq.pojo;import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set;public class StudentService {//数组练习private student[] arrayStudent;//list练习private List<student> studentList;//set练习private Set<student> studentSet;//Map练习private Map<String,student> studentMap;//Properties属性练习private Properties properties;//各种各样的输出方法来判断是哪种类别的public void arrayTest(){System.out.println("array练习");System.out.println(this.getArrayStudent());}public void ListTest(){System.out.println("List练习");System.out.println(this.getStudentList());}public void setTest(){System.out.println("Set练习");System.out.println(this.getStudentSet());}public void mapTest(){System.out.println("map练习");System.out.println(this.getStudentMap());}public void proTest(){System.out.println("properties练习");System.out.println(this.getProperties());}public student[] getArrayStudent() {return arrayStudent;}public void setArrayStudent(student[] arrayStudent) {this.arrayStudent = arrayStudent;}public List<student> getStudentList() {return studentList;}public void setStudentList(List<student> studentList) {this.studentList = studentList;}public Set<student> getStudentSet() {return studentSet;}public void setStudentSet(Set<student> studentSet) {this.studentSet = studentSet;}public Map<String, student> getStudentMap() {return studentMap;}public void setStudentMap(Map<String, student> studentMap) {this.studentMap = studentMap;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;} }
(2)applicationContext.xml(Sring核心配置文件)
<?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"><!--配置不同的JavaBean,交由 IOC 容器进行管理--><!--注入简单数据类型--><bean name="student1" class="com.gq.pojo.student"><property name="id" value="1"></property><property name="name" value="zhansgan"></property></bean><bean name="student2" class="com.gq.pojo.student"><property name="name" value="lisi"></property><property name="id" value="2"></property></bean><bean name="StudentService" class="com.gq.pojo.StudentService"><!-- 注入数组类型的数据--><property name="arrayStudent"><list><ref bean="student1"></ref><ref bean="student2"></ref><bean class="com.gq.pojo.student"><property name="id" value="3"></property><property name="name" value="wangwu"></property></bean></list></property><!--注入List类型的数据 --><property name="studentList"><list><ref bean="student1"/><ref bean="student2"/><bean class="com.gq.pojo.student"><property name="name" value="uiui"></property><property name="id" value="11"></property></bean></list></property><!--注入Set数据--><property name="studentSet"><set><ref bean="student1"></ref><ref bean="student2"></ref><bean class="com.gq.pojo.student"><property name="id" value="21"></property><property name="name" value="setset"></property></bean></set></property><!--注入map数据--><property name="studentMap"><map><entry key="map1" value-ref="student1"></entry><entry key="map2" value-ref="student2"></entry><entry key="map3"><bean class="com.gq.pojo.student"><property name="name" value="maptest"></property><property name="id" value="33"></property></bean></entry></map></property><!--注入Properties类型数据--><property name="properties"><props><prop key="prop1">i am 1</prop><prop key="prop2">i am 2</prop></props></property></bean></beans>
(3)测试
@Testpublic void t2(){ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");StudentService service=context.getBean("StudentService",StudentService.class);service.arrayTest();service.ListTest();service.mapTest();service.setTest();service.proTest();}
关于乱码问题,只需要将编码格修改成UTF-8就可以了,具体的修改我就不多说了,大家也可以自己查找资料,不是很难的。
三、注入null或者empty类型的数据
(1)UserService
public class UserService {private List<String> list;public void test(){System.out.println("i am listTest");}public UserService(List<String> list) {this.list = list;}public UserService() {}public List<String> getList() {return list;}public void setList(List<String> list) {this.list = list;} }
(2)applicationContext.xml(Spring核心配置文件)
<bean name="UserService" class="com.gq.pojo.UserService"><property name="list"><list><value>i am first persion</value><value></value><!--空值--><null></null><!--空字符串--><value>i am last persion</value></list></property></bean>
(3)测试
@Testpublic void t3(){ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService=context.getBean("UserService",UserService.class);List<String> list = userService.getList();System.out.println(list);}
相关文章:
Spring的注入
目录 一、Spring的概念 二、各种数据类型的注入 (1)studentService (2)applicationContext.xml(Sring核心配置文件) (3)测试 三、注入null或者empty类型的数据 (1…...
Linux-Docker的基础命令和部署code-server
1.安装docker 1.安装需要的安装包 yum install -y yum-utils2.设置镜像仓库 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo3.安装docker yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin do…...
微信小程序授权登陆 getUserProfile
目录 前言 步骤: 示例代码: 获取用户信息的接口变化历史: 注意事项: 前言 在微信小程序中,你可以使用 getUserProfile 接口来获取用户的个人信息,并进行授权登录。以下是使用 getUserProfile 的步骤: 小程序发了…...
深度学习AI识别人脸年龄
以下链接来自 落痕的寒假 GitHub - luohenyueji/OpenCV-Practical-Exercise: OpenCV practical exercise GitHub - luohenyueji/OpenCV-Practical-Exercise: OpenCV practical exercise import cv2 as cv import time import argparsedef getFaceBox(net, frame, conf_thresh…...
兔队线段树维护后缀非严格递增子序列的哈希值:CCPC2023深圳K
https://vjudge.net/contest/594134#problem/K 场上想到如果两个序列的后缀非严格递增子序列相同则平局,但不知道怎么维护 发现不用输出谁赢,只用判断是否平局,所以肯定是判断两个东西是否相等 然后如果单纯维护后缀非严格递增子序列&#…...
Django框架FAQ
文章目录 问题1:Django数据库恢复问题2:null和blank的区别3.报错 django.db.utils.IntegrityError: (1062, “Duplicate entry ‘‘ for key ‘mobile‘“)4.报错 Refused to display ‘url‘ in a frame because it set ‘X-Frame-Options‘ to deny5.报错 RuntimeError: cryp…...
chinese-hanfu-sd1.5-v30 训练日记
chinese-hanfu-sd1.5-v30 训练日记 训练数据: found directory /dataset/train_dataset2/chinese-hanfu-sd1-v30/img/10_ohxm woman contains 2465 image files found directory /dataset/train_dataset2/chinese-hanfu-sd1-v30/img/10_khs woman contains 8220 im…...
【Redis系列】Redis的核心命令(上)
哈喽,大家好,我是小浪。那么上篇博客教会了大家如何在Linux上安装Redis,那么本篇博客就要正式开始学习Redis啦,跟着俺的随笔往下看~ 1、启动Redis 那么如何启动Redis呢?最常用的是以下这个命令: redis-cl…...
鸿蒙 API9 接入 Crypto库
鸿蒙 API9 接入 Crypto库 开发环境 API9。 参考文档 之前研究了半天鸿蒙自身支持的算法库,只能说集成起来还是比较麻烦的,不如开箱即用的npm crypto好用。不过之前也没想到三方库会这么快的适配鸿蒙,毕竟小程序都多少年了,各种…...
Halcon WPF 开发学习笔记(2):Halcon导出c#脚本和WPF初步开发
文章目录 前言HalconC#教学简单说明如何二开机器视觉如何二次开发Halcon导出Halcon脚本新建WPF项目,导入Halcon脚本和Halcon命名空间 前言 我目前搜了一下我了解的机器视觉软件,有如下特点 优点缺点兼容性教学视频(B站前三播放量)OpenCV开源࿰…...
红队专题-从零开始VC++C/S远程控制软件RAT-MFC-超级终端
红队专题 招募六边形战士队员[16]超级终端(1) 招募六边形战士队员 一起学习 代码审计、安全开发、web攻防、逆向等。。。 私信联系 [16]超级终端(1) 服务端 — 本地打开cmd — 接收命令 — 执行 — 发送回显 客户端 — 远端发送命令 — 接收回显 发送开启cmd命令 --- 接受…...
ROS机器人毕业论文数量井喷-数据日期23年11月13日
背景 ROS机器人论文数量在近3年井喷发展,仅硕士论文知网数据库可查阅就已经达到2264篇,实际相关从业者远远远大于这个数值。 按日期排序,每页20篇,23年还未结束,检索本身也不一定完备,就超过200。 相关从业…...
BIO、NIO、AIO之间有什么区别
文章目录 BIO优缺点示例代码 NIO优缺点示例代码 AIO优缺点示例代码 总结 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。 BIO、NIO和AIO是Java编程语言中用于处理输入输出(IO…...
强烈建议linux中nvidia 545.29驱动不要升
我之前一直用终端连接我的工作站(系统是arch rolling状态),结果昨天回家难得想试试545驱动下的效果。结果一用chrome播放视频就卡,甚至后面进Login界面也会卡住鼠标。 折腾了一晚上用 $sudo downgrade nvidia nvidia-prime nvid…...
css格式和样式选择器-学习记录
文章目录 一、css代码代码格式1、内联格式(不推荐)2、内部格式(不推荐)3、外部格式 (推荐) 二、css样式选择器1、类型选择器2、类选择器(推荐)3、id选择器 三、样式表的组合1、Multi…...
【Python】Matplotlib-多张图像的显示
一,情景描述 大家在写论文或者实验报告的时候,经常会放多张图片或数据图像在一起形成对比。比如,我现在有一张经过椒盐噪声处理的图像,现在进行三种滤波,分别是均值,高斯,中值滤波,…...
数据库 关系数据理论
问题 数据冗余更新异常插入异常删除异常 一个好的模式应当不会发生插入异常、删除异常和更新异常,数据冗余应尽可能少 数据依赖 定义:一个关系内部属性与属性之间的一种约束关系(该约束关系是通过属性间值的相等与否体现出来数据间相关联…...
网易数帆:云原生向左,低代码向右
网易数帆,前身是网易杭州研究院于2016年孵化的网易云,历经7载探索与沉淀,如今已进化成为覆盖云原生、低代码、大数据和人工智能四大技术赛道的数智化服务提供商,服务于金融、央国企、能源、制造等领域300余家头部企业。 近日&…...
上线亚马逊出口美国审核CPC认证标准内容解析
儿童玩具产品、母婴产品出口美国都需要CPC认证证书和CPSIA报告进行过关清关。 一、什么是CPC认证? CPC认证是Children’sProduct Certificate的英文简称,CPC证书就类似于国内的质检报告,在通过相关检测,出具报告后同时可出具的一…...
SharePoint 的 Web Parts 是什么
Web Parts 可以说是微软 SharePoint 的基础组件。 根据微软自己的描述,Web Parts 是 SharePoint 对内容进行构建的基础,可以想想成一块一块的砖块。 我们需要使用这些砖块来完成一个页面的构建。 我们可以利用 Web Parts 在 SharePoint 中添加文本&am…...
uni-app学习笔记二十二---使用vite.config.js全局导入常用依赖
在前面的练习中,每个页面需要使用ref,onShow等生命周期钩子函数时都需要像下面这样导入 import {onMounted, ref} from "vue" 如果不想每个页面都导入,需要使用node.js命令npm安装unplugin-auto-import npm install unplugin-au…...
django filter 统计数量 按属性去重
在Django中,如果你想要根据某个属性对查询集进行去重并统计数量,你可以使用values()方法配合annotate()方法来实现。这里有两种常见的方法来完成这个需求: 方法1:使用annotate()和Count 假设你有一个模型Item,并且你想…...
Vue2 第一节_Vue2上手_插值表达式{{}}_访问数据和修改数据_Vue开发者工具
文章目录 1.Vue2上手-如何创建一个Vue实例,进行初始化渲染2. 插值表达式{{}}3. 访问数据和修改数据4. vue响应式5. Vue开发者工具--方便调试 1.Vue2上手-如何创建一个Vue实例,进行初始化渲染 准备容器引包创建Vue实例 new Vue()指定配置项 ->渲染数据 准备一个容器,例如: …...
【单片机期末】单片机系统设计
主要内容:系统状态机,系统时基,系统需求分析,系统构建,系统状态流图 一、题目要求 二、绘制系统状态流图 题目:根据上述描述绘制系统状态流图,注明状态转移条件及方向。 三、利用定时器产生时…...
leetcodeSQL解题:3564. 季节性销售分析
leetcodeSQL解题:3564. 季节性销售分析 题目: 表:sales ---------------------- | Column Name | Type | ---------------------- | sale_id | int | | product_id | int | | sale_date | date | | quantity | int | | price | decimal | -…...
大数据学习(132)-HIve数据分析
🍋🍋大数据学习🍋🍋 🔥系列专栏: 👑哲学语录: 用力所能及,改变世界。 💖如果觉得博主的文章还不错的话,请点赞👍收藏⭐️留言Ǵ…...
html-<abbr> 缩写或首字母缩略词
定义与作用 <abbr> 标签用于表示缩写或首字母缩略词,它可以帮助用户更好地理解缩写的含义,尤其是对于那些不熟悉该缩写的用户。 title 属性的内容提供了缩写的详细说明。当用户将鼠标悬停在缩写上时,会显示一个提示框。 示例&#x…...
Python 包管理器 uv 介绍
Python 包管理器 uv 全面介绍 uv 是由 Astral(热门工具 Ruff 的开发者)推出的下一代高性能 Python 包管理器和构建工具,用 Rust 编写。它旨在解决传统工具(如 pip、virtualenv、pip-tools)的性能瓶颈,同时…...
基于TurtleBot3在Gazebo地图实现机器人远程控制
1. TurtleBot3环境配置 # 下载TurtleBot3核心包 mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src git clone -b noetic-devel https://github.com/ROBOTIS-GIT/turtlebot3.git git clone -b noetic https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git git clone -b noetic-dev…...
使用Spring AI和MCP协议构建图片搜索服务
目录 使用Spring AI和MCP协议构建图片搜索服务 引言 技术栈概览 项目架构设计 架构图 服务端开发 1. 创建Spring Boot项目 2. 实现图片搜索工具 3. 配置传输模式 Stdio模式(本地调用) SSE模式(远程调用) 4. 注册工具提…...


