[ 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
英文地址,請注明出處:http://en.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由…