[ Spring 框架 ] 框架搭建和属性赋值

目录

1. Spring定义:

(1). IOC( Inversion of Control):

(2). AOP (Aspect Oriented Programming):

(3)一站式:

2. spring搭建:

(1). 创建一个Maven项目

(2). 导入核心 jar包

(3). 编写 spring 配置文件

(4). 编写实体类,并生成set方法

(5). 在resource中加入spring核心依赖 

(6)获取对象,测试spring

3. xml配置方式属性赋值

4. 注解方式实现属性赋值

(1). 开启注解扫描

(2). 注解自己所创建的对象

5. spring注解标签分类

(1). 生成对象的注解

(2). 属性注入的注解


1. Spring定义:

    Spring是一个 轻量级 (核心功能的jar比较小,运行占的资源少),IOC 和 AOP 一站式的Java开发框架

官网地址: Spring | Home

(1). IOC( Inversion of Control):

控制反转,是一种编程思想,将对象的管理(创建 初始化 存储 销毁 添加额外的功能...)统一交给spring框架

正控:若要使用某个对象,需要自己去负责对象的创建

反控:若要使用某个对象,只需要从Spring框架中获取需要使用的对象,不关心对象的创建过程,而是把创建对象的控制权反转给了 Spring 框架.

       以前开发时,在哪需要对象,我们就在哪里new一个对象 ; 而现在,由于IOC容器(spring框架)负责对象的实例化、对象的初始化,对象和对象之间依赖关系、 对象的销毁、对外提供对象的查找等操作,即对象的整个生命周期都是由容器来控制. 我们就把创建对象都交给spring框架完成,自己不new对象,需要使用对象时,直接从spring框架中获取 .

(2). AOP (Aspect Oriented Programming):

      面向切面编程 ,是一种编程思想(是面向对象的补充)   可以使用代理对象,在不修改源代码的情况下,为目标方法添加额外的功能         

(3)一站式:

       涉及数据访问层,web层层,可以对项目中的对象进行管理,还提供AOP功能,便于功能的扩展,对后端的项目统一管理

spring项目体系结构:

2. spring搭建:

(1). 创建一个Maven项目

(2). 导入核心 jar包

<!-- spring-context -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>5.2.2.RELEASE</version>

</dependency>

(3). 编写 spring 配置文件

<?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">

<bean id="user" class="com.ff.spring.model.User"> </bean>

</beans>

(4). 编写实体类 Dao接口 和 Service 

例如

package com.ffyc.spring.model;import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;public class Student {private int no;private String name ;public Student(){System.out.println("这是无参的构造方法");}public int getNo() {return no;}public void setNo(int no) {this.no = no;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

(5). 在resource中加入spring核心依赖 

<bean id="student" class="com.ffyc.spring.model.Student"></bean>

<bean id="studentDao" class="com.ffyc.spring.dao.StudentDao"></bean>
<bean id="studentService" class="com.ffyc.spring.service.StudentService">

例如:

<?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"><!--1.配置: 在spring中配置我们的类,把我们的类交给spring管理--><bean id="student" class="com.ffyc.spring.model.Student" scope="prototype"></bean><!--生成对象的作用域: spring框架默认创建一个对象(scope="singleton"),例如dao service对象.如果需要创建多个对象时,需要添加配置scope="prototype" 例如模型类student --><bean id="studentDao" class="com.ffyc.spring.dao.StudentDao"></bean><bean id="studentService" class="com.ffyc.spring.service.StudentService"><!--创建对象,并为属性注入值--><!--方法一:用set方法为属性赋值(推荐)--><property name="studentDao" ref="studentDao"></property><property name="student" ref="student"></property><!--方法二: 用构造方法赋值 --><!-- <constructor-arg name="studentDao" ref="studentDao"></constructor-arg>--></bean></beans>

(6)测试spring

ApplicationContext  applicationContext  = new ClassPathXmlApplicationContext("application.xml");

StudentService studentService =applicationContext.getBean("studentService",StudentService.class);

例如:

package com.ffyc.spring.test;import com.ffyc.spring.model.Student;
import com.ffyc.spring.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test1 {public static void main(String[] args) {/*之前如何使用对象: 在哪需要,就在哪new对现在如果使用对象: 把我们的类配置给spring框架,当spring框架启动时就会为我们创建对象,需要时从spring框架里直接获取就行*///启动spring框架,获取配置文件 现在获取对象ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");Student student1 = applicationContext.getBean("student", Student.class);Student student2 = applicationContext.getBean("student", Student.class);System.out.println(student1);System.out.println(student2);StudentService studentService =applicationContext.getBean("studentService",StudentService.class);}
}

3. xml配置方式属性赋值

1. 定义:

        指Spring 创建对象的过程中,将对象依赖属性(简单值 集合 对象)通过配置设置给该对象

实现 IOC 需要 DI 做支持, 也可以简单理解为在创建对象时,为对象的属性赋值方式有两种:

(1). 通过set和get方法对属性赋值

(2). 通过构造方法为属性赋值

package com.ffyc.spring.service;import com.ffyc.spring.dao.StudentDao;
import com.ffyc.spring.model.Student;public class StudentService {StudentDao studentDao;Student student;/*  //方法二: 通过构造方法为属性赋值public StudentService(StudentDao studentDao, Student student) {this.studentDao = studentDao;this.student = student;}*/public  void save(){/*StudentDao studentDao = new StudentDao();Student student = new Student();studentDao.*/studentDao.saveStudent(student);}//方法二: 通过set和get方法对属性赋值public StudentDao getStudentDao() {return studentDao;}public void setStudentDao(StudentDao studentDao) {this.studentDao = studentDao;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}
}
<?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"><!--1.配置: 在spring中配置我们的类,把我们的类交给spring管理--><bean id="student" class="com.ffyc.spring.model.Student" scope="prototype"></bean><!--生成对象的作用域: spring框架默认创建一个对象(scope="singleton"),例如dao service对象.如果需要创建多个对象时,需要添加配置scope="prototype" 例如模型类student --><bean id="studentDao" class="com.ffyc.spring.dao.StudentDao"></bean><bean id="studentService" class="com.ffyc.spring.service.StudentService"><!--创建对象,并为属性注入值--><!--方法一:用set方法为属性赋值(推荐)--><property name="studentDao" ref="studentDao"></property><property name="student" ref="student"></property><!--方法二: 用构造方法赋值 --><!-- <constructor-arg name="studentDao" ref="studentDao"></constructor-arg>--></bean></beans>

注意: spring 生成对象的作用域 scope ="singleton / prototype" 

         singleton(默认):  在 Spring中只存在一个对象(bean) ,每次拿取都是同一个.

         prototype   :        相当于getBean()的时候都会创建一个新的对象

4. 注解方式实现属性赋值

(1). 开启注解扫描

<context:component-scan base-package="包名"> </context:component-scan>

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--配置 spring对哪个包下的类进行扫描--><context:component-scan base-package="com.ffyc.spring"> </context:component-scan></beans>

(2). 注解自己所创建的对象

@Component (value=“user”)等同于 <bean id=“user” class=“”></bean>

@Service

@Repository

以上注解都可以实现创建对象功能,为了后续扩展功能,在不同的层使用不同的注解标记
@Scope(value=“prototype”) 原型
@Scope(value=“ singleton ”) 单例
package com.ffyc.spring.model;import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;@Component //等价于配置注解  <bean id="student" class="com.ffyc.spring.model.Student" scope="prototype"></bean>
@Scope(value = "prototype")
public class Student {private int no;private String name ;public Student(){System.out.println("这是无参的构造方法");}
}
package com.ffyc.spring.dao;import com.ffyc.spring.model.Student;
import org.springframework.stereotype.Repository;@Repository(value = "studentDao")
public class StudentDao {public void saveStudent(Student student){System.out.println("保存学生");}
}
package com.ffyc.spring.service;import com.ffyc.spring.dao.StudentDao;
import com.ffyc.spring.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;//service服务层 进行事务管理 数据访问...
@Service
public class StudentService {@Autowired//相当于<property name="studentDao" ref="studentDao"></property>StudentDao studentDao;@AutowiredStudent student;/*  //方法二: 通过构造方法为属性赋值public StudentService(StudentDao studentDao, Student student) {this.studentDao = studentDao;this.student = student;}*/public void save(){/*StudentDao studentDao = new StudentDao();Student student = new Student();studentDao.*/studentDao.saveStudent(student);}//方法二: 通过set和get方法对属性赋值public StudentDao getStudentDao() {return studentDao;}public void setStudentDao(StudentDao studentDao) {this.studentDao = studentDao;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}
}

@Autowired: 是spring框架中提供的注解标签,默认要求注入的对象必须存在,如果不存在,会报错  可以根据属性 类型 或者 对象名字(value="名字")去spring框架中查找对象 

@Resource: 是Java中提供的注解标签 同样要求注入的对象也必须存在,如果不存在,会报错 


 可以根据属性 类型 或者 对象名字(value="名字")去spring框架中查找对象 

5. spring注解标签分类

(1). 生成对象的注解

@Component

@Service

@Repository

(2). 属性注入的注解

@Autowired

@Qualifier (value = "studao") 通过对象名查找

@Resource

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

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

相关文章

前端 大文件分片下载上传

前端 大文件分片下载上传 背景介绍&#xff1a; 当前项目是给投行部门做系统&#xff0c;业务方需要有专门的文档中心去管理文件&#xff0c;包括但是不限于文件的上传和下载等等。笔者本来就是采用的浏览器表单上传的方式进行文件上传&#xff0c;但是谁曾想在进行稍微大一点的…

【Python练习】097. 编写一个函数,实现简单的版本控制工具

097. 编写一个函数,实现简单的版本控制工具 097. 编写一个函数,实现简单的版本控制工具 示例代码 功能说明 使用方法 注意事项 实现方法 基于文件快照的实现方法 基于差异存储的实现方法 基于命令模式的实现方法 基于Git-like的实现方法 097. 编写一个函数,实现简单的版本控…

嵌入式硬件篇---Tof

TOF 的原理与本质TOF&#xff08;Time of Flight&#xff0c;飞行时间&#xff09;是一种通过测量信号&#xff08;通常是光&#xff09;在空间中传播时间来计算距离的技术。其本质是利用 “距离 速度 时间” 的物理公式&#xff1a;通过发射信号&#xff08;如激光、红外光&…

Vue diff简介

Vue3 diff 最长递增子序列双端diff 理念 相同的前置和后置元素的预处理&#xff0c;考虑边界情况&#xff0c;减少移动&#xff1b;最长递增子序列&#xff08;动态规划、二分法&#xff09;&#xff0c;判断是否需要移动 操作 前置与后置预处理判断是否需要移动 递增法&#x…

罗技MX Anywhere 2S鼠标修复记录

【现象】单击时总是出现双击的现象 【问题原因】从网络收集&#xff1a; 说法1&#xff0c;欧姆龙微动损坏&#xff1b;说法2&#xff0c;按键磨损导致按键和微动开关接触不良&#xff1b; 【问题排查】 微动损坏&#xff1a;拆掉壳子后&#xff0c;用手按住左键的微动开关&…

常见IP模块的仲裁策略和实现

在一个 Message Unit 中包含两个 Core&#xff08;处理器核心&#xff09;&#xff0c;它们之间访问共享资源&#xff08;如寄存器、FIFO、buffer 等&#xff09;时&#xff0c;仲裁机制&#xff08;Arbitration&#xff09; 是确保系统稳定性和正确性的关键。以下是常见的仲裁…

上周60+TRO案件,波比的游戏时间/丹迪世界/飞盘/迪奥/多轮维权,手表/汽车品牌持续发力,垃圾桶专利等新增侵权风险!

赛贝整理上周&#xff08;2025年8月11日-8月15日&#xff09;的TRO诉讼案件发案情况&#xff0c;根据赛贝TRO案件查询系统了解到&#xff0c;上周合计发起了超60起诉讼案件&#xff0c;涵盖 IP /品牌、版权、专利等多个领域&#xff0c;涉及影视、奢侈品、汽车、游戏、日常用品…

用 Python 在 30 分钟内搭一个「可回放的实时日志」——把攻击流量变成可视化剧情

业务背景 我们运营一款 FPS 端游&#xff0c;外挂作者常把 DDoS 伪装成「玩家掉线」来骗客服。以前排查要捞 CDN 日志、对时间戳、人工比对&#xff0c;平均 2 小时才能定位。现在用一条 30 行的 Python 脚本把边缘节点日志实时打到 Kafka&#xff0c;再回放到 Grafana&#xf…

如何将 LM Studio 与 ONLYOFFICE 结合使用,实现安全的本地 AI 文档编辑

人工智能正在改变我们的工作方式——但如今大多数 AI 工具都存在弊端&#xff1a;速度和便利性虽有所提升&#xff0c;但也意味着数据需要发送到外部服务器。对于教育工作者、企业、非政府组织以及任何处理敏感信息的人来说&#xff0c;这都是不可接受的风险。 LM Studio 和 O…

超市电商销售分析项目:从数据分析到业务决策

国际超市电商销售数据分析实战&#xff1a;从数据清洗到业务决策的完整流程 在电商行业&#xff0c;数据是驱动业务增长的核心引擎。本文将以国际超市电商销售数据为研究对象&#xff0c;完整拆解从数据准备 → 深度分析 → 策略输出的实战流程&#xff0c;涵盖数据清洗、多维度…

GitHub 热榜项目 - 日榜(2025-08-17)

GitHub 热榜项目 - 日榜(2025-08-17) 生成于&#xff1a;2025-08-17 统计摘要 共发现热门项目&#xff1a;12 个 榜单类型&#xff1a;日榜 本期热点趋势总结 本期GitHub热榜呈现三大技术趋势&#xff1a;1) AI基础设施持续爆发&#xff0c;Archon OS和Parlant聚焦AI任务管…

记忆翻牌游戏 greenfoot 开发

记忆翻牌游戏是一种经典的益智游戏&#xff0c;玩家需要翻开卡片并记住它们的位置&#xff0c;然后找到所有匹配的卡片对。 核心玩法 游戏开始时&#xff0c;所有卡片都是背面朝上玩家每次可以翻开两张卡片如果两张卡片图案相同&#xff0c;则保持翻开状态&#xff08;匹配成功…

【lucene】SegmentInfos

SegmentInfos 类中文说明 ———————————— **一句话** SegmentInfos 是 segments_N 文件的**内存表示**。它把磁盘上的 segments_N 读进来&#xff0c;变成一堆 SegmentInfo 的集合&#xff1b;当你增删改索引、合并段、提交时&#xff0c;再把它写回磁盘&#x…

Read Frog:一款开源AI浏览器语言学习扩展

Read Frog&#xff1a;一款开源AI浏览器语言学习扩展 来源&#xff1a;Poixe AI Read Frog&#xff08;中文名&#xff1a;陪读蛙&#xff09;是一款开源的浏览器扩展&#xff0c;旨在通过人工智能技术&#xff0c;将常规网页浏览转化为一种沉浸式的语言学习体验。该工具通过…

天地图应用篇:增加全屏、图层选择功能

天地图应用篇&#xff1a;增加全屏、图层选择功能本节说明&#xff1a; 目的&#xff1a; 实现地图的图层切换全屏显示 / 退出全屏案例截图 示下&#xff1a;案例代码示例代码&#xff1a; <template><div class"tianditu-map-container"><!-- 顶部搜…

从零开始,系统学习AI与机器学习:一份真诚的学习路线图

人工智能&#xff08;AI&#xff09;和机器学习&#xff08;ML&#xff09;正在深刻改变众多行业的面貌&#xff0c;掌握这些技术已成为许多技术从业者提升竞争力的重要路径。无论你是希望进入这个充满潜力的领域&#xff0c;还是期望在现有技术基础上进行拓展&#xff0c;一份…

NVIDIA CWE 2025 上海直击:从 GPU 集群到 NeMo 2.0,企业 AI 智能化的加速引擎

前言 8 月 8 日&#xff0c;我受邀参加了在上海举办的 NVIDIA CWE 大会。作为一个正在企业内部推动 AI 落地的从业者&#xff0c;这场会议对我来说不仅是“充电”&#xff0c;更像是一场“解题会”。参会感受 在分享干货之前&#xff0c;我先谈谈这次参会的不同感受。给我感受特…

Web攻防-大模型应用LLM安全提示词注入不安全输出代码注入直接间接数据投毒

知识点&#xff1a; 1、WEB攻防-LLM安全-API接口安全&代码注入 2、WEB攻防-LLM安全-提示词注入&不安全输出 Web LLM&#xff08;Large Language Model&#xff09;攻击指针对部署在Web端的AI大语言模型的攻击行为。攻击者通过恶意提示词注入、训练数据窃取、模型逆向工…

docker compose再阿里云上无法使用的问题

最原始的Dokcerfile # 使用官方Python 3.6.8镜像 FROM python:3.6.8-slimWORKDIR /app# 复制依赖文件 COPY requirements.txt .RUN pip install --upgrade pip # 检查并安装依赖&#xff08;自动处理未安装的包&#xff09; RUN pip install --no-cache-dir -r requirements.tx…

C++STL容器List的模拟实现

一、引言list的实现&#xff0c;还是比较简单的&#xff0c;大家只要想着土家楼的形状&#xff0c;画出图来就好了&#xff0c;不需要过多担心。本次的博客会发出一个完整的实现List的List.hpp&#xff0c;以后也会这样&#xff0c;主要是分段发被说孩子分段生。二、模拟List由…